SlideShare a Scribd company logo
Control Structures
ANIL KUMAR
https://www.facebook.com/AniLK0221
https://www.facebook.com/AniLK0221
Control Structures
• Programs are written using three basic structures
– Sequence
• a sequence is a series of statements that execute one
after another
– Repetition(loop or iteration)
• repetition (looping) is used to repeat statements while
certain conditions are met.
– Selection(branching)
• selection (branch) is used to execute different
statements depending on certain conditions
• Called control structures or logic structures
https://www.facebook.com/AniLK0221
Sequence
• The sequence structure
directs the computer to
process the program
instructions, one after
another, in the order
listed in the program
https://www.facebook.com/AniLK0221
Selection
(Branch)
• Selection structure:
makes a decision
and then takes an
appropriate action
based on that
decision
– Also called the
decision structure
https://www.facebook.com/AniLK0221
Repetition (Loop)
• Repetition structure:
directs computer to
repeat one or more
instructions until some
condition is met
– Also called a loop or
iteration
https://www.facebook.com/AniLK0221
Flow Control
• How the computer moves through the
program
• Many keywords are for "flow control"
int, float, double, char,
break, case, continue, default, do, else, for,
goto, if, return, switch, while
https://www.facebook.com/AniLK0221
Normal flow
Statement 1;
Statement 2;
Statement 3;
Statement 4;
https://www.facebook.com/AniLK0221
Flow control
 Selection (Branch)
 if
 if-else
 nested if
 (goto)
 switch
 Repetition (Loop)
 while
 do-while
 For
 nesting
https://www.facebook.com/AniLK0221
Conditional Statements
• if
• if else
• nested if (if – else if – else if – else)
• statement blocks ({…})
• (goto)
• switch (case, default, break)
https://www.facebook.com/AniLK0221
if
• if(condition)statement
if (x==100)
cout<<“x is 100”;
 If the condition is true, statement is
executed.
 If the condition is false, statement is not
executed.
https://www.facebook.com/AniLK0221
if flow
Condititional Statementif(…)
true
false
https://www.facebook.com/AniLK0221
if else
 if (condition)statement1
else statement2
if (x==100)
cout<<“x is 100”;
else
cout<<“x is not 100”;
 If the statement1 is true, then print out on the screen x is
100
 If the statement2 is true, then print out on the screen x is
not 100
https://www.facebook.com/AniLK0221
if else flow
Condititional Statementif(…)
else Statement
true
false
https://www.facebook.com/AniLK0221
Statement Blocks
 If we want more than a single instruction is executed, we must
group them in a in a block of statements by using curly brackets
({…})
if (x>0)
{
cout<<“x is positive”;
}
else
{
cout<<“x is negative”;
}
https://www.facebook.com/AniLK0221
Nested if statements
 When if statement occurs with in another if statement, then such type of if statement is called nested if statement.
 if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
https://www.facebook.com/AniLK0221
Nested if statements
 In this program the
statement if(a>c) is nested
with in the if(a>b).
 if (a>b) is true only then
the second if statement
if(a>c) is executed.
 If the first if condition is
false then program control
shifts to the statement
after corresponding else
statement.
if(a>=b && a>=c)
cout<<“a is
biggest”;
elseif(b>=a &&
b>=c)
cout<<“b is
biggest”;
Else
cout<<“c is
biggest”;
https://www.facebook.com/AniLK0221
Nested if flow
Condititional Statement 2else if
else if Condititional Statement 3
Else Statement
Condititional Statement 1if true
true
true
false
false
false
https://www.facebook.com/AniLK0221
goto
• It allows making an absolute jump to another point in the
program.
• "Go to" part of a program
#include<iostream.h>
int main()
{
int n=10;
loop:
cout<<n<<“,”;
n--;
if(n>0) goto loop;
cout << “FIRE!“;
return 0;
}
https://www.facebook.com/AniLK0221
Switch Statement
• Like the goto statement but more structured
– Structure is good - less confusing
– Its objective is to check several possible constant
values for an expression and Similar to if-elseif-
elseif-else but a little simpler.
– So the switch statement is better than goto !
https://www.facebook.com/AniLK0221
Switch flow
Condititional Statement 2case 2
case 3 Condititional Statement 3
Condititional Statement 1case 1
switch
https://www.facebook.com/AniLK0221
Switch statement
switch(expression) {
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
default:
default block of instructions
}
https://www.facebook.com/AniLK0221
Switch statement
 Switch evaluates expression and checks if it is equivalent to
constant1, if it is, it executes block of instructions 1 until it
finds the break keyword, then the program will jump to the
end of the switch selective structure.
 If expression was not equivalent to constant1, it will check if
