SlideShare a Scribd company logo
Control Structures in C++
1)While
2)do/while,
3)for
4) Switch
5) Break
6)continue
The while Repetition Structure
• Repetition structure
– while loop repeated until condition becomes false.
• Flow chart:
condition statement
true
false
int main()
{
int x = 2;
while (x >= 0)
{
if ( x == 2)
{
cout << “Value of x is : “ << x << endl;
}
x = x – 1;
}
return 0;
}
• Common errors:
– infinite loop
– unitialized variables
The for Repetition Structure
• The general format when using for loops is
for ( initialization; LoopContinuationTest; increment )
statement
• Example:
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
– Prints the integers from one to ten
Flowchart for for
Condition
Test the variable
statement
true
false
Increment variable
Initialize variable
1 // sum_even.cpp
2 // Summation with for
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int main()
9 {
10 int sum = 0;
11
12 for ( int number = 2; number <= 100; number += 2 )
13 sum += number;
14
15 cout << "Sum is " << sum << endl;
16
17 return 0;
18 }
Sum is 2550
• Program to sum the even numbers from 2 to 100
The switch Multiple-Selection Structure
• switch
– Useful when variable or expression is tested for
multiple values
– Consists of a series of case labels and an optional
default case
– break is (almost always) necessary
General form of switch statement:
switch (expression)
{
case val1:
statement
break;
case val2:
statement
break;
….
case valn:
statement
break;
default:
statement
break;
}
flowchart
true
false
.
.
.
case a case a action(s) break
case b case b action(s) break
false
false
case z case z action(s) break
true
true
default action(s)
The do/while Repetition Structure
• The do/while repetition structure is similar to the
while structure,
– Condition for repetition tested after the body of the loop
is executed
• Format:
do {
statement
} while ( condition );
• Example (letting counter = 1):
do {
cout << counter << " ";
} while (++counter <= 10);
– This prints the integers from 1 to 10
• All actions are performed at least once.
true
false
statement
condition
The break and continue Statements
• Break
– Causes immediate exit from a while, for,
do/while or switch structure
– Program execution continues with the first
statement after the structure
– Common uses of the break statement:
• Escape early from a loop
• Skip the remainder of a switch structure
• Continue
– Skips the remaining statements in the body of a
while, for or do/while structure and proceeds
with the next iteration of the loop
– In while and do/while, the loop-continuation test is
evaluated immediately after the continue statement
is executed
– In the for structure, the increment expression is
executed, then the loop-continuation test is evaluated
The continue Statement
• Causes an immediate jump to the loop test
int next = 0;
while (true){
cin >> next;
if (next < 0)
break;
if (next % 2) //odd number, don’t print
continue;
cout << next << endl;
}
cout << “negative num so here we are!” << endl;
control statement examples
• Problem:1
A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a
program that analyzes the results. If more than 8 students pass, print "Raise
Tuition".
Problem : 2 Calculating compound interest
Where,
P=10000 is principle amount
R=5 is the rate and
T=2 is the time span
(Hint:Formula to calculate compound interest annually is given by:
Amount= P(1 + R/100)t
Compound Interest = Amount – P)
Problem :3 Program to build simple calculator using switch statement
Problem :4 Write a program to check user entered number is prime or not.

More Related Content

PPT
Control Statement.ppt
PPT
for loops in C++ and their functions and applicability
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPTX
Cs1123 6 loops
PPTX
SwitchandlomahsjhASSJjajdjsdjdjjjop.pptx
PPT
Visula C# Programming Lecture 4
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPT
03 conditions loops
Control Statement.ppt
for loops in C++ and their functions and applicability
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Cs1123 6 loops
SwitchandlomahsjhASSJjajdjsdjdjjjop.pptx
Visula C# Programming Lecture 4
Introduction to Java Programming - Lecture 11.pptx
03 conditions loops

Similar to Control structures.ppt (20)

PPTX
C Programming Control Structures(if,if-else)
PDF
Loop and while Loop
PPT
Chapter 3 Control structures.ppt
PPTX
control structure
PPT
control-statements....ppt - definition
PPT
FP 201 - Unit 3 Part 2
PPTX
Programming Fundamentals in C++ structures
PPT
Iteration
PPTX
Lec7 - Loops updated.pptx
PPT
Programming loop
PPTX
Ch6 Loops
PPT
Chapter 5 Looping
PPT
M C6java6
PPTX
Lecture 4
PPTX
Iterative control structures, looping, types of loops, loop working
PPT
C++ control loops
PDF
Repetition, Basic loop structures, Loop programming techniques
PPTX
Lect3-C--EEB.pptx
PDF
4th_Ed_Ch03.pdf
C Programming Control Structures(if,if-else)
Loop and while Loop
Chapter 3 Control structures.ppt
control structure
control-statements....ppt - definition
FP 201 - Unit 3 Part 2
Programming Fundamentals in C++ structures
Iteration
Lec7 - Loops updated.pptx
Programming loop
Ch6 Loops
Chapter 5 Looping
M C6java6
Lecture 4
Iterative control structures, looping, types of loops, loop working
C++ control loops
Repetition, Basic loop structures, Loop programming techniques
Lect3-C--EEB.pptx
4th_Ed_Ch03.pdf
Ad

