Lecture 5
Lecture 5
Language
Lecture 5
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. Expressions
2. Initialization Statement
3. Increment Operator
• Prefix Form
• Postfix Form
4. Special Functions
• getche()
• getch()
5. Escape Sequences 2
Expressions in C++
• Definition: A combination of variables, operators, and values that
produce a result.
• A statement that evaluate to the value is called expression,Another
Defination Combination of Operand. Constant and variables
• Expression = operator (symbol) + Operand (Value)
Oprand can be variable or a function
• Example A+B; A B is operand + is operator
X+100; variable+value
(7+3)*b; 7 and 3 constant, b variables, + * are operators
Types:
1. Arithmetic Expression: Uses arithmetic operators.
2. Logical Expression: Uses logical operators.
3. Relational Expression: Uses relational operators.
3
Expressions in C++
Example:
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 5;
int result = x + y; // Arithmetic expression
cout << "Result: " << result << endl;
return 0;
} //********Program Run in Visual studio 2022************\\
4
Expressions in C++
Types:
1. Arithmetic Expression: Uses arithmetic operators.
A+2*b; (+.-,*,/,%)
5*(a-b);
2. Logical Expression: Uses logical operators.
Two condtion combine
((ch>=‘a’) && (ch<=‘z’))||((ch<=‘a’)&&(ch<=‘z’))
3. Relational Expression: Uses relational operators.
A>B; A<B (==, !=, <,>, <=, >=)
;
}
5
Initialization Statement
• Definition: Assigning an initial value to a variable at the time of its
declaration.
• Syntax: data_type variable_name = value;
Example:
#include <iostream>
using namespace std;
int main() {
int age = 20; // Initialization statement
cout << "Age: " << age << endl;
return 0;
}
6
Initialization Statement
Program run in visual studio 2022
7
Increment and Decrement Operators
• Definition: Increases the value of a variable by 1. while in decrement
it decrease the value by 1.
• {
int a=5;
a++; output=5
}
Forms:
1. Prefix Form (++x): Increment happens before the variable is
used.
2. Postfix Form (x++): Increment happens after the variable is used.
3. Prefix Form (--x): decrement happens before the variable is used.
4. Postfix Form (x--): decrement happens after the variable is used
8
Prefix Increment (++x)
• Description: The value is incremented first, then used.
• First the variable x is incremented then update the value of x is
returned.
Example:
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << "Prefix Increment: " << ++x << endl; // Increments x before
printing
return 0;
}
9
Prefix Increment (++x)
run in visual studio 2022
10
Postfix Increment (x++)
• Description: The value is used first, then incremented.
First the current value of the variable x is used in the expression
• The variable x is incremented by 1.
Example:
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << "Postfix Increment: " << x++ << endl; // Prints x first, then
increments
cout << "After Increment: " << x << endl; // Displays updated value of x
return 0;
} 11
Postfix Increment (x++)
Program run in visual studio 2022
12
Special Functions: getche() and getch() 1.1
• getche() Function:
• Reads a single character from the keyboard and echoes(display) it
to the screen. It is useful when you want the user to see the
character they types as part of input feedback.
Example (requires <conio.h>):
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
cout << "Press a key: ";
char ch = getche(); // Echoes the input character
cout << "\nYou pressed: " << ch << endl;
return 0;
} 13
Special Functions: getche() and getch() 1.2
• getch() Function:
• Reads a single character but does not echo (display) it to the
screen.
It is useful when you want to capture input without showing it
such as password input or reading control characters
Example:
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
cout << "Press a key: ";
char ch = getch(); // Does not echo input
cout << "\nYou pressed: " << ch << endl;
return 0; 14
}
Key Differences
Features Getch () Getche()
Echo on screen No, the input Yes input is
character is visible
hidden
Input visibility Hidden (silent Visible input
Input) displayed
Common use Password input, Interactive
cases silent control prompts or
feedback
15
Escape Sequences
• Definition: Special character combinations that perform specific actions in text
formatting.
• Common Escape Sequences in C++:
• \n: Newline
• \t: Tab
• \\: Backslash
• \': Single quote
• \": Double quote
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello\nWorld!\tTabbed Text\\Backslash" << endl;
return 0;
}
16
Escape Sequences
Program in visual studio 2022
17
Escape Sequences
Program in visual studio 2022
18
Escape Sequences
Program in visual studio 2022
19
Summary
• Expressions perform calculations or comparisons.
20
Assignment No 4 and Quiz
21