SlideShare uma empresa Scribd logo
1 de 38
AFTER THIS LESSON, LEARNERS WILL BE ABLE
TO:
 Understand the concept and usage of selection and iteration
statements.
 Know various types of control structures available in C++.
 Analyze the problem, decide and evaluate conditions.
 To analyze and choose an appropriate combination of constructs to
solve a particular problem
 Write code that employ decision structures, including those that
employ sequences of decision and nested decisions.
 Design simple applications having iterative nature.
The order in which a set of statements are
executed in a program is known as its flow of
control
Generally the flow of control is sequential
However at times it is required to
change this sequential flow so that
only a particular statement or group
of statements are executed.
This is achieved control structures
.
Control structures are statements that upon execution alter
or control the sequence of execution of statements in a
program.
These can be categorized into the following categories:
Selection Control
Statements
• if
• if.....else
• switch.....case
Iterative Control
Structures
• for loop
• while loop
• do.....while loop
Jump Statements
• break
• continue
• exit
• go to
The if statement is used to execute an instruction or
a set of instructions only if a condition is fulfilled.
SYNTAX
if ( conditional expression)
Statement;
where :
• Conditional expression is any valid c++ expression having
Relational and/or logical operators. Eg (x>y), (x+y !=8)etc
• Statement is a valid c++ executable statement such as a cout
or calculation etc
There can be more than one executable statement
in an if. In such a case the statements have to be
enclosed in curly brackets .
if ( conditional expression)
{ Statement 1;
Statement 2;
.
.
}0
0
Program
• #include <iostream.h>
• void main()
• {int x,y;
• x=9, y=7;
• if(x>y)
• cout<<“Condition True”;}
Output : Condition
True
The if.. else statement executes the set of statement(s)following if, if the given
condition is true otherwise it executes an the set of statement(s) following the
else.
SYNTAX
If (condition expression)
Statement 1;
else
Statement 2;
 Only one statement either 1 or 2 will be executed in any case .
 Statement 1 will be executed if conditional expression evaluates to true
 Statement 2 will be executed if it evaluates to false.
Program
• #include <iostream.h>
• void main()
• {int x,y;
• cin>>x>>y;
• if(x>y)
• cout<<“Condition True”;
• else
• cout<<“Condition False”;
• }
Input: 15,8 Output :
Condition True
Input :15, 22 Output :
Condition False
#include <iostream.h>
void main()
{
int age;
cout<<“Enter the age of a person”;
cin>>age;
if (age>=18)
cout<<“nYou can vote”;
else
cout<<“n you cannot vote”;}
Enter the age of a person
20
You can vote
When we write an if within
an if or an if..else within an if
what we get are nested if or
if else statements. We can
have various nesting
constructs:
1. if (condtion)
if(condition)]
statement 1;
2. if (condition)
if (condition)
statement 1;
else
if (condition)
statement 2;
3. if (condition)
if(condition)
statement 1;
else
statement 2;
4.if (condition)
statement 1;
else
if (condition)
if(condition)
statement 2;
 There can be any level of nesting. But the rule is:
 Whenever a condition is encountered it is checked, if it
evaluates to true, the statement immediately following it is
executed otherwise the next statement is executed.
 If there is an else part, the statement in the else is executed.
