0% found this document useful (0 votes)
2 views

Lecture 3

This document is a lecture on the basics of programming in C++, covering numbers, identifiers, and statements. It explains the types of numbers (integer and real), the characteristics of identifiers, and the distinction between single and compound statements. Additionally, it includes programming examples and an assignment for further practice.

Uploaded by

farhannisar578
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3

This document is a lecture on the basics of programming in C++, covering numbers, identifiers, and statements. It explains the types of numbers (integer and real), the characteristics of identifiers, and the distinction between single and compound statements. Additionally, it includes programming examples and an assignment for further practice.

Uploaded by

farhannisar578
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Programming

Language

Lecture 3
Presented By
Farhan Nisar
Program: BS(IT)
[email protected]
PhD (In Progress)
Research Area: Cloud Computing Security, AI Techniques to detect Cyber Threads
1
Overview

1. Numbers in C++
2. Types of Identifiers
3. Statements in C++
4. Examples

2
Numbers in C++

• Numbers represent numeric data in a program.


• Divided into two main categories:
1. Integer Numbers (whole numbers)
2. Real Numbers (numbers with fractional parts)

3
Integer Numbers
• Description:
• Whole numbers, both positive and negative.
• Example:
#include <iostream>
using namespace std;

int main() {
int count = 100; // Integer variable
cout << "Count: " << count << endl;
return 0;
}
Output Count:100

• Note: Integer types include int, short, long, and their modifiers. 4
Real Numbers
• Description: Numbers with decimal points, like 3.14.
• Stored in float, double, or long double.

• Example:
#include <iostream>
using namespace std;

int main() {
double price = 99.99; // Real number stored as double
cout << "Price: " << price << endl;
return 0;
}

5
Identifiers in C++
Types of Identifiers

• Definition: Names used to identify variables, constants, functions,


etc.
• Must begin with a letter or underscore, followed by letters, digits, or
underscores.
Characteristics of identifier:
Alphanumeric: Can consist of letters (A-Z, a-z), digits (0-9)
Cannot start with a digit: For example int 2number is invalid.

int main() {
int age = 25; // 'age' is an identifier
float height = 5.9; // 'height' is an identifier
cout << "Age: " << age << endl;
cout << "Height: " << height << " feet"endl;
return 0;
}
Categories of Identifiers:
1. Constant Identifiers
2. Variable Identifiers
6
Constant Identifiers
• Description: Values that do not change during
program execution.
• Declared using the const keyword.

• Example:
#include <iostream>
using namespace std;

int main() {
const double pi = 3.14159; // Constant identifier
cout << "Value of Pi: " << pi << endl;
return 0; 7
Variable Identifiers
• Description: Names representing memory locations that store values
which can change during execution.
•Variable identifiers help the compiler and programmer differentiate between various
data items.
•Always choose meaningful names to make your code easier to understand.

Example:
#include <iostream>
using namespace std;
int main() {
int age = 20; // Variable identifier
age = 25; // Variable value changed
cout << "Age: " << age << endl;
return 0;
} 8
Example of variable identifier
• #include <iostream> // Required for input and output

• int main() {
• // Declaring variables with different identifiers
• int age = 7; // 'age' is an identifier
• float height = 1.2; // 'height' is an identifier
• string name = "Farhan"; // 'name' is an identifier

• // Displaying the variables' values


• cout << "Name: " << name << endl;
• cout << "Age: " << age << :endl;
• cout << "Height: " << height << " meters" << endl;

• return 0;
• }
Output ?

9
Statements in C++

• Definition: A command or instruction that the program executes.

• Divided into:
1. Single Statements
2. Compound Statements

10
Single Statements
• Description: A single line of instruction ending with a semicolon.

• Example:
#include <iostream>
using namespace std;

int main() {
int x = 10; // Single statement
cout << "Value: " << x << endl; // Single statement
return 0;
}

11
Compound Statements
• Description: A block of multiple statements enclosed in {}.
• Typically used with control structures.
• Compound statements are often used in control structures like if, for,
while and switch statement to execute multiple statement together

• Example:
Syntax:
#include <iostream> {
using namespace std; // Multiple statements
statement1;
int main() { statement2;
statement3;
int num = 10;
}
if (num > 0) { // Compound statement starts
cout << "Number is positive." << endl;
num += 5; // Adding 5 to num
} // Compound statement ends
12
return 0;
Program Demonstration
Example: Combining numbers, identifiers, and statements
#include <iostream>
using namespace std;

int main() {
const double taxRate = 0.05; // Constant identifier
double price = 100.0; // Variable identifier
double total = price + (price * taxRate); // Single statement
// Compound statement
if (total > 100) {
cout << "Total exceeds $100!" << endl;
} else {
cout << "Total is within budget." << endl;
}
return 0;
13
}
Summary
• Numbers are represented as integers and real numbers in C++.
• Identifiers define constants or variables in the program.
• Statements are the building blocks of program logic, either single or
compound.

Clear understanding of these basics is essential for writing effective C++


programs.

14
Assignment

1. Difference between integer, float, character with


programming example?
2. What is Modifiers and how many types of Modifiers?
3. Write a program that can add, subtract, multiply and division
in C++?

Deadline: The last date for submitting assignments is the last class
of the week.
Only Handwritten assignments will be accepted.
15

You might also like