More from Rahul Borate (20)

PPT
Bionomial and Poisson Distribution chapter.ppt
PPTX
Exponential Distribution chapter of stat.pptx
PPTX
Unit IV database intergration with node js
PPTX
Unit 1 Express J for mean stack and mern
PPTX
Unit II Developing Cyber Threat Intelligence Requirements.pptx
PPTX
Chapter I Introduction To Cyber Intelligence.pptx
PPT
Sampling of statistics and its techniques
PPT
sampling techniques of statistics and types
PPT
ch7 powerpoint presentation introduction.ppt
PPT
chapter 4 introduction to powerpoint .ppt
PPT
chapter ppt presentation engineering 1.ppt
PPT
student t-test (new) distrutuin t test.ppt
PPTX
scatter plots and visualization concept.pptx
PPT
Corelation and covariance and cocrrr.ppt
PPTX
Box Plot in stat using python hypothesis.pptx
PPTX
Data Visualization_pandas in hadoop.pptx
PPT
sampling for statistics and population.ppt
PPTX
Software and Hardware for requirements.pptx
PPTX
Hadoop and their in big data analysis EcoSystem.pptx
PPTX
UNIT II Evaluating NoSQL for various .pptx
Bionomial and Poisson Distribution chapter.ppt
Exponential Distribution chapter of stat.pptx
Unit IV database intergration with node js
Unit 1 Express J for mean stack and mern
Unit II Developing Cyber Threat Intelligence Requirements.pptx
Chapter I Introduction To Cyber Intelligence.pptx
Sampling of statistics and its techniques
sampling techniques of statistics and types
ch7 powerpoint presentation introduction.ppt
chapter 4 introduction to powerpoint .ppt
chapter ppt presentation engineering 1.ppt
student t-test (new) distrutuin t test.ppt
scatter plots and visualization concept.pptx
Corelation and covariance and cocrrr.ppt
Box Plot in stat using python hypothesis.pptx
Data Visualization_pandas in hadoop.pptx
sampling for statistics and population.ppt
Software and Hardware for requirements.pptx
Hadoop and their in big data analysis EcoSystem.pptx
UNIT II Evaluating NoSQL for various .pptx
Ad

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Insiders guide to clinical Medicine.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Lesson notes of climatology university.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Insiders guide to clinical Medicine.pdf
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Lesson notes of climatology university.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

Control structures.ppt

  • 1. Control Structures in C++ 1)While 2)do/while, 3)for 4) Switch 5) Break 6)continue
  • 2. The while Repetition Structure • Repetition structure – while loop repeated until condition becomes false. • Flow chart: condition statement true false int main() { int x = 2; while (x >= 0) { if ( x == 2) { cout << “Value of x is : “ << x << endl; } x = x – 1; } return 0; } • Common errors: – infinite loop – unitialized variables
  • 3. The for Repetition Structure • The general format when using for loops is for ( initialization; LoopContinuationTest; increment ) statement • Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; – Prints the integers from one to ten
  • 4. Flowchart for for Condition Test the variable statement true false Increment variable Initialize variable
  • 5. 1 // sum_even.cpp 2 // Summation with for 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int sum = 0; 11 12 for ( int number = 2; number <= 100; number += 2 ) 13 sum += number; 14 15 cout << "Sum is " << sum << endl; 16 17 return 0; 18 } Sum is 2550 • Program to sum the even numbers from 2 to 100
  • 6. The switch Multiple-Selection Structure • switch – Useful when variable or expression is tested for multiple values – Consists of a series of case labels and an optional default case – break is (almost always) necessary
  • 7. General form of switch statement: switch (expression) { case val1: statement break; case val2: statement break; …. case valn: statement break; default: statement break; }
  • 8. flowchart true false . . . case a case a action(s) break case b case b action(s) break false false case z case z action(s) break true true default action(s)
  • 9. The do/while Repetition Structure • The do/while repetition structure is similar to the while structure, – Condition for repetition tested after the body of the loop is executed • Format: do { statement } while ( condition ); • Example (letting counter = 1): do { cout << counter << " "; } while (++counter <= 10); – This prints the integers from 1 to 10 • All actions are performed at least once. true false statement condition
  • 10. The break and continue Statements • Break – Causes immediate exit from a while, for, do/while or switch structure – Program execution continues with the first statement after the structure – Common uses of the break statement: • Escape early from a loop • Skip the remainder of a switch structure
  • 11. • Continue – Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop – In while and do/while, the loop-continuation test is evaluated immediately after the continue statement is executed – In the for structure, the increment expression is executed, then the loop-continuation test is evaluated
  • 12. The continue Statement • Causes an immediate jump to the loop test int next = 0; while (true){ cin >> next; if (next < 0) break; if (next % 2) //odd number, don’t print continue; cout << next << endl; } cout << “negative num so here we are!” << endl;
  • 13. control statement examples • Problem:1 A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results. If more than 8 students pass, print "Raise Tuition". Problem : 2 Calculating compound interest Where, P=10000 is principle amount R=5 is the rate and T=2 is the time span (Hint:Formula to calculate compound interest annually is given by: Amount= P(1 + R/100)t Compound Interest = Amount – P) Problem :3 Program to build simple calculator using switch statement Problem :4 Write a program to check user entered number is prime or not.