SlideShare a Scribd company logo
Control
Structures
Decision making and
statements
Done by :
Elakkiya.S
I Bsc.cs-B
❏ 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
❏ 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
❏ 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
❏ 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
❏ Example
❏ #include< iostream.h>
int main( )
{
int x,y;
x=15;
y=13;
if (x > y )
{
cout << "x is greater than y";
getch();
}
}
5
❏ 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
❏ Flowchart for if...else statement
7
❏ 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
❏ 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
❏ 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
❏ 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
❏ 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
❏ Flowchart for goto statement
13
❏ 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
❏ Loops in c++
❏ Loops : :- repetition of instructions is called loop there are following loop in
c/c++ language .
15
❏ 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
❏ 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
❏ 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
❏ Flowchart for for loop
19
❏ 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
❏ 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
❏ Flowchart for while loop
22
❏ 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
❏ Syntax and flowchart for do...while loop
❏ Syntax :
do
{
. statement is;
}
while(condition)
24
Control structures

More Related Content

Similar to Control structures

UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
DhanushKumar610673
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
C statements
C statementsC statements
C statements
Ahsann111
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
BasirKhan21
 
While loop
While loopWhile loop
While loop
Feras_83
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
Abou Bakr Ashraf
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Abdii Rashid
 
C language 2
C language 2C language 2
C language 2
Arafat Bin Reza
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
NkurikiyimanaGodefre
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
sujatha629799
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
Rahul Bathri
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 

Similar to Control structures (20)

UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
C statements
C statementsC statements
C statements
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
While loop
While loopWhile loop
While loop
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
C language 2
C language 2C language 2
C language 2
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 

Recently uploaded

Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 

Recently uploaded (20)

Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 

Control structures

  • 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
  • 8. ❏ Flowchart for if...else statement 7
  • 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
  • 14. ❏ Flowchart for goto statement 13
  • 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
  • 20. ❏ Flowchart for for loop 19
  • 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
  • 23. ❏ Flowchart for while loop 22
  • 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