SlideShare a Scribd company logo
1 of 40
C++ Basics
Assignment Statements
• Standard Assignment Statement
– In an assignment statement, first the expression on
the right-hand side of the equal sign is evaluated and
then the variable on the left-hand side of the equal
sign is set equal to this value.
– Syntax:
Variable = Expression;
– Examples:
distance = rate * time;
count = count + 2;
Compound Assignment Statement
Escape Sequences
Constants
• When you initialize a variable inside a
declaration, you can mark the variable so that
the program is not allowed to change its
value. To do this, place the word const in
front of the declaration.
• Syntax:
const Type_Name Variable_Name = Constant;
• Examples:
const int MAX_TRIES = 3;
const double PI = 3.14159;
Decision Controls
Decision making
• Critical to computer programming
• Many situations where a choice between
two or more options is required
Condition
• The basis for writing our programmed
decision control
• Must always equate to TRUE or FALSE
– Boolean type ( bool )
Simple IF Statement
• If the condition is true, a statement is
executed; and if the condition is false,
nothing happens.
• Syntax:
if ( <condition> )
<statement> ;
Simple IF Statement
Simple IF Statement
• Syntax:
if ( <condition> )
<statement> ;
• Example:
cin >> grade;
if ( grade >= 75 )
cout << “Congrats! You Passed.”;
Simple IF Statement
• In the case where we need to execute more
than one statements, we block the
statements.
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
Simple IF Statement
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
• Example
cin >> grade;
if ( grade >= 75 )
{
cout<<“Congrats.n”;
cout<<“You passed.”;
}
Let’s try…
• Write a program that will determine if a
person is legally an adult or not by reading
in his/her age.
Program sample 1
Program sample 1
Program sample 1
Program sample 1
Program sample 1
Simple IF-ELSE Statement
• If the condition is true, a statement is
executed; and if the condition is false, a
different statement is executed.
• Syntax:
if ( <condition> )
<statement> ;
else
<statement> ;
Simple IF-ELSE Statement
Simple IF-ELSE Statement
• Syntax:
if ( <condition> )
<statement_1> ;
else
<statement_2> ;
• Example:
cin >> grade;
if ( grade >= 75 )
cout<< “Congrats.”;
else
cout<<“You failed.”;
Simple IF-ELSE Statement
• In the case where we
need to execute more
than one statements, we
block the statements.
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
else
{
<statement> ;
<statement> ;
}
Simple IF-ELSE Statement
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
else
{
<statement> ;
<statement> ;
}
• Example:
if ( grade >= 75)
{
cout<<“Congrats.n”;
cout<<“You passed.”;
}
else
{
cout<<“Sorry.n”;
cout<<“You failed.”;
}
Continuing Sample 1
Switch Statement
• Alternative to the long string of ifs
• Not as flexible as the if statement
• Useful when a single variable must be
compared to multiple values.
Nested ifs that display department name based on
department number
Conditional Operator
• Yet another alternative to the if statement
• Here, the if operator (conditional operator)
is represented by a question mark (?)
Can be expressed using the conditional operator:
USING THE LOGICAL AND AND
OR OPERATORS
Logical AND operator
• Use a logical AND operator to create a
compound Boolean expression in which
two conditions must be true for the entire
expression to evaluate as TRUE.
• Create by typing two ampersands (&&)
between two Boolean expressions.
Truth Table (AND)
Logical OR operator
• Used to create a compound Boolean
expression in which at least one of two
conditions must be true for the entire
expression to evaluate as TRUE.
• Created by typing two pipes (||) between
Boolean expressions.
Truth Table (OR)
Combining AND and OR selections
• ANDs and ORs can be combined into a
single expression.
• AND takes precedence (evaluated first)
Evaluated first
OK, …
Now, we practice …

More Related Content

Similar to Decision Controls in C++ Programming Lecture

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptxAliAbro7
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
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
 
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.pptxdoncreiz1
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control StructuresPRN USM
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceRaianaTabitha
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 
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 ConceptsTech
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
lecture 6 bca 1 year-1.pptx
lecture 6 bca 1 year-1.pptxlecture 6 bca 1 year-1.pptx
lecture 6 bca 1 year-1.pptxclassall
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Muhammad Tahir Bashir
 

