SlideShare a Scribd company logo
Lab 5
CS106 - Fundamentals of Computer Science
Presented by TA. Nada Kamel
Agenda
 Multiway if/else statement
 Nested if/else statement
 Switch structure
 Example
 Hands-on problem
Presented by TA. Nada Kamel
Multiway if/else
Presented by TA. Nada Kamel
Multiway if/else
if(boolean_expression_1)
{
// statements to be executed if the first boolean expression is true
}
else if(boolean_expression_2)
{
// statements to be executed if the first boolean expression is false and the second boolean
expression is true
}
..
..
else
{
// statements to be executed if all test expressions are false
}
Presented by TA. Nada Kamel
Nested if/else
Presented by TA. Nada Kamel
Nested if/else
Presented by TA. Nada Kamel
if(boolean_expression_1)
{
// if statement to be executed if the first boolean expression is true
if(boolean_expression_2)
{
// statements to be executed if the second boolean expression is true
}
else
{
// statements to be executed if the second boolean expression is false
}
}
Example
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Switch
Presented by TA. Nada Kamel
Switch
switch (expression)
{
case constant_expression :
// statement(s)
break;
case constant_expression :
// statement(s)
break;
..
..
default :
// statement(s)
}Presented by TA. Nada Kamel
Example
Presented by TA. Nada Kamel
Example
Write a program in C++ that reads a character of a student final
grade in a course to display the following:
Sample Input: D
Sample Output: You passed, your grade is D.
Presented by TA. Nada Kamel
Grade Output
A Excellent!
B Well done
C Well done
D You passed
F Better try again
if any other letter entered Invalid grade
Presented by TA. Nada Kamel
Hands-on Problem
Presented by TA. Nada Kamel
Problem 1
At AUC, the academic standing of a student is determined based on the number of the
credit hours that the student has achieved. The rules are as follows:
Write a program in C++ to input the number of credit hours achieved by the student
and print out his/her academic standing.
Implement your solution in C++ once using nested-if structure and second using
switch structure.
Validate the user input to accept only number of credit hours that is between 0 and
133 (inclusive).
Number of Credit Hours (CrH) Academic Standing
CrH < 30 Freshman
30 <= CrH < 60 Sophomore
60 <= CrH < 90 Junior
90 <= CrH < 120 Senior
120 <= CrH Graduating Senior
Presented by TA. Nada Kamel
Nested if-else
statement
Presented by TA. Nada Kamel
Switch case
Presented by TA. Nada Kamel
Any Questions?
Presented by TA. Nada Kamel

More Related Content

PPSX
CS106 Lab 4 - If statement
PPSX
CS106 Lab 6 - While and Do..While loop
PPSX
CS106 Lab 7 - For loop
PPSX
CS106 Lab 11 - Functions (passing by reference)
PPSX
CS106 Lab 8 - Nested loops
PPSX
CS106 Lab 10 - Functions (passing by value)
PPSX
CS106 Lab 3 - Modulus
CS106 Lab 4 - If statement
CS106 Lab 6 - While and Do..While loop
CS106 Lab 7 - For loop
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 8 - Nested loops
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 3 - Modulus

What's hot (20)

PPTX
Set theory and logic
PPTX
MATLAB programming tips 2 - Input and Output Commands
DOC
PPTX
Data structures and algorithms lab2
PPTX
Introduction To Algorithms
PPTX
Algorithm and pseudocode conventions
PPT
Fundamental of Algorithms
DOCX
Compiler lab final report writing
PPTX
Data structures and algorithms lab3
PPT
Code Tuning
PDF
Algorithm defination, design & Implementation
DOCX
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
PPSX
Type conversion
PPTX
20100522 software verification_sharygina_lecture02
PPTX
Sorting two numbers
PDF
DOCX
Mycasestudy
PDF
N_Asm Assembly recursion (sol)
PDF
CP Handout#4
PDF
Linear Regression Parameters
Set theory and logic
MATLAB programming tips 2 - Input and Output Commands
Data structures and algorithms lab2
Introduction To Algorithms
Algorithm and pseudocode conventions
Fundamental of Algorithms
Compiler lab final report writing
Data structures and algorithms lab3
Code Tuning
Algorithm defination, design & Implementation
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
Type conversion
20100522 software verification_sharygina_lecture02
Sorting two numbers
Mycasestudy
N_Asm Assembly recursion (sol)
CP Handout#4
Linear Regression Parameters
Ad