. if (condtion)
if(condition)
statement 1;
A simple if within an if with no else part. Statement1
will be executed only if both ifs evaluate to true.
Example program segement1:
int x = 13, y = 6, z = 2;
if ( x > y) // outer if
if ( x > z) // inner if
cout << “ x is greater then y & z”;
The outer if evaluates to true. So the control goes to the next
statement. The if here also gives true, thus the cout is
executed and we get the output:
x is greater than y & z
. if (condition)
if (condition)
statement 1;
else
if (condition)
statement 2;
An if in the if part as well as in the else part of an outer
if
int x = 13, y = 16, z = 2;
if ( x > y)
if(x>z)
cout<<“nx is greatest “;
else
if ( y > z)
cout << “ny is greatest”;
y is greatest
output
//program to find largest of 3 numbers
#include<iostream.h>
void main()
{ int x,y,z;
cout<<“nenter 3 numbers:”<<endl;
cin>>x>>y>>z;
if(x>y)
if(x>z)
cout<<“n”<<x<<“is the largest”;
else
cout<<“n”<<z<<“ïs the largest”;
else
if(y>z)
cout<<“n”<<y<<“is the largest”;
else
cout<<“n”<<z<<“is the largest””;
We can have complex
structures like the if else if
where conditions are tested
sequentially and a course of
action takes place.
if (condition 1)
statement 1;
else
if (condition 2)
statement 2;
else
if (condition 3)
statement 3;
else
if (condition 4)
statement 4;
else
statement 5;
In a nested if..else statement
we start from the end and
match the first else we
encountered with the closest
preceding if that does not
already have an else.
The next else with the next
preceding if and so on. The
structure here illustrates this.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks;
cout << “ n please input the marks”;
cin >> marks;
if ( marks >= 90)
cout << “grade a ”;
else
if ( marks >= 75)
cout << “grade b ”;
else
if ( marks >= 65)
cout << “grade c”;
else
if ( marks >= 50)
cout << “grade d”;
else
if ( marks >= 40)
cout << “grade e ”;
else
cout << “grade f ”;
}
Program to display a grade
according to the marks input
by the user.
The switch statement is a
selection control structure that
is used to model branching of
code to be executed depending
on the value of the switch
expression.
The switch statement provides
with a no. of options out of
which only one is executed
depending on its value.
 First, the switch expression is evaluated.
 If the value matches any of the case labels the
control branches to the statement following the
matched case
 From here, control proceeds sequentially until
either a break statement or the end of the switch
statement is encountered.
 If the value of the switch does not match any of
the cases then either of the two happens.
 If there is a default label, the statement
following default is executed.
 If there is no default, control comes out of
switch and proceeds to the next statement after
the switch.
int x = 0;
switch( x )
{
case 0 : cout <<” Zero ”;
break;
case 8 : cout <<” Eight ”;
break;
case 9 : cout <<” Nine ”;
break;
default : cout <<” Wrong number ”;
}
The switch is evaluated on the
basis of the variable
x. Depending on its value a
particular case is executed.
Now we shall observe the
output for some values of x.
Explanation:
1. The variable in the switch
bracket ie, x has the value 0.
2. This value is matched with
each of the cases.
3. When a match is found ie,
case 0, the statement following
the colon is executed.
4. Next the break statement is
executed which causes the
control out of the switch. There
are no other comparisons
Explanation:
1. The variable in the bracket
ie, x has the value 9.
2. This value is matched with
each of the cases.
3. When a match is found ie,
case 9, the statement
following the colon is
executed.
4. Next the break statement is
executed which causes the
control out of the switch.
There are no other
comparisons
Explanation:
1. The variable in the bracket
ie, x has the value 15.
2. This value is matched with
each of the cases.
3. Since none of the case
value matches the default
case is executed.
4. The value Wrong Number
is displayed
5. If there is no default in the
above input case there
shall be no output
1. The case label cannot be an expression eg.
a+b or a-b etc.
2. The case label cannot be a string
expression eg. “Hello”, “good” etc.
3. Each case constant can appear only once in
a given switch statement
4. Only one default label can be there each
switch Statements
5. The label must be a constant of type
integer or character only. It can not be
floating point.
6. The default label is executed only if none of
the case labels match the expression in the
switch. Suppose in the above example the
value of x is 15, then the output would be
wrong number
case a-b: cout<<“hello”;1
case “Rita”: marks=marks+20;
case 2: cout<<“hello”;
break;
case 2 : cout<<”Welcome”;
break;
default : cout<<“wrong choice”;
default: cout<<“Try again”;
case 8.9: cout<<“hello”;
2
3
4
5
 If the break is omitted then
