2. • Control Structure
• if Statement
• if-else Statement
• if-else-if Statement
• Nested if Statement
• Switch Statement
• Difference between if-else-if and switch Statement
• While Loop
• Do while Loop
• For Loop
Copyright @ IT Series
Topics
Contents
4. Sequence
Executes the statements in the same order in which they are written in the program
Entry point
Exit point
Flowchart
Copyright @ IT Series
5. Selection
A control structure which chooses alternative to execute
Executes a statement or set of statements based on a condition
Also called decision-making structure or conditional
structure.
Different types of selection structures in C language:
If
if-else
if-else-if
switch
Flowchart
Copyright @ IT Series
Exit point
Entry point
6. Repetition
A control structure which executes a statement or set of statements repeatedly for a specific number of times
Also called iteration structure or loop
Different types of loops available in C language:
while loop
do-while loop
for loop
Entry point
Exit point
Flowchart
Copyright @ IT Series
7. The if statement is the primary selection control structure
It is used to execute or skip a statement or set of statements by checking a condition
The condition is given as a relational expression. e. g marks >=40
Syntax / General Form
The syntax of if statement for single statement:
if (condition)
statement;
The syntax for compound statements:
if (condition)
{
statement 1;
statement 2;
statement N;
}
if is a keyword in C
language. It is always
written in lowercase.
Block of statements
inside the braces is
called the body of the if
statement. If there is
only 1 statement in the
body, the { } may be
omitted.
Do not place ; after (condition)
Copyright @ IT Series
8. If Programs
#include <iostream>
using namespace std;
int main() {
int marks;
// Taking input for marks
cout << "Enter your marks: ";
cin >> marks;
// Checking if marks are 40 or more
if (marks >= 40) {
cout << "Congratulations! You have passed." << endl;
}
return 0;
}
Output:
Enter your marks: 65
Congratulations! You have passed.
9. #include <iostream>
#include <cmath>
using namespace std;
int main() {
int num1, num2;
// Input for the first number
cout << "Enter the first number: ";
cin >> num1;
// Input for the second number
cout << "Enter the second number: ";
cin >> num2;
// Checking if the second number is the square of the first number
if (num2 == (num1 * num1)) {
cout << num2 << " is the square of " << num1 << "." << endl;
}
return 0;
}
Output:
Enter the first number: 5
Enter the second number: 25
25 is the square of 5.
10. The simplest selection structure but it is very limited in its use
Statement or set of statements is executed if the condition is true
But if the condition is false then nothing happens (no alternate action is performed)
A user may want to:
Execute one statement or set of statements if the condition is true
Execute other statement or set of statements if the condition is false
In this situation, simple if statement cannot be used effectively
Solution ‘if-else’ structure can be used to handle this kind of situation effectively
Example
A program should display Pass! if the student gets 40 or more marks
It should display Fail! if the student gets less than 40 marks
Simple if statement cannot be used to handle this situation
Copyright @ IT Series
17. Copyright @ IT Series
Write a program that input three number. determine whether numbers are equal or not
using nested if structure.
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
// Input for three numbers
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
// Checking if numbers are equal using nested if structure
if (num1 == num2) {
if (num2 == num3) {
cout << "All three numbers are equal." << endl;
} else {
cout << "First two numbers are equal, but the third number is different." << endl;
}
} else {
cout << "The numbers are not all equal." << endl;
}
return 0;
}
Output:
Enter three numbers: 5 5 5
All three numbers are equal.
18. Select one of several alternatives when selection is based on the value of a single variable or an expression
In C, the value of this expression may be of type int or char
The switch statement is a better way of writing a program when a series of if-else-if occurs
Syntax / General Form
Copyright @ IT Series
19. Rules of using switch case in C/C++ program
• The case label must integer or character
• Each case label must be unique
• A switch statement can have only one default label
• The default label can be used anywhere in switch statement
• The case label must end with colon. The default label is optional
Copyright @ IT Series
#include <iostream>
using namespace std;
int main() {
int day;
// Input for the day of the week (1-7)
cout << "Enter a number corresponding to a day of the week (1-7): ";
cin >> day;
// Using switch statement to display the day of the week
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input! Please enter a number between 1 and 7." <<
endl;
break;
}
return 0;
Output:
Enter a number corresponding to a day of the week (1-7): 3
Wednesday
21. Loop
The control structure that executes a statement or set of statements repeatedly is called loop.
Loops are also known as iteration or repetition structure
Loops are basically used for two purposes:
They are used to execute a statement or number of statements for a specified number of
times
Example: A user may display his name on screen for 10 times
The loops are also used to get a sequence of values
Example: A user may display a set of natural numbers from 1 to 10
There are three types of loops available in C:
while loop
do-while loop
for loop
22. while loop is the simplest loop of C
It executes one or more statements while the given condition remains true
It is useful when the number of iterations is not known in advance
Syntax
Condition is evaluated:
if it is true, the statement(s) are executed, and then condition is evaluated again
if it is false, the loop is exited
An iteration is
an execution of
the loop body.
while is a
keyword. It is
always written in
lowercase.
Loop body
Don’t put ; after (condition)
while is a pretest
loop (the condition
is evaluated before
the loop executes)
Copyright @ IT Series
Flowchart
23. while Loop Example
Copyright @ IT Series
int n;
n = 1;
while(n<=5)
{
printf(“Pakistann");
n++;
}
Produces output:
Counter Variable
A variable that is incremented or decremented each time a loop iterates
It can be used to control the execution of the loop (as a loop control variable)
It must be initialized before entering loop
It may be incremented/decremented either inside the loop or in the loop test
n++; is the same as n= n + 1;
Don’t put ; after (condition)
Loop body
while is a pretest
loop (the condition
is evaluated before
the loop executes)
cout<<“Pakistann”;
24. Infinite loop
Copyright @ IT Series
The loop must contain code to allow the condition to eventually become false so the loop can be exited
Otherwise, you have an infinite loop
Example
int n; n = 1;
while(n<10)
cout<<n<<"t";
Infinite loop because n is always < 10
A loop that does not
stop is known as an
infinite loop
25. This loop executes one or more statements while the given condition is true
In this loop, the condition comes after the body of loop
The loop body always executes at least once
Syntax:
Execution continues as long as the condition is true; the loop is exited when the condition becomes false
; after (condition) is also required
The loop executes at least once even if the condition is false in the beginning
An error occurs if the semicolon
is not used at the end of the
condition.
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Copyright @ IT Series
26. do-while Loop Example
Copyright @ IT Series
int n;
n = 1;
do
{
cout<<“Welcome to Cn”;
n++;
}
while(n<=5);
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Condition
28. It is a pretest loop that executes one or more statements for a specified number of times
This loop is also called counter-controlled loop
It is useful with counters or if precise number of iterations is known
Syntax / General Form Example
for (initialization; condition; increment/decrement)
{
statement 1;
statement 2;
statement N;
}
Flowchart
Copyright @ IT Series
cout<<“Welcom to C++n”;
29. You can define variables in initialization code
for (int n=1; n <= 5; n++)
Can omit initialization if already done
int n = 1;
for (; n <= 5; n++)
Can omit update if done in loop body
for (n = 1; n<= 5;)
n++;
The condition in for loop is mandatory. It cannot be omitted
int n=1;
for (;n<= 5;)
n++;
Copyright @ IT Series