Selection Control Structure: Relational Operator Boolean Value
Selection Control Structure: Relational Operator Boolean Value
Mahfudzah Othman
UiTM Perlis
CHAPTER 3
SELECTION CONTROL STRUCTURE
Boolean Values
Beside the arithmetic values we have the Boolean values TRUE (1) or FALSE
(0).
Relational Operators
There are 6 relational operators that support logical relationships.
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).
OPERATOR MEANING
< Less than
<= Less than or equal
> Greater
>= Greater than or equal
== Equal
!= Not equal
Logical Operators
C++ has three logical operators for combining logical values and creating
new logical values.
OPERATOR OPERATION
! Logical NOT
&& Logical AND
|| Logical OR
1
CSC128
Mahfudzah Othman
UiTM Perlis
It is a unary operator.
It changes a TRUE value (non zero) to FALSE (zero) and FALSE value
(zero) to TRUE (1).
X !X
F T
T F
X !X
Zero 1
Non Zero 0
It is a binary operator.
There are 4 distinct possible combinations of values in its operands.
Result: If both operands are TRUE, so TRUE and FALSE in all other
cases.
X Y X && Y
F F F
F T F
T F F
T T T
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1
2
CSC128
Mahfudzah Othman
UiTM Perlis
It is a binary operator.
There are 4 distinct possible combinations of values in its operands.
Result: If both operands are FALSE, so FALSE and TRUE in all other
cases.
X Y X || Y
F F F
F T T
T F T
T T T
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1
Logical Priority
Operation Priority
() 1
! 2
&& 3
|| 4
Operation Priority
() 1
! 2
*, /, % 3
+, - 4
<, <=, >, >=, ==, != 5
&& 6
|| 7
3
CSC128
Mahfudzah Othman
UiTM Perlis
Example 1:
Example 2:
If (x!= 0)
y = 3;
else
z = 2;
Run: y =3
4
CSC128
Mahfudzah Othman
UiTM Perlis
SELECTION STRUCTURE
One-way Selection
Syntax: if (condition)
Statement 1;
Flowchart:
TRUE
conditio Statement 1
n
FALSE
Example: Suppose the passing grade of an exam is 50. Develop the algorithm
and write the program to solve the problem.
Algorithm:
Pseudocode:
1. Start
2. Input grade
3. If grade>50, print pass.
4. End
5
CSC128
Mahfudzah Othman
UiTM Perlis
Flowchart:
Start
Input grade
TRUE
grade>5 Print pass
0
FALSE
End
Programming:
void main()
{
float grade;
if (grade>50)
cout<<”You have passed the exam”;
}
Two-way Selection
The basic decision statement in the computer is the two ways selection. The
decision is describe to the computer as a conditional statement that can be
answered either TRUE or FALSE.
If the answer is TRUE, one/more action statement are executed. If the answer is
FALSE, then the different action/set of actions is executed.
Regardless of which set of actions are executed, the program continues with the
next statement after the selection statement.
6
CSC128
Mahfudzah Othman
UiTM Perlis
Syntax: if (expression)
Statement 1;
else
Statement 2;
Flowchart:
TRUE
expressi Statement 1
on
FALSE
Statement 2
Example: Develop the algorithm and write a program that accepts two integers
and determine which integer is bigger between them.
7
CSC128
Mahfudzah Othman
UiTM Perlis
Algorithm:
Pseudocode:
1. Start
2. Input two integers; no1 and no2.
3. If (no1>no2)
print “Number 1 is bigger then Number 2”
else
print “Number 2 is bigger than Number 1”.
4. End
Flowchart:
Start
TRUE
If
(no1>no Number 1 is bigger than
2) Number 2
FALSE
End
8
CSC128
Mahfudzah Othman
UiTM Perlis
Programming:
void main()
{
int no1, no2;
if (no1>no2)
cout<<”Number 1 is bigger than Number 2”;
else
cout<<”Number 2 is bigger than Number 1”;
}
Multiway Selection
For the if…else, the statement may be any statement, including another if…else.
There is no limit as to how many levels can be nested, but if there are more than
three, they can become difficult to read.
Syntax: if (expression 1)
If (expression 2)
Statement 1;
else
Statement 2;
else
Statement 3;
9
CSC128
Mahfudzah Othman
UiTM Perlis
Flowchart:
FALSE
FALSE
Statement 2
Statement 3
Example: Develop the algorithm and write a program that accepts two integers
and determine whether the integers are equal or one of them is smaller and
bigger than the other.
Algorithm:
Pseudocode:
1. Start
2. Input two integers; no1 and no2.
3. If (no1>=no2)
If (no1>no2)
print “Number 1 is bigger than Number 2”
else
print “These numbers are equal”.
else
print “Number 2 is bigger than number 1”
4. End
10
CSC128
Mahfudzah Othman
UiTM Perlis
Flowchart:
Start
TRUE FALSE
If If
(no1>=no (no1>no2 These numbers are
2) ) equal
FALSE
TRUE
End
Programming:
void main()
{
int no1, no2;
if (no1>=no2)
if (no1>no2)
cout<<”Number 1 is bigger than Number 2”;
else
cout<<”These numbers are equal”;
else
cout<<”Number 2 is bigger than Number 1”;
}
11
CSC128
Mahfudzah Othman
UiTM Perlis
Compound Statements
Example:
void main ()
{
int x, y;
cout<<”Enter two integers:”;
cin>>x>>y;
if (x>y)
{
int temp = x; compound
statement
x = y;
y = temp;
}
cout<<x<<” ”<<y<<endl;
}
Switch Statements
12
CSC128
Mahfudzah Othman
UiTM Perlis
Flowchart:
Multi
way
selection
The switch expression contains condition that is evaluated. For every possible
value that can result from the condition, a separate case constant is defined.
There must be at least one case statement. Each case expression is associated
with a constant.
The case expression is followed by a colon (:) and then the statement with which
it is associated. There may be one or more statements for each case.
If you do not provide a default, the computer will simply continue with the
statement after closing brace in the switch.
If we want to execute only one of the cases, we must use break statement. The
break statement causes the program to jump out of the switch statement.
Two or more case expression values can belong to the same statement.
13
CSC128
Mahfudzah Othman
UiTM Perlis
Example: Write a program using switch statement that will displays numbers
between 1 to 3.
Programming:
void main()
{
int no;
switch (no)
{
case 1 : cout<<”Number: 1”;
break;
case 2 : cout<<”Number: 2”;
break;
case 3 : cout<<”Number: 3”;
break;
default : cout<<”Sorry…please enter 1-3 only”;
break;
}
}
14