Unit 6 Decision-Making in C++
Unit 6 Decision-Making in C++
PROGRAM DESIGN
● During a selection:
● A question is asked
● An action is taken based on the answer
● No matter the path taken, the next task is executed.
● Selection can be represented in code using:
○ The if selection statement is called a single-selection structure. It selects or
ignores a single action (group of actions)
○ The if…else statement is called a double selection structure. It selects
between two different action (group of actions)
○ The switch statement is called a multiple-selection structure. It selects
among different actions (group of actions)
RECAP FROM ALGORITHMS AND FLOWCHARTS:
Syntax: The if Statement
#include <iostream>
using namespace std;
if (condition)
int main()
{ {
int age;
}
cout<<"How old are you? ";
cin>>age;
return 0;
if (age>=60) }
{
Syntax: The if...else if Statement
if (condition_1)
{
[ statement_1a;
:
statement_na;]
}
else if (condition_2)
{
[ statement_1b;
:
statement_nb;]
}
Example: The if...else if Statement
#include <iostream> else if (age>=16 && age<=60)
using namespace std; {
int main() cout<<"Plan retirement";
{ }
Question 1:
Write a C++ program that accepts a mark value from the user. If the mark is
greater than 40, the program should print PASSED otherwise, it should print
FAILED
Question 2:
Write a C program that accepts a mark value Range Grade
from the user. System should display the 100.0 – 70.0 A
corresponding grade for the mark using the 69.9-50.0 B
49.9-40.0 C
given table
39.0 and below F
Syntax: The switch Statement
:
switch ( integer_expression ) case const_nm:
{
case const_1: [statement_nm1;
[statemebt_1a; :
: statement_nmk;]
statement_na;] break;
break;
case const_2: default:
[statement_1b; [statement_1z;
: :
statement_nb;] statement_nz;]
break;
}
Example: The switch Statement
#include <iostream> case 200:
using namespace std; cout<<"GOLD;
break;
int main()
{ case 300:
int value; cout<<GREEN";
cout<<Enter an integer: "; break;
cin>>value;
default:
switch (value) cout<<"BLACK";
{ break;
case 100: }
cout<<"\n RED";
break; return 0;
}
End