SlideShare a Scribd company logo
1 of 26
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

Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
C statements
C statementsC statements
C statementsAhsann111
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
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.pptManojKhadilkar1
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdfBasirKhan21
 
While loop
While loopWhile loop
While loopFeras_83
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Abou Bakr Ashraf
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptxsujatha629799
 
Decision control structures
Decision control structuresDecision control structures
Decision control structuresRahul Bathri
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ KUMAR
 
C++ decision making
C++ decision makingC++ decision making
C++ decision makingZohaib 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

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 

Recently uploaded (20)

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 

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