0% found this document useful (0 votes)
9 views14 pages

Chap 0104

Uploaded by

udyadav430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Chap 0104

Uploaded by

udyadav430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lecture – 4

Control Statements
Control flow
• Flow Control, which refers to the ability to direct the flow of a program
based on specific conditions.
• This allows developers to control how their programs execute and can help
to make them more efficient and effective.

Control Statements
• Conditional Statements are used in programming oops to run a certain piece
of program only if a specific condition is met.
• The control statements are used to control the flow of execution of the
program.
Statements
1. Conditional or Selection statements

1.1 if - statement
The if statement is the simplest of the three and is used to run a certain piece
of code nly if a certain condition is true

For example:

int x = 5;
if (x == 5)
{
cout << "x is 5" << endl;
}

Output :

X is 5 In this example, the block of code inside the curly


braces will only be executed if the condition inside
the parentheses is true.
1.2 if-else

The if-else statement is used when we want to execute some code only if
some condition exists. If the given condition is true then code will be executed
otherwise else statement will be used to run other part of the code.

For example:

int x = 5;
if (x == 5)
{
cout << "x is 5" << endl;
}
else {
cout << "x is not 5" << endl;
}
Output: In this example, if the condition inside the parentheses is
true, the first block of code will be executed. Otherwise, the
X is 5
second block of code will be executed.
1.3 if-else-if Statement
The if-else-if ladder statement executes one condition from multiple statements.
Example :
int num;
cout<<"Enter a number :“<<endl;
cin>>num;
if (num <0 || num >100)
{
cout<<"wrong number";
}
else if(num >= 0 && num < 50){
cout<<"poor";
}
else if (num >= 50 && num < 80)
{
cout<<"good";
}
else if (num >= 80 && num <= 100) Output :
{ Enter a number :
cout<<"excellent"; 70 good
}
1.4 Switch Statement

The switch statement is used to execute different blocks of code based on the
value of a variable.

For example:

int x = 2; the switch statement will execute the


switch (x) { block of code associated with the value of
case 1: x. If x is 1, the first block of code will be
cout << "x is 1" << std::endl; executed. If x is 2, the second block of code
will be executed. If x is any other value, the
break;
default block of code will be executed.
case 2:
cout << "x is 2" << std::endl;
break; Output :
default:
X is 2
std::cout << "x is not 1 or 2" << std::endl;
break;
}
2. Loop or iterative statements

2.1 for loop


The for loop allows a program to execute a piece of program a fixed number of
times.

The syntax for the for loop is:


for (initialization; condition; increment/decrement)
{
// code to execute repeatedly
} Output:
1
Example : 2
for (int i = 1; i <= 3; i++) 3
{ 4
cout << i << endl; 5
}
2.2 while loop
The while loop is used to execute when we want to run some code for until some
specific condition matches.

The syntax for the while loop is: In this example, the while loop will
while (condition) continue to execute the block of code
{ inside the curly braces as long as x is
// code to execute repeatedly less than 5. Each time the loop executes,
} the value of x will be incremented by 1.

For example: Output :


0
int x = 0;
1
while (x < 5)
{
2
cout << x << endl; 3
x++; 4
}
2.3 do-while loop
The do-while loop is the same as the while loop, but the condition is checked after
the first iteration of the loop.

The syntax for the do-while loop is:


do{
// code to execute repeatedly the do-while loop will execute the block of
} code inside the curly brackets, and then it
while(condition) will check the condition. So it will be
executed a minimum of one time.
For example:

int x = 0; Output :
do { 0
cout << x << endl; 1
x++; 2
} 3
while (x < 5); 4
3. Flow control and jump statements
3.1 return statement
The return statement in C++ is used to terminate the execution of a function and
return control to the calling function. It can also be used to return a value from the
function.
Syntax:
Return (expression); This example return the sum (in which the parameter
is passed and add 5 to the parameter )
Example :
int myFunction(int x) {
return 5 + x;
} Output
:
int main() { 8
cout << myFunction(3);
return 0;
}
3.2 break statement

The break is used to break loop or switch statement. It breaks the current flow of the
program at the given condition. In case of inner loop, it breaks only inner loop.

Syntax:
jump-statement;
break;

Example :

for (int i = 1; i <= 10; i++)


{ Output:
if (i == 5) 1
{ 2
break; 3
} 4
cout<<i<<"\n";
}
3.3 continue Statement

The continue statement is used to continue loop. It continues the current flow of the
program and skips the remaining code at specified condition. In case of inner loop, it
continues only inner loop.

Syntax : Output :
jump-statement; 1
continue; 2
3
Example : 4
6
for(int i=1;i<=10;i++ 7
){ 8
if(i==5){ 9
continue; 10
}
cout<<i<<"\n";
}
Do Subscribe

You might also like