SlideShare a Scribd company logo
DECISION
STRUCTURES
THE IF-ELSE IF STATEMENT AND NESTED IF STATEMENTS IN C++
Presentators:
M. Tahir Bashir
Fahad Iftikhar
Bilal Ahmad
C++
@UMT 8-Apr-2016
CONDITIONAL STATEMENTS
• The C++ conditional statements are the:
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• if statement
• if-else statement
• switch statement
• A conditional statement lets us choose which statement will be executed next
FLOW OF CONTROL
• Unless specified otherwise, the order of statement execution through a function is
linear: one statement after another in sequence
• Some programming statements allow us to:
• decide whether or not to execute a particular statement
• execute a statement over and over, repetitively
• These decisions are based on Boolean expressions (or conditions) that evaluate to
true or false
• The order of statement execution is called the flow of control
THE IF STATEMENT
• The if statement has the following syntax:
if ( condition )
statement;
if is a C++
reserved word
The condition must be a
Boolean expression. It must
evaluate to either true or false.
If the condition is true, the statement is executed.
If it is false, the statement is skipped.
LOGIC OF AN IF STATEMENT
condition
evaluated
statement
true
false
THE IF STATEMENT
• An example of an if statement:
if (FirstNo > SecondNo)
result = FirstNo - SecondNo;
cout<<"The resultant is"<< result;
• First the condition is evaluated -- the value of FirstNo is either greater than the
value of SecondNo, or it is not
• If the condition is true, the assignment statement is executed -- if it isn’t, it is
skipped.
• Either way, the call to cout is executed next
RELATIONAL OPERATORS
• A condition often uses one of C++'s equality operators or relational operators
== equal to
< less than
!= not equal to
• Note the difference between the equality operator (==) and the assignment
operator (=)
> greater than
<= less than or equal to
>= greater than or equal to
LOGICAL OPERATORS
• C++ provides logical operators.
• The binary logical operators combine two Boolean expressions into one.
• The unary logical operator switches the value of a Boolean expression.
• Binary logical operators have lower precedence than relational operators
• NOT has the same precedence as negation.
Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary
LOGICAL NOT
• The logical NOT operation is also called logical negation or logical complement
• If some condition a is true, then !a is false; if a is false, then !a is true
• Logical expressions can be shown using a truth table
a !a
True False
False True
LOGICAL AND & LOGICAL OR
• The logical AND expression
a && b
is true if both a and b are true, and false otherwise
• The logical OR expression
a || b
is true if a or b or both are true, and false otherwise
IF-ELSE STATEMENT:
General form of an if-else statement:
if(BooleanExpression)
statement or block 1
else
statement or block 2
LOGIC OF AN IF-ELSE STATEMENT
condition
evaluated
statement1
true false
statement2
THE CONDITIONAL OPERATOR
• C++ provides and operator to create short expressions that work like if-else
statements.
BooleanExpression ? Value1 : Value2;
• If BooleanExpression is true, Value1 is returned
• If BooleanExpression is false, Value2 is returned
• Example:
if (score < 50)
cout<<“Sorry! You Have Failed…";
else
cout<<"You Have Successfully Passed! ";
THE IF-ELSE IF STATEMENT
• Sometimes you need to be able to test a series of conditions
• You can do this with the if-else if statement
• General form:
if (BooleanExpression1)
statement or block 1
else if (BooleanExpression2)
statement or block 2
else
statement or block 3
• If BooleanExpression1 is true, then statement or block 1 is executed.
• If BooleanExpression1 is false, then BooleanExpression2 is tested.
• If BooleanExpression2 is true, then statement or block 2 ais executed.
• If BooleanExpression2 is false, then statement or block 3 is executed.
Note: You can have as many if else clauses as is
needed.
NESTED IF STATEMENTS
• Nesting is enclosing one structure inside of another.
• A block in C++ can contain any valid C++ code, this includes other if statements:
if(BooleanExpression1) {
if(BooleanExpression2) {
statement1;
statement2;
}
statement3;
statement4;
}
• If BooleanExpression1 is true and BooleanExpression2 is true , what is executed?
• statement1 , statement2 , statement3 , statement4
• If BooleanExpression1 is true and BooleanExpression2 is false , what is executed?
• statement3 , statement4
THE END
ANY QUESTION

More Related Content

What's hot (20)

PPTX
Types of loops in c language
sneha2494
 
DOC
Jumping statements
Suneel Dogra
 
PPTX
While , For , Do-While Loop
Abhishek Choksi
 
PPT
While loop
Feras_83
 
PPT
Looping statements in Java
Jin Castor
 
PPTX
Loops in c language
tanmaymodi4
 
PPT
Iteration
Liam Dunphy
 
