SlideShare una empresa de Scribd logo
Algorithms and programming
basics
• Boolean operations
• Control structures
• if..else
• switch..case
Binary logic
• bool variable
• 1/0 – variable keep one of two variants
• It interpreter like true or false
• Binary logic is a statements, that gives one
result – true or false (0 or 1)
• 14>2 = 1(true)
• Example:
• int x = 12;
• x == 12
• Output: 1
Equality and relational operators
equality operator
or
relational operator
C++ equality
or rela tional
Operator
Example
of C++
condition
Meaning of
C++ condition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
 >= x >= y xis greater than or equal to y
 <= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y
Logical Operators
• Used as conditions in loops, if statements
• && (logical AND)
▫ true if both conditions are true
if ( gender == 1 && age >= 65 )
++seniorFemales;
• || (logical OR)
▫ true if either of condition is true
if ( semesterAverage>= 90|| finalExam >= 90 )
cout << "Student grade is A" << endl;
Logical Operators
• ! (logical NOT, logical negation)
▫ Returns true when its condition is false, & vice
versa
if ( !( grade == sentinelValue ) )
cout << "The next grade is " << grade << endl;
Alternative:
if ( grade != sentinelValue )
cout << "The next grade is " << grade << endl;
Control Structures
• Sequential execution
▫ Statements executed in order
• Transfer of control
▫ Next statement executed not next one in
sequence
• 3 control structures
▫ Sequence structure
 Programs executed sequentially by default
▫ Selection structures
 if, if/else, switch
▫ Repetition structures
 while, do/while, for
if Selection Structure
• Selection structure
▫ Choose among alternative courses of action
▫ Pseudocode example:
▫ If the condition is true
 Print statement executed, program continues to
next statement
▫ If the condition is false
 Print statement ignored, program continues
▫ Indenting makes programs easier to read
 C++ ignores whitespace characters (tabs, spaces,
etc.)
if Selection Structure
•if (logical statement)
•Action1;
•Else
•Action2;
if Selection Structure
• Translation into C++
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
• Diamond symbol (decision symbol)
▫ Indicates decision is to be made
▫ Contains an expression that can be true or false
 Test condition, follow path
• if structure
▫ Single-entry/single-exit
if/else structure
• “if” structure
▫ Make decision based on truth or falsity of
condition
▫ If condition met – body is executed, else - bodyis
not executed
▫ If there is an “else” structure
 Then if condition is false
the body of “else” structure
is executed
if/else structure
if(x > 10){
cout << "x is more than ten";
y = x * 2;
}
else
cout << "x is smaller";
• If there is more than one statements or expressions
in “if/else” structure use curly brackets { }
if/else Selection Structure
• if
▫ Performs action if condition true
• if/else
▫ Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 50 )
cout << "Passed";
else
cout << "Failed";
if/else Selection Structure
• Ternary conditional operator (?:)
▫ Three arguments (condition, value if true,
value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );
true
false
print “Failed” print “Passed”
grade >= 60
Condition Value if true Value if false
if/else Selection Structure
• Nested if/else structures
▫ One inside another, test for multiple cases
▫ Once condition met, other statements skipped
if student’s grade is greater than or equal to 90
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
if/else Selection Structure
if/else Selection Structure
• if (logical statement){
• Action1;
• Action2;
• Action3;
• }
• Else
• Action4;
• if (logical statement){
• Action1;
• Action2;
• }
• Else{
• Action3;
• Action4;
• }
if/else Selection Structure
• Compound statement
▫ Set of statements within a pair of braces
if ( grade >= 50 )
cout << "Passed.n";
else {
cout << "Failed.n";
cout << "You must take this course again.n";
}
▫ Without braces,
cout << "You must take this course again.n";
always executed
• Block
▫ Set of statements within braces
Switch structure
• Used for testing variable for multiple values
• For each value the case keyword is used
• If no cases matched then optional default is used
• switch(){
▫ case value1: statements; break;
▫ case value2: statements; break;
▫ ……
▫ default: statements; break;
• }
Switch and nested if else
Switch structure is the same as nested if/else structure
that uses only equality comparison
Switch diagram
52
Thanks for
your
attention!

Más contenido relacionado

Similar a Control Structures, If..else, switch..case.pptx

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
mcollison
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 
Selection
SelectionSelection
Control structures in C
Control structures in CControl structures in C
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
ronistgdr
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
ssuserfb3c3e
 
Ch3.1
Ch3.1Ch3.1
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
Stephen JE Ventura
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
Karwan Mustafa Kareem
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Lecture#3 Algorithms and computing
Lecture#3 Algorithms and computingLecture#3 Algorithms and computing
Lecture#3 Algorithms and computing
NUST Stuff
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
2b75fd3051
 
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
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
Abdullah Bhojani
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
Blue Elephant Consulting
 
