Selection Program Structure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Computer Fundamentals

SELECTION/CONDITIONAL PROGRAM STRUCTURE

Syntax 1. if condition

if (condition) //no semicolon at the end of parenthesis


{
statement 1; if the condition is TRUE it will execute the statement/s after the if
condition. Otherwise, it will do nothing.
:
: { } is only needed if you have more than one statement inside
statement n; the if condition. Otherwise, you can remove { }
}

Syntax 2. if-else condition

if (condition) //no semicolon at the end of parenthesis


{
statement 1; if the condition is TRUE it will execute the statement/s after the if
condition. Otherwise, it will execute the statement/s after the else
:
keyword.
:
statement n; { } is only needed if you have more than one statement inside the if or else
} condition. Otherwise, you can remove { }

else //no parenthesis needed and semicolon at the end


{
statement 1;
:
:
statement n;
}

Syntax 3. Multiway if condition

In a multiway if condition, if the first if condition is TRUE it will execute the


statement/s for that condition and it will continue to check for the other if condition until it
reaches the last if condition.

if (condition 1) //no semicolon at the end of parenthesis


{
statement/s;
}

if (condition 2) //no semicolon at the end of parenthesis


{
statement/s;
}
:
:
if (condition n) //no semicolon at the end of parenthesis
{
statement/s;
}

Page 1 of 7
Computer Fundamentals

Syntax 4. Multiway if-else condition

In a multiway if-else condition, if the first if condition is TRUE it will execute the
statement/s for that condition and it will check the other if condition in the list.

On the other hand, if no if condition is satisfied it will execute the statement of the
last else in the list.

if (condition 1) //no semicolon at the end of parenthesis


{
statement/s;
}

else if (condition 2) //no semicolon at the end of parenthesis


{
statement/s;
}
:
:
else if (condition n) //no semicolon at the end of parenthesis
{
statement/s;
}

else //no parenthesis and semicolon at the end


{
statement/s;
}

Syntax 5. Nested if-else condition

if (condition 1) //no semicolon at the end of parenthesis


{ if (condition 1-1)
{ statement/s;
}
else
{ statement/s;
}
}

else //no semicolon at the end of parenthesis


{ if (condition)
{ statement/s;
}
else
{ statement/s;
}
}

Page 2 of 7
Computer Fundamentals

switch – is a multiway selection statement with this syntax :


(switch is similar to multiway if-else)

switch (identifier) //no semicolon at the end of parenthesis


{
case value1 : //terminated with a colon
statement/s;
break;

case value2 : //terminated with a colon


statement/s;
break;

:
:

case valueN : //terminated with a colon


statement/s;
break;

default : //terminated with a colon


statement/s;

Page 3 of 7
Computer Fundamentals

SELECTION/CONDITIONAL PROGRAM STRUCTURE

1.) Create a flowchart and a C++ program that will prompt the user to input two
numbers and output the highest number entered.

Flowchart C++ Program


Start
#include <iostream>
using namespace std;
Declarations int main()
num N1, N2 { int N1, N2;

cout<<” Input two numbers: ”;


Input N1, N2 cin>> N1>> N2;

if (N1 > N2)


cout<<N1<<endl;
If N
else
N1 > N2 Output N2
cout<<N2<<endl;
return 0;
Y }
Output N1

Stop

Page 4 of 7
Computer Fundamentals

2.) Create a flowchart and a C++ program that would input an integer number and then
indicate whether the number is an even or an odd number.

Flowchart
Start

Declarations
num number

Input number

If Y Output
number % 2 == 0 A
“even number”

N
Output
“odd number”
A

Stop

C++ Program

#include<iostream>
using namespace std;

int main()
{ int num

cout<<” Input an integer number: ”;


cin>> num;

if (num % 2 == 0)
cout<< “ The number you’ve entered is an even number”<<endl;
else
cout<< “ The number you’ve entered is an odd number”<<endl;
return 0;
}

Page 5 of 7
Computer Fundamentals

3.) Workers at Kookaburra Factory have a regular working hours of 30 hours per week
and are paid $ 10.00 per hour. However, if the workers rendered more than 30 hours
per week, the excess hours are paid 75% more. Create a flowchart and a C++ program
that would input the number of hours rendered by a worker in one week and output his
net salary.
Flowchart

Start

Declarations
NHrs, Salary

Input NHrs

If N
Salary = NHrs *10 A
NHrs > 30

Y
Salary = 300 + (NHrs – 30) * 17.50
A

Output Salary

Stop

C++ Program

#include <iostream>
using namespace std;

int main()
{ double NHrs, Salary;

cout<< ” Input number of hours worked: ”;


cin>> NHrs;
if (NHrs > 30)
Salary = 300 + (NHrs – 30) * 17.50;
else
Salary = NHrs * 10.00;
cout<< “ Your salary = ”<< Salary<<endl;
return 0;
}
Page 6 of 7
Computer Fundamentals

4.) The fine for an over-speeding violation depends on the speed of the erring driver, as
follows:
100 to 120 km/h = 3,000.00 Php
121 km/h and above = 5, 000 Php

Create a flowchart and a C++ program to input the car’s speed and then output the
fine, if there’s any.

5.) In the game of JACK N POY each of the two player choose either a scissors (code S),
paper (code P), or rock (code R). If one chooses scissors and the other chooses stone
then stone wins. If one chooses paper and the other stone then paper wins. If one chooses
paper and the other scissors then scissors wins. If they both choose the same then the
result is a tie. Create a flowchart or a C++ program that will input two character codes
corresponding to the object selected and then output either the message “PLAYER 1
WINS” or “PLAYER 2 WINS”.

Page 7 of 7

You might also like