Lecture 3
Lecture 3
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++
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
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
• return 0;
• }
Output ?
9
Statements in C++
• 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.
14
Assignment
Deadline: The last date for submitting assignments is the last class
of the week.
Only Handwritten assignments will be accepted.
15