expression is equivalent to constant2. if it is, it executes block
of instructions 2 until it finds the break keyword.
 Finally if the value of expression has not matched any of the
specified constants, the program will execute the instructions
included in the default: section, if this one exists, since it is
optional.
https://www.facebook.com/AniLK0221
Switch statement
switch(x) {
case 1:
cout<<“x is 1”;
break;
case 2:
cout<<“x is 2”;
break;
default:
cout<<“value of x is unknown”;
}
https://www.facebook.com/AniLK0221
Loops and iterations
• Loops have as objective to repeat a certain number of
times or while a condition is fulfilled. When a single
statement or a group of statements will be executed
again and again in the program then such type of
processing is called loop. Loop is divided into two parts
Body of loop
Control of loop
https://www.facebook.com/AniLK0221
Loops and iterations
 Control of loop is divided into two parts:
 Entry control loop- in this first of all condition is checked
if it is true then body of the loop is executed. Otherwise
we can exit from the loop when the condition becomes
false. Entry control loop is also called base loop.
Example- While loop and for loop
 Exit control loop- in this first body is executed and then
condition is checked. If condition is true, then again body
of loop is executed. If condition is false, then control will
move out from the loop. Exit control loop is also called
Derived loop. Example- Do-while
https://www.facebook.com/AniLK0221
Loops and iterations
• The loops in statements in C++ language are-
While loop
Do loop/Do-while loop
For loop
Nested for loop
https://www.facebook.com/AniLK0221
While Loop
 while (expression) statement
 While loop is an Entry control loop
 And its function is simply to repeat statement while
expression is true.
Condititional Statementswhile(…)
true
false
https://www.facebook.com/AniLK0221
While loop
#include<iostream.h>
int main()
{
int n;
cout<<“Enter the starting number”;
cin>>n;
while(n>0){
cout<<n<<“,”;
n--;
}
cout << “FIRE!“;
return 0;
} https://www.facebook.com/AniLK0221
For loop
• For loop is an Entry control loop when action is to be
repeated for a predetermined number of times.
• Most used – most complicated
• Normally used for counting
• Four parts
– Initialise expression
– Test expression
– Body
– Increment expression
for(initialization;condition;increment)
https://www.facebook.com/AniLK0221
for loop
body
statements
continuation
test
initialisation
increment
true
false
https://www.facebook.com/AniLK0221
For loop
int main()
{
int count;
for (count=1; count<=10; count++)
{
cout <<count<<“,”;
}
cout<<“FIRE”;
return 0;
} https://www.facebook.com/AniLK0221
Do-While Loop
• do statement while (expression)
• Do-while is an exit control loop. Based on a condition,
the control is transferred back to a particular point in the
program.
– Similar to the while loop except that condition in
the do while is check is at end of loop not the start
do {
action1;
}
while (condition is true);
action2;
https://www.facebook.com/AniLK0221
Do Flow
condititional statements
while(…)
true
false
do
https://www.facebook.com/AniLK0221
Nesting of Loops
• A loop can be inside another loop. C++ can
have at least 256 levels of nesting.
for(init;condition;increment)
{
for(init;condition;increment)
}
statement(s);
}
statement(s);
} https://www.facebook.com/AniLK0221
Break Statement
 Goes straight to the end of a do, while or for
loop or a switch statement block,
#include<iostream.h>
int main(){
int n;
for(n=10;n>0;n--){
cout<<n<<“,”;
if(n==5){
cout<<“count down aborted!”;
break;}}
return 0;}
O/P: 10,9,8,7,6,5,count down
aborted!
https://www.facebook.com/AniLK0221
Continue Statement
 Goes straight back to the start of a do, while or
for loop,
#include<iostream.h>
int main()
{
int n;
for(n=10;n>0;n--){
if(n==5)continue;
cout<<n<<“,”;}
cout << “FIRE!“;
return 0;}
O/P: 10,9,8,7,6,4,3,2,1,FIRE!
https://www.facebook.com/AniLK0221
Summary
 Programs: step-by-step instructions that tell a
computer how to perform a task
 Programmers use programming languages to
communicate with the computer
 First programming languages were machine languages
 High-level languages can be used to create procedure-
oriented programs or object-oriented programs
 Algorithm: step-by-step instructions that accomplish a
task (not written in a programming language)
 Algorithms contain one or more of the following control
