CONTROL STRUCTURES(SELECTION)
PROGRAM COMPONENTS
SEQUENCE Groups Of Sequential Steps
SELECTION Making Choices
IF-THEN (one way) IF-THEN-ELSE (two way or multi way)SWITCH (multi way)
ITERATION(Looping) Repeating Steps
WHILE – DO (top tested)DO – WHILE (bottom tested)FOR (fixed iteration)
BOOLEAN DATA TYPE
Two Values – true (1) or false (0) Example:
bool isOvertime = true;
cout << “Is overtime “ << isOvertime << endl;
isOvertime = false;
cout << “Is overtime “ << isOvertime << endl;
Internal program value Cannot be read in via keyboard entry Only displayed as 1 or 0
BOOLEAN DATA TYPE
Boolean names should reflect true valueFor example:
bool done = false;
bool even = false;
bool error = true;
Can be initialized with integer: Using 0 intializes to false Anything Else initializes to true
It is best to use values true or false
RELATIONAL / EQUALITY OPERATORS
Boolean expression is:A condition in which a relational operator tests therelationship between two values or expressions andreturns a boolean result (true or false)
Relational / Equality operators:Allow comparison of the size, magnitude or equality of data items that are the same or compatible types.
For example: less than ( < )…. 3 < 5 evaluates to true bool isLessThan = 3 < 5;
RELATIONAL / EQUALITY OPERATORS
Operator Meaning True Example False Example
< Less Than 3 < 5 4 < 2 or 6 < 6
<= Less Than OR Equal To 3 <= 4 or 3 <= 3 10 <= 5
> Greater Than 8 > 7 9 > 10 or 4 > 4
>= Greater Than OR Equal To
5 >= 4 or 5 >= 5 5 > = 6
== Equal To 20 == 20 10 == 11
!= Not Equal To 10 != 20 20 != 20
Evaluation of either a relational or equality operator always results to either true or false.
RELATIONAL / EQUALITY OPERATORS
Relational Operator Priority(<, <=, >, >=) Are below the arithmetic operator
Equality Operator Priority( ==, !=) Are below the the relational operators
LOGICAL OPERATORS
Logical Operators ( !, &&, || ):Allow the combination of boolean values according to the rules of mathematical logic. Logical expressions evaluate to a boolean result (true or false)
Not (!) Inverts the boolean value
true becomes false or false become true
LOGICAL OPERATORS
AND (&&) Evaluates to true only if both values are true
bool x;
bool y;
X && Y ==
True && True True
True && False False
False && True False
False && False False
LOGICAL OPERATORS
OR ( || ) Evaluates to false only if both values are false
bool x;
bool y;
X || Y ==
True || True True
True || False True
False || True True
False || False False
OPERATOR PRECEDENCE
All operator precedence:
BOOLEAN, RELATIONAL, EQUALITY AND LOGICAL OPERATIORS
Example:
bool flag1, flag2, flag3;
int num1, num2;
flag1 = (98 % 13 != 98 / 13) || (2 * 5 != 10);
cout << "The truth value of variable flag1 is " << flag1 << endl;
num1 = 12 + 3 * 7;
num2 = 10 + num1 % 3;
flag2 = (num1 > num2) && (num2 < 12);
cout << "The truth value of variable flag2 is " << flag2 << endl;
flag3 = flag1 || !flag2;
cout << "The truth value of variable flag3 is " << flag3 << endl;
SELECTION
MAKING CHOICES One-Way (choose to do or not to do)
if
Two-Way (choose to do one or the other)if – elseconditional operator
Multiple Selection(choose to do one of many)nested if - else switch
ONE-WAY SELECTION
SIMPLE if ( RELATIONAL EXPRESSION(s) )
STATEMENT;
COMPOUND if ( RELATIONAL EXPRESSION(s) )
{STATEMENT;STATEMENT;STATEMENT(s);
}
ONE WAY SELECTION
RULES1. The statement(s) is/are executed if
and only if the relational expression(s) evaluate(s) to true.
ONE WAY SELECTION
EXAMPLES:if (current_balance > 1000)
interest = current_balance * 0.015;
if (day == 7 && hours_worked > 40)
{
ot_pay = (hours_worked – 40) * pay_rate * 2;
total_pay = 40 * pay_rate + ot_pay;
}
ONE WAY SELECTION EXAMPLE:
int main()
{ bool isEven; int num;
cout << "Enter an integer "; cin >> num; cout << endl;
if (num > 10) cout << num << " is greater than 10" << endl;
isEven = (num % 2 == 0); if (isEven) cout << num << " is an EVEN number." << endl;
if (!isEven) cout << num << " is an ODD number." << endl;
system ("pause"); return 0;
}
TWO WAY SELECTION
SIMPLE if ( RELATIONAL EXPRESSION(s) )
Statement;
else
Statement;
TWO WAY SELECTION
COMPOUND if ( RELATIONAL EXPRESSION(s) )
{Statement;Statement(s);
}else{
Statement;Statement(s);
}
TWO WAY SELECTION
RULES1. First statement(s) will be executed if and
only if the evaluation of the relational expression(s) is/are true.
2. Second statement(s) will be excuted if and only if the evaluation of the relational expression(s) is/are false.
TWO WAY SELECTION
EXAMPLESchar employee;
string state;
float ot_pay, pay_rate, gross_pay;
float hours_worked, tax;
Example 1:
if (state == “CO”)
tax = 0.065;
else
tax = 0.05;
TWO WAY SELECTION
EXAMPLESExample 2:
if (employee == ‘E’ || employee == ‘S’)
gross_pay = 40 * pay_rate;
else
{
reg_pay = pay_rate * 40;
ot_pay = (hours_worked – 10) * pay_rate * 1.5;
gross_pay = reg_pay + ot_pay;
}
CONDITIONAL OPERATOR
Can Accomplish Two Way Selection Using The Conditional Operator (?:) expression1 ? expression2 : expression3
Examplestate == “CO” ? tax = 0.065 : tax = 0.05;
Cannot Include Compound Statements
Top Related