SlideShare une entreprise Scribd logo
1  sur  38
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

Contenu connexe

Tendances

java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case StatementsDipesh Pandey
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 

Tendances (20)

Control statements
Control statementsControl statements
Control statements
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Strings
StringsStrings
Strings
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
The Loops
The LoopsThe Loops
The Loops
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
10. switch case
10. switch case10. switch case
10. switch case
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 

En vedette

Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programAAKASH KUMAR
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selectionOnline
 
Control structures i
Control structures i Control structures i
Control structures i Ahmad Idrees
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)akmalfahmi
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
Jedi course notes intro to programming 1
Jedi course notes intro to programming 1Jedi course notes intro to programming 1
Jedi course notes intro to programming 1aehj02
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments rajni kaushal
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Abdullah khawar
 

En vedette (20)

Control Structures
Control StructuresControl Structures
Control Structures
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control structures i
Control structures i Control structures i
Control structures i
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Starting c++
Starting c++Starting c++
Starting c++
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Jedi course notes intro to programming 1
Jedi course notes intro to programming 1Jedi course notes intro to programming 1
Jedi course notes intro to programming 1
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pascal programming language
Pascal programming languagePascal programming language
Pascal programming language
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 

Similaire à Control structure C++

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
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
 
control statements
control statementscontrol statements
control statementsAzeem Sultan
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control StatementsSiddharthSelenium
 
Flow of control c++
Flow of control c++Flow of control c++
Flow of control c++Arpit Meena
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop typesRj Baculo
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statementsRai University
 

Similaire à Control structure C++ (20)

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
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
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
control structure
control structurecontrol structure
control structure
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
control statements
control statementscontrol statements
control statements
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Control statement
Control statementControl statement
Control statement
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control Statements
 
Flow of control c++
Flow of control c++Flow of control c++
Flow of control c++
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Flow Control (C#)
Flow Control (C#)Flow Control (C#)
Flow Control (C#)
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 

Dernier

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Dernier (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

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