structures: sequence, selection, and repetition
https://www.facebook.com/AniLK0221
Summary (continued)
• Sequence structure: process the instructions,
one after another, in the order listed
• Repetition structure: repeat one or more
instructions until some condition is met
• Selection structure: directs the computer to
make a decision, and then to select an
appropriate action based on that decision
https://www.facebook.com/AniLK0221

More Related Content

What's hot (20)

PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
While , For , Do-While Loop
Abhishek Choksi
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Control structures in c++
Nitin Jawla
 
PPTX
Loops c++
Shivani Singh
 
PPTX
Conditional statement c++
amber chaudary
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
python conditional statement.pptx
Dolchandra
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
Control Statement programming
University of Potsdam
 
PPTX
Inline function
Tech_MX
 
PPTX
classes and objects in C++
HalaiHansaika
 
PDF
Managing I/O in c++
Pranali Chaudhari
 
PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Exception handling c++
Jayant Dalvi
 
While , For , Do-While Loop
Abhishek Choksi
 
Function in C program
Nurul Zakiah Zamri Tan
 
Control structures in c++
Nitin Jawla
 
Loops c++
Shivani Singh
 
Conditional statement c++
amber chaudary
 
FUNCTIONS IN c++ PPT
03062679929
 
Data types in c++
Venkata.Manish Reddy
 
Storage class in C Language
Nitesh Kumar Pandey
 
Looping statement in python
RaginiJain21
 
python conditional statement.pptx
Dolchandra
 
Pointers in c++
sai tarlekar
 
Control Statement programming
University of Potsdam
 
Inline function
Tech_MX
 
classes and objects in C++
HalaiHansaika
 
Managing I/O in c++
Pranali Chaudhari
 
Control structures in C++ Programming Language
Ahmad Idrees
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 

Viewers also liked (20)

PPT
Control Structures
Ghaffar Khan
 
PPTX
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
PDF
Control statements
Kanwalpreet Kaur
 
PDF
Object-Oriented Programming 3
Warawut
 
PPT
Control structures selection
Online
 
PPT
Control structures i
Ahmad Idrees
 
PPT
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
PPTX
Pointers in c++
Vineeta Garg
 
PPTX
Control statements in Java
Jin Castor
 
PDF
C++ control structure
bluejayjunior
 
PPTX
Starting c++
Ameer Khan
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PDF
CP Handout#2
trupti1976
 
PPT
8.3 program structure (1 hour)
akmalfahmi
 
PDF
CP Handout#5
trupti1976
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PDF
Jedi course notes intro to programming 1
aehj02
 
DOC
Java programming lab assignments
rajni kaushal
 
PDF
Pascal programming language
Verónica Meo Laos
 
PPTX
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
Control Structures
Ghaffar Khan
 
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
Control statements
Kanwalpreet Kaur
 
Object-Oriented Programming 3
Warawut
 
Control structures selection
Online
 
Control structures i
Ahmad Idrees
 
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Pointers in c++
Vineeta Garg
 
Control statements in Java
Jin Castor
 
C++ control structure
bluejayjunior
 
Starting c++
Ameer Khan
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
CP Handout#2
trupti1976
 
8.3 program structure (1 hour)
akmalfahmi
 
CP Handout#5
trupti1976
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Jedi course notes intro to programming 1
aehj02
 
Java programming lab assignments
rajni kaushal
 
Pascal programming language
Verónica Meo Laos
 
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
Ad

Similar to Control structure C++ (20)

PPTX
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
PPT
C++ chapter 4
SHRIRANG PINJARKAR
 
PPTX
Control structures
ElakkiyaS11
 
PPTX
Chapter 3
enidegmossu
 
PPTX
Basic C concepts.
Farooq Mian
 
PPTX
5.pptx fundamental programing one branch
ssuserdde43b
 
PPTX
C++ lecture 02
HNDE Labuduwa Galle
 
PDF
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
surafiraol103
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PDF
Fundamentals of Computer Programming Summary of Flow Controls
ChereLemma2
 
PPTX
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
PPT
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
PPTX
Flow of control
Shobhit Singhal
 
PPT
03 conditions loops
Manzoor ALam
 
PPTX
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
PPT
control-statements....ppt - definition
Papitha7
 
PPTX
Bsc cs pic u-3 handling input output and control statements
Rai University
 
PPTX
handling input output and control statements
Rai University
 
PPTX
Btech i pic u-3 handling input output and control statements
Rai University
 
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
C++ chapter 4
SHRIRANG PINJARKAR
 
Control structures
ElakkiyaS11
 
Chapter 3
enidegmossu
 
Basic C concepts.
Farooq Mian
 
5.pptx fundamental programing one branch
ssuserdde43b
 
