SlideShare uma empresa Scribd logo
1 de 17
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

Mais conteúdo relacionado

Mais procurados

Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 

Mais procurados (20)

Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Loops
LoopsLoops
Loops
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
While loop
While loopWhile loop
While loop
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c
Loops in cLoops in c
Loops in c
 
Iteration
IterationIteration
Iteration
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Looping
LoopingLooping
Looping
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Forloop
ForloopForloop
Forloop
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
C++ loop
C++ loop C++ loop
C++ loop
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 

Destaque

1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
tototo147
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
Chris Farrell
 
1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]
tototo147
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
TAlha MAlik
 
Overview of c++
Overview of c++Overview of c++
Overview of c++
geeeeeet
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
Kevin III
 

Destaque (17)

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
 
1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]1.1 core programming [understand computer storage and data types]
1.1 core programming [understand computer storage and data types]
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Overview of c++
Overview of c++Overview of c++
Overview of c++
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
Loops in C
Loops in CLoops in C
Loops in C
 

Semelhante a Understand Decision structures in c++ (cplusplus)

Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2
DanWooster1
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
dplunkett
 

Semelhante a Understand Decision structures in c++ (cplusplus) (20)

Control structure
Control structureControl structure
Control structure
 
Control_Statements.pptx
Control_Statements.pptxControl_Statements.pptx
Control_Statements.pptx
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
 
Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Conditonals.pdf
Conditonals.pdfConditonals.pdf
Conditonals.pdf
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
 
class interview demo
class interview demo class interview demo
class interview demo
 
python
pythonpython
python
 
Demo for Class.pptx
 Demo for Class.pptx Demo for Class.pptx
Demo for Class.pptx
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Decision Controls in C++ Programming Lecture
Decision Controls in C++ Programming LectureDecision Controls in C++ Programming Lecture
Decision Controls in C++ Programming Lecture
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 

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