PPTX
Presentation on nesting of loops
bsdeol28
 
PPTX
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
PPT
Looping in c++
deekshagopaliya
 
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
PPTX
Forloop
Dipen Vasoya
 
PPTX
Comp ppt (1)
Sriman Sawarthia
 
PPTX
C++ loop
Khelan Ameen
 
PPSX
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
PPT
Looping in c++
deekshagopaliya
 
Types of loops in c language
sneha2494
 
Jumping statements
Suneel Dogra
 
While , For , Do-While Loop
Abhishek Choksi
 
While loop
Feras_83
 
Looping statements in Java
Jin Castor
 
Loops in c language
tanmaymodi4
 
Iteration
Liam Dunphy
 
Presentation on nesting of loops
bsdeol28
 
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
Looping in c++
deekshagopaliya
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Forloop
Dipen Vasoya
 
Comp ppt (1)
Sriman Sawarthia
 
C++ loop
Khelan Ameen
 
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Looping in c++
deekshagopaliya
 

Viewers also liked (17)

PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PPTX
1.4 core programming [understand error handling]
tototo147
 
PPTX
Conditional statement c++
amber chaudary
 
PDF
Software Development Fundamentals
Chris Farrell
 
PPTX
1.1 core programming [understand computer storage and data types]
tototo147
 
PPT
CBSE Class XI Programming in C++
Pranav Ghildiyal
 
PDF
Multidimensional arrays in C++
Ilio Catallo
 
PPTX
Cs1123 3 c++ overview
TAlha MAlik
 
PPT
C++ control loops
pratikborsadiya
 
PPT
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
PPT
Overview of c++
geeeeeet
 
PPTX
C++ Overview PPT
Thooyavan Venkatachalam
 
PPTX
Overview of c++ language
samt7
 
PDF
Data structures / C++ Program examples
Kevin III
 
PPTX
Learn c++ Programming Language
Steve Johnson
 
PDF
Revision notes for exam 2011 computer science with C++
Deepak Singh
 
PPTX
Loops in C
Kamal Acharya
 
Control structures in C++ Programming Language
Ahmad Idrees
 
1.4 core programming [understand error handling]
tototo147
 
Conditional statement c++
amber chaudary
 
Software Development Fundamentals
Chris Farrell
 
1.1 core programming [understand computer storage and data types]
tototo147
 
CBSE Class XI Programming in C++
Pranav Ghildiyal
 
Multidimensional arrays in C++
Ilio Catallo
 
Cs1123 3 c++ overview
TAlha MAlik
 
C++ control loops
pratikborsadiya
 
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
Overview of c++
geeeeeet
 
C++ Overview PPT
Thooyavan Venkatachalam
 
Overview of c++ language
samt7
 
Data structures / C++ Program examples
Kevin III
 
Learn c++ Programming Language
Steve Johnson
 
Revision notes for exam 2011 computer science with C++
Deepak Singh
 
Loops in C
Kamal Acharya
 
Ad

Similar to Understand Decision structures in c++ (cplusplus) (20)

PPTX
C++ MAKING DECISION (IF IF/ELSE, SWITCH)
MelissaGuillermo1
 
PDF
Selection
Jason J Pulikkottil
 
PPTX
C++ IF STATMENT AND ITS TYPE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PPT
CHAPTER-3a.ppt
Tekle12
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPTX
intro to CONTROL STRUCTURES 1 for C++.pptx
ragustilo27
 
PPTX
Ref Lec 4- Conditional Statement (1).pptx
BilalAhmad735613
 
PPTX
Control Structures, If..else, switch..case.pptx
doncreiz1
 
PPTX
Fekra c++ Course #2
Amr Alaa El Deen
 
PPTX
Lecture 2
Mohammed Khan
 
PPT
2621008 - C++ 2
S.Ali Sadegh Zadeh
 
PPT
Chapter 4 Making Decisions
GhulamHussain142878
 
PPTX
CPP04 - Selection
Michael Heron
 
PPT
Chap 4 c++
Venkateswarlu Vuggam
 
PPTX
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
PPTX
Introduction to C++ programming language
divyadhanwani67
 
PDF
Chap 4 c++
Venkateswarlu Vuggam
 
PDF
Programming Fundamentals Decisions
imtiazalijoono
 
PDF
Selection & Making Decisions in c
yndaravind
 
PDF
Chap 4 c++
Widad Jamaluddin
 
C++ MAKING DECISION (IF IF/ELSE, SWITCH)
MelissaGuillermo1
 
CHAPTER-3a.ppt
Tekle12
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
intro to CONTROL STRUCTURES 1 for C++.pptx
ragustilo27
 
Ref Lec 4- Conditional Statement (1).pptx
BilalAhmad735613
 