C++ lecture 02
HNDE Labuduwa Galle
 
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
surafiraol103
 
control-statements, control-statements, control statement
crrpavankumar
 
Fundamentals of Computer Programming Summary of Flow Controls
ChereLemma2
 
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
Flow of control
Shobhit Singhal
 
03 conditions loops
Manzoor ALam
 
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
control-statements....ppt - definition
Papitha7
 
Bsc cs pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Rai University
 
Ad

Recently uploaded (20)

PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Controller Request and Response in Odoo18
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Introduction presentation of the patentbutler tool
MIPLM
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 

Control structure C++

  • 2. Control Structures • Programs are written using three basic structures – Sequence • a sequence is a series of statements that execute one after another – Repetition(loop or iteration) • repetition (looping) is used to repeat statements while certain conditions are met. – Selection(branching) • selection (branch) is used to execute different statements depending on certain conditions • Called control structures or logic structures https://www.facebook.com/AniLK0221
  • 3. Sequence • The sequence structure directs the computer to process the program instructions, one after another, in the order listed in the program https://www.facebook.com/AniLK0221
  • 4. Selection (Branch) • Selection structure: makes a decision and then takes an appropriate action based on that decision – Also called the decision structure https://www.facebook.com/AniLK0221
  • 5. Repetition (Loop) • Repetition structure: directs computer to repeat one or more instructions until some condition is met – Also called a loop or iteration https://www.facebook.com/AniLK0221
  • 6. Flow Control • How the computer moves through the program • Many keywords are for "flow control" int, float, double, char, break, case, continue, default, do, else, for, goto, if, return, switch, while https://www.facebook.com/AniLK0221
  • 7. Normal flow Statement 1; Statement 2; Statement 3; Statement 4; https://www.facebook.com/AniLK0221
  • 8. Flow control  Selection (Branch)  if  if-else  nested if  (goto)  switch  Repetition (Loop)  while  do-while  For  nesting https://www.facebook.com/AniLK0221
  • 9. Conditional Statements • if • if else • nested if (if – else if – else if – else) • statement blocks ({…}) • (goto) • switch (case, default, break) https://www.facebook.com/AniLK0221
  • 10. if • if(condition)statement if (x==100) cout<<“x is 100”;  If the condition is true, statement is executed.  If the condition is false, statement is not executed. https://www.facebook.com/AniLK0221
  • 12. if else  if (condition)statement1 else statement2 if (x==100) cout<<“x is 100”; else cout<<“x is not 100”;  If the statement1 is true, then print out on the screen x is 100  If the statement2 is true, then print out on the screen x is not 100 https://www.facebook.com/AniLK0221
  • 13. if else flow Condititional Statementif(…) else Statement true false https://www.facebook.com/AniLK0221
  • 14. Statement Blocks  If we want more than a single instruction is executed, we must group them in a in a block of statements by using curly brackets ({…}) if (x>0) { cout<<“x is positive”; } else { cout<<“x is negative”; } https://www.facebook.com/AniLK0221
  • 15. Nested if statements  When if statement occurs with in another if statement, then such type of if statement is called nested if statement.  if (condition1) if (condition2) statement-1; else statement-2; else statement-3; https://www.facebook.com/AniLK0221
  • 16. Nested if statements  In this program the statement if(a>c) is nested with in the if(a>b).  if (a>b) is true only then the second if statement if(a>c) is executed.  If the first if condition is false then program control shifts to the statement after corresponding else statement. if(a>=b && a>=c) cout<<“a is biggest”; elseif(b>=a && b>=c) cout<<“b is biggest”; Else cout<<“c is biggest”; https://www.facebook.com/AniLK0221
  • 17. Nested if flow Condititional Statement 2else if else if Condititional Statement 3 Else Statement Condititional Statement 1if true true true false false false https://www.facebook.com/AniLK0221
  • 18. goto • It allows making an absolute jump to another point in the program. • "Go to" part of a program #include<iostream.h> int main() { int n=10; loop: cout<<n<<“,”; n--; if(n>0) goto loop; cout << “FIRE!“; return 0; } https://www.facebook.com/AniLK0221
  • 19. Switch Statement • Like the goto statement but more structured – Structure is good - less confusing – Its objective is to check several possible constant values for an expression and Similar to if-elseif- elseif-else but a little simpler. – So the switch statement is better than goto ! https://www.facebook.com/AniLK0221
  • 20. Switch flow Condititional Statement 2case 2 case 3 Condititional Statement 3 Condititional Statement 1case 1 switch https://www.facebook.com/AniLK0221
  • 21. Switch statement switch(expression) { case constant1: block of instructions 1 break; case constant2: block of instructions 2 break; default: default block of instructions } https://www.facebook.com/AniLK0221
  • 22. Switch statement  Switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes block of instructions 1 until it finds the break keyword, then the program will jump to the end of the switch selective structure.  If expression was not equivalent to constant1, it will check if expression is equivalent to constant2. if it is, it executes block of instructions 2 until it finds the break keyword.  Finally if the value of expression has not matched any of the specified constants, the program will execute the instructions included in the default: section, if this one exists, since it is optional. https://www.facebook.com/AniLK0221
  • 23. Switch statement switch(x) { case 1: cout<<“x is 1”; break; case 2: cout<<“x is 2”; break; default: cout<<“value of x is unknown”; } https://www.facebook.com/AniLK0221
  • 24. Loops and iterations • Loops have as objective to repeat a certain number of times or while a condition is fulfilled. When a single statement or a group of statements will be executed again and again in the program then such type of processing is called loop. Loop is divided into two parts Body of loop Control of loop https://www.facebook.com/AniLK0221
  • 25. Loops and iterations  Control of loop is divided into two parts:  Entry control loop- in this first of all condition is checked if it is true then body of the loop is executed. Otherwise we can exit from the loop when the condition becomes false. Entry control loop is also called base loop. Example- While loop and for loop  Exit control loop- in this first body is executed and then condition is checked. If condition is true, then again body of loop is executed. If condition is false, then control will move out from the loop. Exit control loop is also called Derived loop. Example- Do-while https://www.facebook.com/AniLK0221
  • 26. Loops and iterations • The loops in statements in C++ language are- While loop Do loop/Do-while loop For loop Nested for loop https://www.facebook.com/AniLK0221
  • 27. While Loop  while (expression) statement  While loop is an Entry control loop  And its function is simply to repeat statement while expression is true. Condititional Statementswhile(…) true false https://www.facebook.com/AniLK0221
  • 28. While loop #include<iostream.h> int main() { int n; cout<<“Enter the starting number”; cin>>n; while(n>0){ cout<<n<<“,”; n--; } cout << “FIRE!“; return 0; } https://www.facebook.com/AniLK0221
  • 29. For loop • For loop is an Entry control loop when action is to be repeated for a predetermined number of times. • Most used – most complicated • Normally used for counting • Four parts – Initialise expression – Test expression – Body – Increment expression for(initialization;condition;increment) https://www.facebook.com/AniLK0221
  • 31. For loop int main() { int count; for (count=1; count<=10; count++) { cout <<count<<“,”; } cout<<“FIRE”; return 0; } https://www.facebook.com/AniLK0221
  • 32. Do-While Loop • do statement while (expression) • Do-while is an exit control loop. Based on a condition, the control is transferred back to a particular point in the program. – Similar to the while loop except that condition in the do while is check is at end of loop not the start do { action1; } while (condition is true); action2; https://www.facebook.com/AniLK0221
  • 34. Nesting of Loops • A loop can be inside another loop. C++ can have at least 256 levels of nesting. for(init;condition;increment) { for(init;condition;increment) } statement(s); } statement(s); } https://www.facebook.com/AniLK0221
  • 35. Break Statement  Goes straight to the end of a do, while or for loop or a switch statement block, #include<iostream.h> int main(){ int n; for(n=10;n>0;n--){ cout<<n<<“,”; if(n==5){ cout<<“count down aborted!”; break;}} return 0;} O/P: 10,9,8,7,6,5,count down aborted! https://www.facebook.com/AniLK0221
  • 36. Continue Statement  Goes straight back to the start of a do, while or for loop, #include<iostream.h> int main() { int n; for(n=10;n>0;n--){ if(n==5)continue; cout<<n<<“,”;} cout << “FIRE!“; return 0;} O/P: 10,9,8,7,6,4,3,2,1,FIRE! https://www.facebook.com/AniLK0221
  • 37. Summary  Programs: step-by-step instructions that tell a computer how to perform a task  Programmers use programming languages to communicate with the computer  First programming languages were machine languages  High-level languages can be used to create procedure- oriented programs or object-oriented programs  Algorithm: step-by-step instructions that accomplish a task (not written in a programming language)  Algorithms contain one or more of the following control structures: sequence, selection, and repetition https://www.facebook.com/AniLK0221
  • 38. Summary (continued) • Sequence structure: process the instructions, one after another, in the order listed • Repetition structure: repeat one or more instructions until some condition is met • Selection structure: directs the computer to make a decision, and then to select an appropriate action based on that decision https://www.facebook.com/AniLK0221