Structured Programming
Decision Making
1
Objectives
• Define structured programming and why we use it
• Describe the control structures used in structured
programming (sequence, selection and repetition)
• Advantages of using structured programming
• Develop conditionals used in selection and repetition
structures
• Develop structured programs using decision structures
2
Introduction
•Normally, a program executes statements
from first to last
•changing which statements run and when,
depending on the circumstances leads to a
better program
•The ability to alter the order in which a
program's statements are executed, the
control flow
3
Structured Programming
• Structured programming is a programming paradigm
(or a culture, or ways of programming) aimed on
improving the clarity, quality, and development time
of a computer program by making extensive use of
subroutines, block structures and for and while
loops—in contrast to using simple tests and jumps
such as the goto statement which could lead to
"spaghetti code" which is both difficult to follow and
to maintain
4
Control Structures
•portions of program code that contain
statements within them and depending on
the circumstances, execute these statements
in a certain way
•Three control structures used to implement
structured programming are
•Sequence, Selection (Decision), Repetition
(Loops/Iteration)
5
Advantages of Structured Programming
• Application programs are easier to read and understand
• Application programs are less likely to contain logic errors
• Errors are more easily found
• Higher productivity during application program development
• Improved application program design
• Application programs are more easily maintained
6
Selection (Decision)
• In order for a program to change its behaviour
depending on the input, there must be a way to test
that input.
• Condition/Boolean expression (true OR false)
• Selection allow the program to check the values of
variables and to execute (or not execute) certain
statements
• C++ has if, switch-case and ? selection structures
7
Operators (Relational and logical)
•These are used to determine whether
some condition is true or false.
8
Relational Operators
•Are used to compare numeric and char
values to determine relative order
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
9
Relational Expressions
•Relational expressions are Boolean (i.e.,
evaluate to true or false)
•Examples:
12 > 5 is
7 <= 5 is
7 <= 7 is
10
Relational Expressions
•if x is 10, then
• x == 10 is
• x <= 8 is
• x != 8 is
• x == 8 is
11
Relational Expressions
•The value can be assigned to a variable
• bool result = (x <= y);
//Assigns 0 for false, 1 for true
•Do not confuse = (assignment) and ==
(equal to)
12
Hierarchy of Relational Operators
Operator Precedence
> >= < <= Highest
== != Lowest
Use this when evaluating an expression that
contains multiple relational operators
13
Logical Operators
used to create relational expressions from other
relational expressions
Operator Meaning Explanation
New relational expression is true if
&& AND both expressions are true
New relational expression is true if
|| OR either expression is true
Reverses the value of an expression;
! NOT true expression becomes false, false
expression becomes true
14
Logical Operators
•a b a && b
•true true true
•true false false
•false true false
•false false false
15
Logical Operators
•a b a || b
•true true true
•true false true
•false true true
•false false false
16
Logical Operators
•a !a
•true false
•false true
17
Logical Operator Examples
int x = 12, y = 5, z = -4;
•
(x > y) && (y > z)
(x > y) && (z > y)
(x <= z) || (y == z)
(x <= z) || (y != z)
!(x >= z)
4-18
Logical Precedence
Highest !
&&
Lowest ||
Example:
(2 < 3) || (5 > 6) && (7 > 8)
is true because AND is evaluated before OR
19
More on Precedence
Highest arithmetic operators
relational operators
Lowest logical operators
Example:
8 < 2 + 7 || 5 == 6 is true
The if Statement
•if (condition)
•{
• statement1
• Statement2
• …
•}
21
The if Statement
• int x=8,y=5;
•if (x > y )
• {
• cout << "x is greater than y \n";
• }
22
The if Statement
• int x=5,y=8;
•if (x > y )
• {
• cout << "x is greater than y \n";
• }
23
The if/else Statement
•Allows a choice between statements
depending on whether (condition) is true or
false
24
The if/else Statement
• if (condition)
{
statement set 1;
}
else
{
statement set 2;
}
25
Example if/else Statements
if (score >= 50)
cout << "You passed.\n";
else
cout << "You did not pass.\n";
if (intRate > 0)
{
interest = loanAmt * intRate;
cout << interest;
}
else
cout << "You owe no interest.\n";
26
if vs. if/else
If there are two conditions and both of them can be
true or both can be false, then use two if
statements:
if (num > 0)
cout << num << " is positive\n";
if (num %2 == 0)
cout << num << " is even\n";
27
if vs. if/else
If the two conditions cannot both be true, then
a single if/else statement can work:
if (num %2 == 0)
cout << num << " is even\n";
else
cout << num << " is odd\n";
28
The if/else if Statement
•This is a chain of if statements that test in
order until one is found to be true
•This also models thought processes
“If it is raining, take an umbrella,
else, if it is windy, take a hat,
else, if it is sunny, take sunglasses.”
29
if/else if Syntax
if (condition 1)
{ statement set 1;
}
else if (condition 2)
{ statement set 2;
}
...
else if (condition n)
{ statement set n;
} 30
Using a Trailing else
•Is used with a set of if/else if statements
•It provides a default statement or action that
is performed when none of the conditions is
true
•It can be used to catch invalid values or
handle other exceptional situations
31
Example if/else if with Trailing else
if (age >= 21)
cout << "Adult";
else if (age >= 13)
cout << "Teen";
else if (age >= 2)
cout << "Child";
else
cout << "Baby";
32
Nested if Statements
• An if statement that is part of the if or else part of another if
statement
• This can be used to evaluate > 1 data item or to test > 1
condition
if (score <= 100)
{
if (score > 90)
grade = 'A';
} 33
Notes on Coding Nested ifs
•An else matches the nearest previous if that
does not have an else
if (score < 100)
{
if (score > 90)
grade = 'A';
else ... // goes with second if not first one
}
else
•Proper indentation aids understanding
34
The Conditional Operator ?
•This can be used to create short if/else
statements
•Syntax: expr ? expr : expr;
•mark>=50 ? cout<<"Passed" :
cout<<"Failed";
35
The Value of a Conditional Expression
int num = 13;
string result= (num%2 ==0) ? "even" : "odd";
cout << num << " is " << result;
36
The switch Statement
•uses the value of an integer expression to
determine the statements to execute
•It may sometimes be used instead of if/else if
statements
37
switch Statement Syntax
switch (IntExpression)
{
case exp1: statement set 1;
case exp2: statement set 2;
...
case expn: statement set n;
default: statement set n+1;
}
38
switch Statement Requirements
1) IntExpression must be an integer variable or
a char,or an expression that evaluates to an
integer value
2) exp1 through expn must be constant integer
type expressions and must be unique in the
switch statement
3) default is optional but recommended
39
The break Statement
•Is used to stop execution in the current block
•It is also used to exit a switch statement
•It is used to execute a single case statement
without executing statements following it
40
Example 1 switch Statement
switch (gender)
{
case 'f': cout << "female";
break;
case 'm': cout << "male";
break;
default : cout << "invalid gender";
}
41
Example 2 switch Statement
switch (option)
{
case 1: cout << "You chose Addition";
break;
case 2: cout << "You chose Subtraction";
break;
case 3: cout << “You chose Multiplication ";
break;
default : cout << “You selected an invalid option";
}
42
Exercise 4 +LAB 5
•(assume x = 6 and y = 2):
•!(x > 2)
•(x > y) && (y > 0)
•(x < y) && (y > 0)
•(x < y) || (y > 0)
43