Control Structures, If..else, switch..case.pptx
doncreiz1
 
Fekra c++ Course #2
Amr Alaa El Deen
 
Lecture 2
Mohammed Khan
 
2621008 - C++ 2
S.Ali Sadegh Zadeh
 
Chapter 4 Making Decisions
GhulamHussain142878
 
CPP04 - Selection
Michael Heron
 
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
divyadhanwani67
 
Programming Fundamentals Decisions
imtiazalijoono
 
Selection & Making Decisions in c
yndaravind
 
Chap 4 c++
Widad Jamaluddin
 
Ad

Recently uploaded (20)

PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 

Understand Decision structures in c++ (cplusplus)

  • 1. DECISION STRUCTURES THE IF-ELSE IF STATEMENT AND NESTED IF STATEMENTS IN C++ Presentators: M. Tahir Bashir Fahad Iftikhar Bilal Ahmad C++ @UMT 8-Apr-2016
  • 2. CONDITIONAL STATEMENTS • The C++ conditional statements are the: • Therefore they are sometimes called selection statements • Conditional statements give us the power to make basic decisions • if statement • if-else statement • switch statement • A conditional statement lets us choose which statement will be executed next
  • 3. FLOW OF CONTROL • Unless specified otherwise, the order of statement execution through a function is linear: one statement after another in sequence • Some programming statements allow us to: • decide whether or not to execute a particular statement • execute a statement over and over, repetitively • These decisions are based on Boolean expressions (or conditions) that evaluate to true or false • The order of statement execution is called the flow of control
  • 4. THE IF STATEMENT • The if statement has the following syntax: if ( condition ) statement; if is a C++ reserved word The condition must be a Boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.
  • 5. LOGIC OF AN IF STATEMENT condition evaluated statement true false
  • 6. THE IF STATEMENT • An example of an if statement: if (FirstNo > SecondNo) result = FirstNo - SecondNo; cout<<"The resultant is"<< result; • First the condition is evaluated -- the value of FirstNo is either greater than the value of SecondNo, or it is not • If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped. • Either way, the call to cout is executed next
  • 7. RELATIONAL OPERATORS • A condition often uses one of C++'s equality operators or relational operators == equal to < less than != not equal to • Note the difference between the equality operator (==) and the assignment operator (=) > greater than <= less than or equal to >= greater than or equal to
  • 8. LOGICAL OPERATORS • C++ provides logical operators. • The binary logical operators combine two Boolean expressions into one. • The unary logical operator switches the value of a Boolean expression. • Binary logical operators have lower precedence than relational operators • NOT has the same precedence as negation. Operator Meaning Kind && AND Binary || OR Binary ! NOT Unary
  • 9. LOGICAL NOT • The logical NOT operation is also called logical negation or logical complement • If some condition a is true, then !a is false; if a is false, then !a is true • Logical expressions can be shown using a truth table a !a True False False True
  • 10. LOGICAL AND & LOGICAL OR • The logical AND expression a && b is true if both a and b are true, and false otherwise • The logical OR expression a || b is true if a or b or both are true, and false otherwise
  • 11. IF-ELSE STATEMENT: General form of an if-else statement: if(BooleanExpression) statement or block 1 else statement or block 2
  • 12. LOGIC OF AN IF-ELSE STATEMENT condition evaluated statement1 true false statement2
  • 13. THE CONDITIONAL OPERATOR • C++ provides and operator to create short expressions that work like if-else statements. BooleanExpression ? Value1 : Value2; • If BooleanExpression is true, Value1 is returned • If BooleanExpression is false, Value2 is returned • Example: if (score < 50) cout<<“Sorry! You Have Failed…"; else cout<<"You Have Successfully Passed! ";
  • 14. THE IF-ELSE IF STATEMENT • Sometimes you need to be able to test a series of conditions • You can do this with the if-else if statement • General form: if (BooleanExpression1) statement or block 1 else if (BooleanExpression2) statement or block 2 else statement or block 3 • If BooleanExpression1 is true, then statement or block 1 is executed. • If BooleanExpression1 is false, then BooleanExpression2 is tested. • If BooleanExpression2 is true, then statement or block 2 ais executed. • If BooleanExpression2 is false, then statement or block 3 is executed. Note: You can have as many if else clauses as is needed.
  • 15. NESTED IF STATEMENTS • Nesting is enclosing one structure inside of another. • A block in C++ can contain any valid C++ code, this includes other if statements: if(BooleanExpression1) { if(BooleanExpression2) { statement1; statement2; } statement3; statement4; } • If BooleanExpression1 is true and BooleanExpression2 is true , what is executed? • statement1 , statement2 , statement3 , statement4 • If BooleanExpression1 is true and BooleanExpression2 is false , what is executed? • statement3 , statement4