Similar to CS106 Lab 5 - Switch case (20)

PDF
MA3696 Lecture 7
PPT
cpphtp4_PPT_02.ppt
PPTX
C++ Condition C++ Conditionsd.pptxs.pptx
PDF
Unit 2=Decision Control & Looping Statements.pdf
PPTX
week 3 Programming lecture 05 (1) j.pptx
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PPTX
05. Conditional Statements
PDF
Loops and conditional statements
PPTX
Programming note C#
PDF
Programming C Part 03
PPT
11-ScriptsAndConditionals.ppt
PPT
11ScriptsAndConditionalsinpython1234.ppt
DOCX
Absolute Java 5e Savitch Test Bank
PDF
Control structures c2 c3
PPTX
Cs1123 5 selection_if
PPT
Flow of Control
PDF
9-java language basics part3
PDF
Undecidabality
PDF
Chap 5 c++
MA3696 Lecture 7
cpphtp4_PPT_02.ppt
C++ Condition C++ Conditionsd.pptxs.pptx
Unit 2=Decision Control & Looping Statements.pdf
week 3 Programming lecture 05 (1) j.pptx
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
05. Conditional Statements
Loops and conditional statements
Programming note C#
Programming C Part 03
11-ScriptsAndConditionals.ppt
11ScriptsAndConditionalsinpython1234.ppt
Absolute Java 5e Savitch Test Bank
Control structures c2 c3
Cs1123 5 selection_if
Flow of Control
9-java language basics part3
Undecidabality
Chap 5 c++
Ad

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chinmaya Tiranga quiz Grand Finale.pdf
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx

CS106 Lab 5 - Switch case

  • 1. Lab 5 CS106 - Fundamentals of Computer Science Presented by TA. Nada Kamel
  • 2. Agenda  Multiway if/else statement  Nested if/else statement  Switch structure  Example  Hands-on problem Presented by TA. Nada Kamel
  • 4. Multiway if/else if(boolean_expression_1) { // statements to be executed if the first boolean expression is true } else if(boolean_expression_2) { // statements to be executed if the first boolean expression is false and the second boolean expression is true } .. .. else { // statements to be executed if all test expressions are false } Presented by TA. Nada Kamel
  • 6. Nested if/else Presented by TA. Nada Kamel if(boolean_expression_1) { // if statement to be executed if the first boolean expression is true if(boolean_expression_2) { // statements to be executed if the second boolean expression is true } else { // statements to be executed if the second boolean expression is false } }
  • 8. Presented by TA. Nada Kamel
  • 10. Switch switch (expression) { case constant_expression : // statement(s) break; case constant_expression : // statement(s) break; .. .. default : // statement(s) }Presented by TA. Nada Kamel
  • 12. Example Write a program in C++ that reads a character of a student final grade in a course to display the following: Sample Input: D Sample Output: You passed, your grade is D. Presented by TA. Nada Kamel Grade Output A Excellent! B Well done C Well done D You passed F Better try again if any other letter entered Invalid grade
  • 13. Presented by TA. Nada Kamel
  • 15. Problem 1 At AUC, the academic standing of a student is determined based on the number of the credit hours that the student has achieved. The rules are as follows: Write a program in C++ to input the number of credit hours achieved by the student and print out his/her academic standing. Implement your solution in C++ once using nested-if structure and second using switch structure. Validate the user input to accept only number of credit hours that is between 0 and 133 (inclusive). Number of Credit Hours (CrH) Academic Standing CrH < 30 Freshman 30 <= CrH < 60 Sophomore 60 <= CrH < 90 Junior 90 <= CrH < 120 Senior 120 <= CrH Graduating Senior Presented by TA. Nada Kamel
  • 17. Switch case Presented by TA. Nada Kamel
  • 18. Any Questions? Presented by TA. Nada Kamel