0% found this document useful (0 votes)
3 views26 pages

CSC 126 CH3

Chapter 3 covers selection control structures in programming, focusing on relational and logical operators, Boolean expressions, and types of selection structures such as one-way, two-way, and multi-way selections. It explains how to implement decision-making using pseudocode and flowcharts, along with examples of if statements and their syntax. The chapter also discusses the precedence of operators and the use of compound statements in selection control.

Uploaded by

2024223538
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)
3 views26 pages

CSC 126 CH3

Chapter 3 covers selection control structures in programming, focusing on relational and logical operators, Boolean expressions, and types of selection structures such as one-way, two-way, and multi-way selections. It explains how to implement decision-making using pseudocode and flowcharts, along with examples of if statements and their syntax. The chapter also discusses the precedence of operators and the use of compound statements in selection control.

Uploaded by

2024223538
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/ 26

CHAPTER 3

SELECTION CONTROL
STRUCTURE
Lesson Outcomes

• Use the relational operator (>, >=, <, <=, ==,!=)


• Use the logical operator (!, &&, ||)
• Understand the Boolean expression
• Write the pseudocode for the selection structure
• Create flowchart for the selection structure
• Implement decision using one‐way, two‐ways, and multiple‐ways
• selection structure (if, if-else)
• Recognize the correct ordering of decisions in multiple branches
• Program simple and complex decision
Comparison & Expression

▪ To make decisions, you must be able to make comparisons and


express the conditions that are true or false.
▪ Comparison is done by using relational operator.

i.e. i > j, x == y, etc.

▪ An expression that has a value either true or false is called a logical


(Boolean) expression.
▪ Condition is represented by a logical (Boolean) expression.
Relational Operator
1. Relational Operator
▪ There are 6 relational operators that allow to state conditions and
make comparisons.
▪ They are all binary operations that accept 2 operands and
compare them.
▪ The result is logical data, that is, it is always true (1) or false (0).

Relational Operator Meaning


< Less than
<= Less or equal
> Greater
>= Greater or equal
== Equal
!= Not equal
Logical Operator

▪ Logical operators enable you to combine logical expressions.


▪ Logical operators take only logical values as operands and
produce only logical values as result.
▪ The operator ! is unary, so it has only one operand.
▪ The operator && and || are binary operators, so it has more than
one operand.

Operator Description
! not
&& and
|| or
Boolean Expression

▪ An expression in which two numbers (arithmetic values) is compared


using a single relational operator.
▪ Each Boolean expression has the Boolean value true or false according
to the arithmetic validity of the expression:
▪ Syntax of Boolean expression:
<arithmetic value> relational operator <arithmetic value> Boolean value
▪ Example of Boolean expression using relational operator:

Expression Meaning Value


8 < 15 8 is less than 15 True
6!=6 6 is not equal to 6 False

2.5 > 5.8 2.5 is greater than 5.8 False

5.9 <= 7.5 5.9 is less than or equal to 7.5 True


Boolean Expression – the ! (not) Operator
1. The ! (not) operator
▪ Expression:
Expression !(Expression)
true (1) false (0)
false (0) true (1)

▪ Example:
Expression Value Explanation
Because ‘A’ > ‘B’ is false, ! (‘A’
! (‘A’ > ‘B’) true
> ‘B’) is true
Because 6 <= 7 is true, ! (6 <=
! (6 <= 7) false 7) is false
Boolean Expression – the && (and) Operator
▪ Expression:
Expression 1 Expression 2 Expression 1 && Expression 2
true (1) true (1) true (1)
true (1) false (0) false (0)
false (0) true (1) false (0)
false(0) false(0) false (0)
▪ Example:
Expression 1 Value Explanation
Because (14 > = 5) is true, (‘A’ < ‘B’)
(14 > = 5) && (‘A’ < ‘B’) true is true, and true && true is true, the
expression evaluates to true.
Because (24 >= 35) is false, (‘A’ < ‘B’)
false
(24 >= 35) && (‘A’ < ‘B’) is true, and false && true is false, the
expression evaluates to false. 8
Boolean Expression – the && (and) Operator