all of the statements
following the selected
branch (case) are also
executed.
 In the program given if the
break is omitted, we get the
output:
ZeroEightNineWrongnumber
int x = 0;
switch( x )
{
case 0 : cout <<” Zero ”;
case 8 : cout <<” Eight ”;
case 9 : cout <<” Nine ”;
default : cout <<” Wrong number ”;
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int day_o_w;
cout<<”Enter numerical day of the week:”<<”t”;
cin>>day_o_w;
switch( day_o_w )
{
case 1 : cout <<” Monday ”;
break;
case 2 : cout <<” Tuesday ”;
break;
case 3: cout <<” Wednesday ”;
break;
case 4 : cout <<” Thursday ”;
break;
case 5 : cout <<” Friday ”;
break;
case 6 : cout <<” Saturday ”;
break;
case 7 : cout <<” Sunday ”;
break;
default : cout <<” Sorry, not a day of the week ”;
}
}
Input value Output
2 Tuesday
6 Saurday
11 Sorry, not a day of the
week
5 Friday
EXPLANATION
1. In the above program, an integer value is input.
2. The value is stored in a variable day_o_w which is
placed in the switch expression.
3. Depending on the input the case labels are
compared.
4. Upon finding a match the case branch with the
matched value is executed.
5. For eg. if the input is 5, case 5 is executed and the
output will be Friday. After this the control goes out
of the switch and the program ends.
Program to display day of week in words
 In a switch statement only exact comparison of values can be
done,ie, only equality operator(==) can be implemented. We
cannot implement relational operators like >, <, >=, <= in a
switch statement.
 The case label in a switch statement must exactly match ie, be
equal to the switch expression, in order for the statement(s) to
be executed.
We can however implement the logical OR( || ) operator in a switch
statement. The following if is equivalent to the switch given below
iif ( letter == ‘a’ || letter == ‘A’)
cout << “Administration”;
else
if ( letter == ‘p’ || letter == ‘P’)
cout << “Purchase”;
else
if ( letter == ‘s’ || letter == ‘S’)
cout << “Sales”;
else
if ( letter == ‘w’ || letter == ‘W’)
cout << “.Warehousing”;
else
cout <<”letter input is incorrect, Enter again”;
switch ( letter )
{
case ‘a’ : // empty case
case ‘A’ : cout<<”Administration”;
break;
case ‘p’ : // empty case
case ‘P’ : cout<<”Purchase”;
break;
case ‘s’ : // empty case
case ‘S’ : cout<<”Sales”;
break;
case ‘w’ : // empty case
case ‘W’ : cout<<”Warehousing”;
break;
default : cout<< “Enter again”;}
Leaving a case
label statement
empty and
without a break
statement takes
the control to the
next case
Makes it fall
through to the
next case
Thus if letter is
‘a’ or ‘A’, in any
case the output
statement
cout<<”Administ
ration”;
After which the
break is
encountered
and control
comes out of the
switch.
Therefore, we can see that leaving a case empty can be used to implement the logic OR ( || )
in a switch statement
If statement
Can be used to check a
range of values using any
of the relational and logical
operators(>,<,>=,<=).
Variables of any type can
be used in the condition eg
float, int or char
Switch statement
Can be used th check only
for exact match using
equality operator(= =)
Only integer variables can
be used in the switch
bracket which determines
the case to be executed

Mais conteúdo relacionado

Mais procurados

Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationBadrul Alam
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 

Mais procurados (20)

Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
10. switch case
10. switch case10. switch case
10. switch case
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
C Programming
C ProgrammingC Programming
C Programming
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
File in C language
File in C languageFile in C language
File in C language
 

Semelhante a Introduction to Selection control structures in C++

Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
Lect3-C--EEB.pptx
Lect3-C--EEB.pptxLect3-C--EEB.pptx
Lect3-C--EEB.pptxKIJAMALEGI
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.pptTekle12
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingPathomchon Sriwilairit
 