conditional_statement.pdf
conditional_statement.pdfconditional_statement.pdf
conditional_statement.pdf
MdShahriarHossainSak
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statements
Vaibhav Khanna
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
Khirulnizam Abd Rahman
 

Similar a Control Structures, If..else, switch..case.pptx (20)

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Selection
SelectionSelection
Selection
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Ch3.1
Ch3.1Ch3.1
Ch3.1
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Lecture#3 Algorithms and computing
Lecture#3 Algorithms and computingLecture#3 Algorithms and computing
Lecture#3 Algorithms and computing
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
conditional_statement.pdf
conditional_statement.pdfconditional_statement.pdf
conditional_statement.pdf
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statements
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 

Último

Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 

Último (20)

Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 

Control Structures, If..else, switch..case.pptx

  • 1. Algorithms and programming basics • Boolean operations • Control structures • if..else • switch..case
  • 2. Binary logic • bool variable • 1/0 – variable keep one of two variants • It interpreter like true or false • Binary logic is a statements, that gives one result – true or false (0 or 1) • 14>2 = 1(true) • Example: • int x = 12; • x == 12 • Output: 1
  • 3. Equality and relational operators equality operator or relational operator C++ equality or rela tional Operator Example of C++ condition Meaning of C++ condition Relational operators > > x > y x is greater than y < < x < y x is less than y  >= x >= y xis greater than or equal to y  <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y  != x != y x is not equal to y
  • 4. Logical Operators • Used as conditions in loops, if statements • && (logical AND) ▫ true if both conditions are true if ( gender == 1 && age >= 65 ) ++seniorFemales; • || (logical OR) ▫ true if either of condition is true if ( semesterAverage>= 90|| finalExam >= 90 ) cout << "Student grade is A" << endl;
  • 5. Logical Operators • ! (logical NOT, logical negation) ▫ Returns true when its condition is false, & vice versa if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;
  • 6. Control Structures • Sequential execution ▫ Statements executed in order • Transfer of control ▫ Next statement executed not next one in sequence • 3 control structures ▫ Sequence structure  Programs executed sequentially by default ▫ Selection structures  if, if/else, switch ▫ Repetition structures  while, do/while, for
  • 7. if Selection Structure • Selection structure ▫ Choose among alternative courses of action ▫ Pseudocode example: ▫ If the condition is true  Print statement executed, program continues to next statement ▫ If the condition is false  Print statement ignored, program continues ▫ Indenting makes programs easier to read  C++ ignores whitespace characters (tabs, spaces, etc.)
  • 8. if Selection Structure •if (logical statement) •Action1; •Else •Action2;
  • 9. if Selection Structure • Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; • Diamond symbol (decision symbol) ▫ Indicates decision is to be made ▫ Contains an expression that can be true or false  Test condition, follow path • if structure ▫ Single-entry/single-exit
  • 10. if/else structure • “if” structure ▫ Make decision based on truth or falsity of condition ▫ If condition met – body is executed, else - bodyis not executed ▫ If there is an “else” structure  Then if condition is false the body of “else” structure is executed
  • 11. if/else structure if(x > 10){ cout << "x is more than ten"; y = x * 2; } else cout << "x is smaller"; • If there is more than one statements or expressions in “if/else” structure use curly brackets { }
  • 12. if/else Selection Structure • if ▫ Performs action if condition true • if/else ▫ Different actions if conditions true or false • Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” • C++ code if ( grade >= 50 ) cout << "Passed"; else cout << "Failed";
  • 13. if/else Selection Structure • Ternary conditional operator (?:) ▫ Three arguments (condition, value if true, value if false) • Code could be written: cout << ( grade >= 60 ? “Passed” : “Failed” ); true false print “Failed” print “Passed” grade >= 60 Condition Value if true Value if false
  • 14. if/else Selection Structure • Nested if/else structures ▫ One inside another, test for multiple cases ▫ Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”
  • 16. if/else Selection Structure • if (logical statement){ • Action1; • Action2; • Action3; • } • Else • Action4; • if (logical statement){ • Action1; • Action2; • } • Else{ • Action3; • Action4; • }
  • 17. if/else Selection Structure • Compound statement ▫ Set of statements within a pair of braces if ( grade >= 50 ) cout << "Passed.n"; else { cout << "Failed.n"; cout << "You must take this course again.n"; } ▫ Without braces, cout << "You must take this course again.n"; always executed • Block ▫ Set of statements within braces
  • 18. Switch structure • Used for testing variable for multiple values • For each value the case keyword is used • If no cases matched then optional default is used • switch(){ ▫ case value1: statements; break; ▫ case value2: statements; break; ▫ …… ▫ default: statements; break; • }
  • 19. Switch and nested if else Switch structure is the same as nested if/else structure that uses only equality comparison