• Logical expressions can be unpredictable


• The following expression appears to represent a comparison of 0,
num = 12, and 10:
0 <= num <= 10
• It always evaluates true because
• 0<=12 evaluates to 1, and 1 <= 10 is true.
• A correct way to write this expression is:
num >= 0 && num <= 10
Boolean Expression – the || (or) Operator
▪ Expression:
Expression 1 Expression 2 Expression 1 && Expression 2
true (1) true (1) true (1)
true (1) false (0) true (1)
false (0) true (1) true (1)
false(0) false(0) false (0)

▪ Example:
Expression 1 Value Explanation
true Because (14 > = 5) is true, (‘A’ > ‘B’) is false,
(14 > = 5) || (‘A’ > ‘B’) and true || false is true, the expression
evaluates to true.

false Because (24 >= 35) is false, (‘A’ > ‘B’)


(24 >= 35) || (‘A’ >‘B’) is false, and false && false is false, the
expression evaluates to false.
Precedence Operator

OPERATORS PRECEDENCE
++,-- First
!, +,- (unary operators) Second
*, /, % Third
+, ‐ (binary operator) Fourth
<, <=, >=, > Fifth
==, != Sixth
&& Seventh
|| Eighth
= (assignment operator) Last
TYPE OF
SELECTION
CONTROL
STRUCTURE
Introduction

• You use the selection control structure or decision control structure


when you want program to make a decision or comparison and then
select one of two paths, depending on the result of that decision or
comparison.
Example 1 Example 2
If (it is raining) If (you have a test tomorrow)
wear a rain coat study tonight
Else
watch a movie
Types of Selection Structure

1. ONE‐WAY SELECTION
a. Simple selection with null false branch (null ELSE statement)

2. TWO‐WAYS SELECTION
a. Simple selection (if‐else statement)
b. Combined selection
i. && (AND)
ii. || OR

3. MULTI‐WAYS (MULTIPLE) SELECTION


a. Linear nested if statement
b. Non‐linear nested if statement
c. Switch statement
One-way Selection (if)

▪ The null ELSE structure is a variation of the simple IF structure. It


used when a task is performed only when a particular condition is
true.

▪ If the condition is false, then no processing will take place and the
IF statement will be passed.

▪ Syntax: if (expression)
Statement
One-way Selection (if)

• Pseudocode:
If (expression) then
statements
endif

• Flowchart:

True
Expression Statements

False
One-way Selection (if)

How it works??
One-way Selection (if)

How it works??
One-way Selection (if)

Example

If the speed of the car is greater than 80km/h, then output a message “you are too fast!”

Start
Begin
input speed
Input
if speed > 80 then speed
output “you are too fast!”
endif T
speed>80 You are
End too fast
F

End
One-way Selection (if)

C++
#include <iostream.h>

void main()
{
int speed;

cout<<“Please enter speed”<<endl;


cin >> speed;

if (speed > 80)


cout << “you are too fast!”;
}
One-way Selection (if)

Exercise: What is the #include <iostream.h>


output of the program if
input data is: void main()
- 90 {
- 20 int mark;

cin >> mark;

if (mark >= 50)


cout << “PASSED!”;
}
One-way Selection (if)

Exercise: What is the #include <iostream.h>


output of the program if void main()
input data is:
- 30 {
- 70 int mark;

cin >> mark;

if (mark >= 50)


cout << “PASSED!”;
cout<< “thank you for using the program”<<endl;

}
One-way Selection (if) – Compound
Statement

• Use the symbols { and } to group several statements as a single unit.


• Simple statements within the compound statements end with
semicolons.
• Compound Statements are sometimes called a statement block.
• Use compound statements in an if statement if you want several
actions executed when the Boolean expression is true.
One-way Selection (if) – Compound
Statement
void main() void main()
{ {
int mark; int mark;

cin >> mark;


cin >> mark;

if (mark >= 50)


if (mark >= 50)
{
cout << “PASSED!”; cout << “PASSED!”;
cout<< “thank you”<<endl; cout<< “thank you”<<endl;

} }
Single statement
}
compound statements
One-way Selection (if)
To be continued…

You might also like