Sec 03 Conditional statement conditional statement is used in programming to make decisions .pdf

AbdullahEmam4 0 views 24 slides Oct 08, 2025
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

A conditional statement is used in programming to make decisions — it lets the program choose what to do based on whether a condition is true or false. Respond to user input
Control the flow of execution
Perform different actions under different conditions


Slide Content

Conditional Statement in C++
Sec 03
Prepared by: Abdullah Emam

Comparison Operators
different comparison operators such as ==,!=,>,< etcthat can be
used in C++ and which data types they can be applied to.
As the name implies, conditional statements specify whether another
statement or block of statements should be executed or not. These
are often called “selection constructs”. The two general types are:
“if…then”
the “switch…case” construct
The conditions tested are specified using comparison
operators. These operators cause the immediate statement in
which they are contained to return a Boolean value of either
true or false.

Comparison Operators Syntax
Equality: ==, or Inequality: !=of any primitive data type (int, char, float, bool, etc.)
These are binary operators (take two operands) and are specified using infix notation
(which means that the operator goes in between the two operands).
Greater-than:>, Greater than or equal to: >=, Less-than: <and Less than or equal
to: <=are also binary operators using infix notation. Use only with numeric data
types; there are specific functions for comparing other data types.
Negation: ! is a unary operator, and prefixes the operand.

if-else Statement
There aretwovariations to theif…thenconditional. Let us take a simple case
and explain it line-by-line.
#include <iostream>
using namespace std;
int main() {
int x = 7; //change this value to see the result for else condition
if (x == 7) {
//this code is executed only if x does indeed contain the integer
value 7
cout << "Value of x is 7"<<endl;
} else {
//this code is executed if the preceding if condition evaluated to
false
cout << "Value of x is not 7 "<<endl;
}
return 0;
}

C++ if...else...else if statement
The if...else statement is used to execute a block of code among two alternatives. However,
if we need to make a choice between more than two alternatives, we use the if...else if...else
statement.
The syntax of the if...else if...else statement is:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}

// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
}
else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}

C++ Nested if...else
Sometimes, we need to use an ifstatement inside another ifstatement.
Thisis known as nested ifstatement.
// outer if statement
if (condition1) {
// statements
// inner if statement
if (condition2) {
// statements
}
}

// C++ program to find if an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
// outer if condition
if (num != 0) {
// inner if condition
if (num > 0) {
cout << "The number is positive." << endl;
}
// inner else condition
else {
cout << "The number is negative." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This line is always printed." << endl;

C++ switch..caseStatement
The switchstatement allows us to execute a block of code among many alternatives.
The syntax of the switchstatement in C++ is:
switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;
case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}

// Program to build a simple calculator
using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *,
/): ";
cin >> oper;
cout << "Enter two numbers: " <<
endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " <<
num2 << " = " << num1 + num2;
break;

case '-':
cout << num1 << " - " << num2 << " = "
<< num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = "
<< num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = "
<< num1 / num2;
break;
default:
// operator is doesn't match any case
constant (+, -, *, /)
cout << "Error! The operator is not
correct";
break;
}
return 0;
}

Quiz
Which of the following is not a comparison
operator in C++ language?
A)>
B)<
C)>=
D)=
Which of the following expressions
evaluates to false?
A)5>=(1+6)-4
B)3!=4-1
C)8-1>4
D)6>=5
If originally,
int x=10;
int y=5;
int z=2;
then which of the following
expressions evaluate to false:
A)
(x+1)!=y-2
B)
!(x+y>20)
C)
(x+1==y)
D)
x+1<z*10

If originally x=2, y=3, and z=5,what is the
value of x,y, and z after executing the
following code?
if (x+1==y)
{
y=y+1;
}
else
{
x++;
}
A)x=4, y=2, z=5
B)x=2, y=4,z=5
C)x=5, y=5, z=2
D)x=3, y=4, z=5
If originally x=1,y=2, and z=3, what is the value
of x, y, and z after executing the following code?
switch(x) {
case 0:
x++;
z=x+1;
break;
case 1:
y=z+x;
break;
default:
z=z+x;
}
A)
x=2, y=2,z=2
B)x=1, y=2,z=4
C)x=1, y=4,z=3
D)x=3, y=2,z=4

Program in C++ to check that whether a number
is even or odd
Step 1:Start
Step 2:Take Input and Read the Numbers
Step 3:Check that If Number % 2 == 0
If true Then
Print : Your selected Number is an Even Number.
Else
Print : Your selected Number is an Odd Number.
Step 4:Finish

Switch case

IF

Take two int values from user and print greatest among
them.

A school has following rules for grading system:
a. Below 25 -F
b. 25 to 45 -E
c. 45 to 50 -D
d. 50 to 60 -C
e. 60 to 80 -B
f. Above 80 -A
Ask user to enter marks and print the corresponding
grade.