C++ decision making
C++ decision makingC++ decision making
C++ decision makingZohaib Ahmed
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
03a control structures
03a   control structures03a   control structures
03a control structuresManzoor ALam
 
C statements
C statementsC statements
C statementsAhsann111
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilizationJAYDEV PATEL
 

Semelhante a Introduction to Selection control structures in C++ (20)

Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Lect3-C--EEB.pptx
Lect3-C--EEB.pptxLect3-C--EEB.pptx
Lect3-C--EEB.pptx
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
C statements
C statementsC statements
C statements
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
 

Mais de Neeru Mittal

Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptxNeeru Mittal
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in PythonNeeru Mittal
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python PandasNeeru Mittal
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and TricksNeeru Mittal
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV ConnectivityNeeru Mittal
 
Working of while loop
Working of while loopWorking of while loop
Working of while loopNeeru Mittal
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++Neeru Mittal
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programmingNeeru Mittal
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 

Mais de Neeru Mittal (17)

Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptx
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Nested loops
Nested loopsNested loops
Nested loops
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 

Último

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Introduction to Selection control structures in C++

  • 1.
  • 2. AFTER THIS LESSON, LEARNERS WILL BE ABLE TO:  Understand the concept and usage of selection and iteration statements.  Know various types of control structures available in C++.  Analyze the problem, decide and evaluate conditions.  To analyze and choose an appropriate combination of constructs to solve a particular problem  Write code that employ decision structures, including those that employ sequences of decision and nested decisions.  Design simple applications having iterative nature.
  • 3. The order in which a set of statements are executed in a program is known as its flow of control Generally the flow of control is sequential However at times it is required to change this sequential flow so that only a particular statement or group of statements are executed. This is achieved control structures .
  • 4. Control structures are statements that upon execution alter or control the sequence of execution of statements in a program.
  • 5. These can be categorized into the following categories: Selection Control Statements • if • if.....else • switch.....case Iterative Control Structures • for loop • while loop • do.....while loop Jump Statements • break • continue • exit • go to
  • 6. The if statement is used to execute an instruction or a set of instructions only if a condition is fulfilled. SYNTAX if ( conditional expression) Statement; where : • Conditional expression is any valid c++ expression having Relational and/or logical operators. Eg (x>y), (x+y !=8)etc • Statement is a valid c++ executable statement such as a cout or calculation etc
  • 7. There can be more than one executable statement in an if. In such a case the statements have to be enclosed in curly brackets . if ( conditional expression) { Statement 1; Statement 2; . . }0 0
  • 8.
  • 9. Program • #include <iostream.h> • void main() • {int x,y; • x=9, y=7; • if(x>y) • cout<<“Condition True”;} Output : Condition True
  • 10. The if.. else statement executes the set of statement(s)following if, if the given condition is true otherwise it executes an the set of statement(s) following the else. SYNTAX If (condition expression) Statement 1; else Statement 2;  Only one statement either 1 or 2 will be executed in any case .  Statement 1 will be executed if conditional expression evaluates to true  Statement 2 will be executed if it evaluates to false.
  • 11.
  • 12.
  • 13. Program • #include <iostream.h> • void main() • {int x,y; • cin>>x>>y; • if(x>y) • cout<<“Condition True”; • else • cout<<“Condition False”; • } Input: 15,8 Output : Condition True Input :15, 22 Output : Condition False
  • 14. #include <iostream.h> void main() { int age; cout<<“Enter the age of a person”; cin>>age; if (age>=18) cout<<“nYou can vote”; else cout<<“n you cannot vote”;} Enter the age of a person 20 You can vote
  • 15. When we write an if within an if or an if..else within an if what we get are nested if or if else statements. We can have various nesting constructs: 1. if (condtion) if(condition)] statement 1; 2. if (condition) if (condition) statement 1; else if (condition) statement 2; 3. if (condition) if(condition) statement 1; else statement 2; 4.if (condition) statement 1; else if (condition) if(condition) statement 2;
  • 16.  There can be any level of nesting. But the rule is:  Whenever a condition is encountered it is checked, if it evaluates to true, the statement immediately following it is executed otherwise the next statement is executed.  If there is an else part, the statement in the else is executed.
  • 17. . if (condtion) if(condition) statement 1; A simple if within an if with no else part. Statement1 will be executed only if both ifs evaluate to true.
  • 18. Example program segement1: int x = 13, y = 6, z = 2; if ( x > y) // outer if if ( x > z) // inner if cout << “ x is greater then y & z”; The outer if evaluates to true. So the control goes to the next statement. The if here also gives true, thus the cout is executed and we get the output: x is greater than y & z
  • 19. . if (condition) if (condition) statement 1; else if (condition) statement 2; An if in the if part as well as in the else part of an outer if
  • 20. int x = 13, y = 16, z = 2; if ( x > y) if(x>z) cout<<“nx is greatest “; else if ( y > z) cout << “ny is greatest”; y is greatest output
  • 21.
  • 22. //program to find largest of 3 numbers #include<iostream.h> void main() { int x,y,z; cout<<“nenter 3 numbers:”<<endl; cin>>x>>y>>z; if(x>y) if(x>z) cout<<“n”<<x<<“is the largest”; else cout<<“n”<<z<<“ïs the largest”; else if(y>z) cout<<“n”<<y<<“is the largest”; else cout<<“n”<<z<<“is the largest””;
  • 23. We can have complex structures like the if else if where conditions are tested sequentially and a course of action takes place. if (condition 1) statement 1; else if (condition 2) statement 2; else if (condition 3) statement 3; else if (condition 4) statement 4; else statement 5;
  • 24. In a nested if..else statement we start from the end and match the first else we encountered with the closest preceding if that does not already have an else. The next else with the next preceding if and so on. The structure here illustrates this.
  • 25. #include<iostream.h> #include<conio.h> void main() { clrscr(); int marks; cout << “ n please input the marks”; cin >> marks; if ( marks >= 90) cout << “grade a ”; else if ( marks >= 75) cout << “grade b ”; else if ( marks >= 65) cout << “grade c”; else if ( marks >= 50) cout << “grade d”; else if ( marks >= 40) cout << “grade e ”; else cout << “grade f ”; } Program to display a grade according to the marks input by the user.
  • 26. The switch statement is a selection control structure that is used to model branching of code to be executed depending on the value of the switch expression. The switch statement provides with a no. of options out of which only one is executed depending on its value.
  • 27.  First, the switch expression is evaluated.  If the value matches any of the case labels the control branches to the statement following the matched case  From here, control proceeds sequentially until either a break statement or the end of the switch statement is encountered.  If the value of the switch does not match any of the cases then either of the two happens.  If there is a default label, the statement following default is executed.  If there is no default, control comes out of switch and proceeds to the next statement after the switch.
  • 28. int x = 0; switch( x ) { case 0 : cout <<” Zero ”; break; case 8 : cout <<” Eight ”; break; case 9 : cout <<” Nine ”; break; default : cout <<” Wrong number ”; } The switch is evaluated on the basis of the variable x. Depending on its value a particular case is executed. Now we shall observe the output for some values of x.
  • 29. Explanation: 1. The variable in the switch bracket ie, x has the value 0. 2. This value is matched with each of the cases. 3. When a match is found ie, case 0, the statement following the colon is executed. 4. Next the break statement is executed which causes the control out of the switch. There are no other comparisons
  • 30. Explanation: 1. The variable in the bracket ie, x has the value 9. 2. This value is matched with each of the cases. 3. When a match is found ie, case 9, the statement following the colon is executed. 4. Next the break statement is executed which causes the control out of the switch. There are no other comparisons
  • 31. Explanation: 1. The variable in the bracket ie, x has the value 15. 2. This value is matched with each of the cases. 3. Since none of the case value matches the default case is executed. 4. The value Wrong Number is displayed 5. If there is no default in the above input case there shall be no output
  • 32. 1. The case label cannot be an expression eg. a+b or a-b etc. 2. The case label cannot be a string expression eg. “Hello”, “good” etc. 3. Each case constant can appear only once in a given switch statement 4. Only one default label can be there each switch Statements 5. The label must be a constant of type integer or character only. It can not be floating point. 6. The default label is executed only if none of the case labels match the expression in the switch. Suppose in the above example the value of x is 15, then the output would be wrong number case a-b: cout<<“hello”;1 case “Rita”: marks=marks+20; case 2: cout<<“hello”; break; case 2 : cout<<”Welcome”; break; default : cout<<“wrong choice”; default: cout<<“Try again”; case 8.9: cout<<“hello”; 2 3 4 5
  • 33.  If the break is omitted then all of the statements following the selected branch (case) are also executed.  In the program given if the break is omitted, we get the output: ZeroEightNineWrongnumber int x = 0; switch( x ) { case 0 : cout <<” Zero ”; case 8 : cout <<” Eight ”; case 9 : cout <<” Nine ”; default : cout <<” Wrong number ”; }
  • 34. #include<iostream.h> #include<conio.h> void main() { clrscr(); int day_o_w; cout<<”Enter numerical day of the week:”<<”t”; cin>>day_o_w; switch( day_o_w ) { case 1 : cout <<” Monday ”; break; case 2 : cout <<” Tuesday ”; break; case 3: cout <<” Wednesday ”; break; case 4 : cout <<” Thursday ”; break; case 5 : cout <<” Friday ”; break; case 6 : cout <<” Saturday ”; break; case 7 : cout <<” Sunday ”; break; default : cout <<” Sorry, not a day of the week ”; } } Input value Output 2 Tuesday 6 Saurday 11 Sorry, not a day of the week 5 Friday EXPLANATION 1. In the above program, an integer value is input. 2. The value is stored in a variable day_o_w which is placed in the switch expression. 3. Depending on the input the case labels are compared. 4. Upon finding a match the case branch with the matched value is executed. 5. For eg. if the input is 5, case 5 is executed and the output will be Friday. After this the control goes out of the switch and the program ends. Program to display day of week in words
  • 35.  In a switch statement only exact comparison of values can be done,ie, only equality operator(==) can be implemented. We cannot implement relational operators like >, <, >=, <= in a switch statement.  The case label in a switch statement must exactly match ie, be equal to the switch expression, in order for the statement(s) to be executed.
  • 36. We can however implement the logical OR( || ) operator in a switch statement. The following if is equivalent to the switch given below iif ( letter == ‘a’ || letter == ‘A’) cout << “Administration”; else if ( letter == ‘p’ || letter == ‘P’) cout << “Purchase”; else if ( letter == ‘s’ || letter == ‘S’) cout << “Sales”; else if ( letter == ‘w’ || letter == ‘W’) cout << “.Warehousing”; else cout <<”letter input is incorrect, Enter again”; switch ( letter ) { case ‘a’ : // empty case case ‘A’ : cout<<”Administration”; break; case ‘p’ : // empty case case ‘P’ : cout<<”Purchase”; break; case ‘s’ : // empty case case ‘S’ : cout<<”Sales”; break; case ‘w’ : // empty case case ‘W’ : cout<<”Warehousing”; break; default : cout<< “Enter again”;}
  • 37. Leaving a case label statement empty and without a break statement takes the control to the next case Makes it fall through to the next case Thus if letter is ‘a’ or ‘A’, in any case the output statement cout<<”Administ ration”; After which the break is encountered and control comes out of the switch. Therefore, we can see that leaving a case empty can be used to implement the logic OR ( || ) in a switch statement
  • 38. If statement Can be used to check a range of values using any of the relational and logical operators(>,<,>=,<=). Variables of any type can be used in the condition eg float, int or char Switch statement Can be used th check only for exact match using equality operator(= =) Only integer variables can be used in the switch bracket which determines the case to be executed