Similar to Decision Controls in C++ Programming Lecture (20)

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Switch.pptx
Switch.pptxSwitch.pptx
Switch.pptx
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
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
 
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
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
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
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Control statements
Control statementsControl statements
Control statements
 
lecture 6 bca 1 year-1.pptx
lecture 6 bca 1 year-1.pptxlecture 6 bca 1 year-1.pptx
lecture 6 bca 1 year-1.pptx
 
Control statements
Control statementsControl statements
Control statements
 
control structure
control structurecontrol structure
control structure
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 

Recently uploaded

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 

Recently uploaded (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

Decision Controls in C++ Programming Lecture

  • 2. Assignment Statements • Standard Assignment Statement – In an assignment statement, first the expression on the right-hand side of the equal sign is evaluated and then the variable on the left-hand side of the equal sign is set equal to this value. – Syntax: Variable = Expression; – Examples: distance = rate * time; count = count + 2;
  • 5. Constants • When you initialize a variable inside a declaration, you can mark the variable so that the program is not allowed to change its value. To do this, place the word const in front of the declaration. • Syntax: const Type_Name Variable_Name = Constant; • Examples: const int MAX_TRIES = 3; const double PI = 3.14159;
  • 7. Decision making • Critical to computer programming • Many situations where a choice between two or more options is required
  • 8. Condition • The basis for writing our programmed decision control • Must always equate to TRUE or FALSE – Boolean type ( bool )
  • 9. Simple IF Statement • If the condition is true, a statement is executed; and if the condition is false, nothing happens. • Syntax: if ( <condition> ) <statement> ;
  • 11. Simple IF Statement • Syntax: if ( <condition> ) <statement> ; • Example: cin >> grade; if ( grade >= 75 ) cout << “Congrats! You Passed.”;
  • 12. Simple IF Statement • In the case where we need to execute more than one statements, we block the statements. • Syntax: if ( <condition> ) { <statement> ; <statement> ; }
  • 13. Simple IF Statement • Syntax: if ( <condition> ) { <statement> ; <statement> ; } • Example cin >> grade; if ( grade >= 75 ) { cout<<“Congrats.n”; cout<<“You passed.”; }
  • 14. Let’s try… • Write a program that will determine if a person is legally an adult or not by reading in his/her age.
  • 20. Simple IF-ELSE Statement • If the condition is true, a statement is executed; and if the condition is false, a different statement is executed. • Syntax: if ( <condition> ) <statement> ; else <statement> ;
  • 22. Simple IF-ELSE Statement • Syntax: if ( <condition> ) <statement_1> ; else <statement_2> ; • Example: cin >> grade; if ( grade >= 75 ) cout<< “Congrats.”; else cout<<“You failed.”;
  • 23. Simple IF-ELSE Statement • In the case where we need to execute more than one statements, we block the statements. • Syntax: if ( <condition> ) { <statement> ; <statement> ; } else { <statement> ; <statement> ; }
  • 24. Simple IF-ELSE Statement • Syntax: if ( <condition> ) { <statement> ; <statement> ; } else { <statement> ; <statement> ; } • Example: if ( grade >= 75) { cout<<“Congrats.n”; cout<<“You passed.”; } else { cout<<“Sorry.n”; cout<<“You failed.”; }
  • 26. Switch Statement • Alternative to the long string of ifs • Not as flexible as the if statement • Useful when a single variable must be compared to multiple values.
  • 27. Nested ifs that display department name based on department number
  • 28.
  • 29. Conditional Operator • Yet another alternative to the if statement • Here, the if operator (conditional operator) is represented by a question mark (?)
  • 30. Can be expressed using the conditional operator:
  • 31. USING THE LOGICAL AND AND OR OPERATORS
  • 32. Logical AND operator • Use a logical AND operator to create a compound Boolean expression in which two conditions must be true for the entire expression to evaluate as TRUE. • Create by typing two ampersands (&&) between two Boolean expressions.
  • 33.
  • 35. Logical OR operator • Used to create a compound Boolean expression in which at least one of two conditions must be true for the entire expression to evaluate as TRUE. • Created by typing two pipes (||) between Boolean expressions.
  • 36.
  • 38. Combining AND and OR selections • ANDs and ORs can be combined into a single expression. • AND takes precedence (evaluated first)
  • 40. OK, … Now, we practice …