0% found this document useful (0 votes)
32 views18 pages

Unit 6 Decision-Making in C++

The document provides an introduction to structured program design in C++, focusing on decision-making using various operators and control structures. It covers syntax and examples for if, if...else, if...else if, nested if statements, and switch statements. Additionally, it includes exercises for practicing mark evaluation and grading based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views18 pages

Unit 6 Decision-Making in C++

The document provides an introduction to structured program design in C++, focusing on decision-making using various operators and control structures. It covers syntax and examples for if, if...else, if...else if, nested if statements, and switch statements. Additionally, it includes exercises for practicing mark evaluation and grading based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INTRODUCTION TO STRUCTURED

PROGRAM DESIGN

Linda Amoako Banning, PhD


DECISION-
MAKING IN C++
SOME OPERATORS USED IN C++

■ Arithmetic Operators ■ Relational Operators


■ +, - ■ < - Less than
■ *, /, %
■ > - Greater than
■ Logical Operators ■ <= - Less or equal to
■ && - AND ■ >= - Greater or equal to
■ || - OR ■ == - Equal to
■ ! - NOT ■ != - Not Equal to
RECAP FROM ALGORITHMS AND FLOWCHARTS:

● 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()
{ {

statement 1; int age;


cout<<"How old are you? ";
cin>>age;
[statement 2;
if (age>=60)
: {
cout<<"You must retire!";
: }

statement n;] return 0;


}
}
Syntax: The if...else Statement
if (condition)
{
[ statement_1a;
:
statement_na;]
}
else
{
[ statement_1b;
:
statement_nb;]
}
Example: The if...else Statement
#include <iostream> cout<<"You must retire!";
using namespace std; }
else
int main() {
{ cout<<"You have "<< 60-age <<" more years to
retire"<<endl;

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";
{ }

int age = 0; else if (age>60)


{
cout<<"How old are u? : "; cout<<"Kindly retire immediately!";
cin>>age;
}
If (age <16)
{ return 0;
cout<<"You cannot work by law!"; }
}
Syntax: The if...else if…else Statement
if (condition_1) else if (condition_k)
{ {
[ statement_1a; [ statement_ka;
: :
statement_na;] statement_kn;]
}
else if (condition_2) }
{ else
[ statement_1b; {
: [ statement_xa;
statement_nb;] :
} statement_xn;]
}
Example: The if...else if…else Statement
# include <iostream> else if (age>=50 && age<60)
using namespace std; {
int main() cout<<"Better plan for your retirement!";
{ }
int age = 0; else
cout<<"How old are u? "; {
cin>>age; cout<<"You should have retired by now!!!";
if(age<16) }
{
cout<<"You cannot work by Law!";
} return 0;
else if (age>=16 && age<50) }
{
cout<<"You still have sometime before u
retire.";
}
Syntax: Nested if Statement
if (condition_1) }
{ }
if (condition_1_a) else
{ {
[statement_1a; if (condition_2_a)
statement_na;] {
} [statement_1c;
else statement_nc;]
{ }
[statement_1b; }
statement_nb;]
Example: Nested if Statement
#include <iostream> else if (age>=16 && age <50)
using namespace std; {
cout<<"Keep working";
int main() }
{ else if (age >=50 && age <60)
int age = 0; {
cout<<"Plan your retirement";
cout<<"How old are you? : "; }
cin>>age; else if (age>=60)
if (age<16 && age >0) {
{ cout<<"You should have retired by now!";
cout<<"You cannot work by law!"; }
if (age>12 && age < 16) else
{ {
cout<<"You should be in JHS"; cout<<" Error!!! ";
} }
else
{ return 0;
cout<<"You should be in Primary school"; }
}
}
Speed Byte

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

You might also like