2. ❏ Control structures
❏ A Program is nothing but a set of statements written in sequential order.
❏ These statements are executed one after the other
❏ Sometimes it may happen that the programmer requires to alter the flow of
execution, or to perform the same operation for fixed iterations or whenever
the condition does not satisfy. In such a situation the programmer uses the
control structure.
❏ There are various control structures supported by C++. Programs which use
such (one or three) control structures are said to be structured programs.
1
3. ❏ Decision making in C++
❏ Decision making is about deciding the order of execution of statements
based on certain conditions or repeat a group of statements until certain
specified conditions are met.
❏ C++ handles decision-making by supporting the following statements,
❏ if statement
❏ switch statement
❏ conditional operator statement
❏ goto statement
2
4. ❏ Decision making with if statement
❏ The if statement may be implemented in different forms depending on the
complexity of conditions to be tested. The different forms are,
❏ Simple if statement
❏ If....else statement
❏ Nested if....else statement
❏ else if ladder
3
5. ❏ Simple if statement
❏ The general form of a simple if statement is,
if (expression)
{
statement-inside;
{
statement -outside;
❏ If the expression is true, then 'statement-inside' it will be executed, otherwise
'statement-inside' is skipped and only 'statement-outside' is executed.
4
6. ❏ Example
❏ #include< iostream.h>
int main( )
{
int x,y;
x=15;
y=13;
if (x > y )
{
cout << "x is greater than y";
getch();
}
}
5
7. ❏ If...else statement
❏ The general form of a simple if...else statement is,
if( expression )
{
Statement-block 1;
{
else
{
statement-block 2;
}
❏ If the 'expression' is true, the 'statement-block 1' is executed, else 'statement-block 1'
is skipped and 'statement-block 2' is executed.
6
9. ❏ Example
❏ void main( )
{
int x,y;
x=15;
y=18;
if (x > y )
{
cout << "x is greater than y";
}
else
cout << "y is greater than x";
}
}
8
10. ❏ Nested if….else statement
❏ The general form of a nested if...else statement is,
if( expression )
{
if( expression1 )
{
Statement-block 1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
❏ if 'expression' is false the
'statement-block 3' will be executed,
otherwise it continues to perform the
test for 'expression 1' . If the
'expression 1' is true the 'statement-
block 1' is executed otherwise
'statement- block 2' is executed.
9
11. ❏ Switch statement
❏ switch statement :- this is multiple conditional statement switch check the
condition if condition is true then perform the statement and totally depend
upon the value of variable otherwise perform the default statement
❏ Prototype :- switch < expression>
Case <statement 1 >
Case <statement 2 >
Case <statement 3 >
Case <statement 4 >
default <statement >
10
12. ❏ The jump statement
❏ C/C++ has four statements that perform an unconditional control transfer.
❏ These are return ( ), goto, break and continue.
❏ Of these, return( ) is used only in functions.
❏ The goto and return ( ) may be used anywhere in the program .
❏ Continue and Break statements may be used only in conjunction with a loop
statement.
❏ In ‘switch case’ and ‘break’ is used most frequently.
11
13. ❏ goto statement
❏ This statement does not require any condition. This statement passes control
anywhere in the program without least care for any condition.
❏ Syntax :
goto label;
__
__
__
label:
❏ label is any valid label
either before or after
goto.
❏ The label must start
with any character
and can be
constructed with rules
used for forming
identifiers
12
15. ❏ Break statement
❏ The break statement allows the programmer to terminate the loop .
❏ The break skips from the loop or the block in which it is defined.. The control
then automatically passes on to the first statement after the loop or the block.
❏ The break statement can be associated with all the conditional statements
(especially switch( ) case.
❏ the break statement ends execution of the nearest enclosing do, for, or while
statement.
❏ We can also use break statements in the nested loops.
❏ The widest use of this statement is in switch case where it is used to avoid flow
of control from one case to other.
14
16. ❏ Loops in c++
❏ Loops : :- repetition of instructions is called loop there are following loop in
c/c++ language .
15
17. ❏ contd..
❏ A sequence of statement is executed until a specified condition is true. This
sequence of statement to be executed is kept inside the curly braces { }
known as loop body.
❏ After every execution of loop body, condition is checked, and if it is found to
be true the loop body is executed again. When condition check comes out
to be false, the loop body will not be executed.
❏ There are three types of loops:
❏ for loop
❏ while loop
❏ do..while loop
16
18. ❏ Loops in c++
❏ The for statement is equivalent to the while and do-while statements.
❏ The only difference between for and while is that the latter checks the logical
condition and then executes the body of the loop, whereas the for statement
test is always performed at the beginning of the loop.
❏ The body of the loop may not be executed at all times if the condition fails at the
beginning.
❏ The do while loop executes the body of the loop at least once regardless of the
logical condition
17
19. ❏ for loop
❏ for loop is used to execute a set of statement repeatedly until a particular
condition is satisfied. we can say it an open ended loop.
❏ Syntax:
for(initialization; condition ;increment/decrement)
{
statement-block;
}
❏ In for loop we have exactly two semicolons, one after
initialization and second after condition.
❏ In this loop we can have more than one initialization
or increment/decrement, separated using comma
operator.
❏ for loop can have only one condition.
18
21. ❏ Example
❏ #include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i)
{
factorial *= i; // factorial = factorial * i;
}
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
}
20
22. ❏ While loop
❏ while loop can be address as an entry control loop. It is completed in 3
steps.
❏ Variable initialization.( e.g int x=0; )
❏ condition( e.g while( x<=10) )
❏ Variable increment or decrement ( x++ or x-- or x=x+2)
❏ Syntax:
variable initialization ; while (condition)
{
statements ;
variable increment or decrement ;
} 21
24. ❏ do...while loop
❏ In some situations it is necessary to execute body of the loop before testing
the condition.
❏ This is also called as post test loop
❏ Such situations can be handled with the help of do-while loop. do statement
evaluates the body of the loop first and at the end, the condition is checked
using while statement.
❏ The do..while loop is an exit condition loop.
23
25. ❏ Syntax and flowchart for do...while loop
❏ Syntax :
do
{
. statement is;
}
while(condition)
24