Decision Making
Decision Making
Examples:
int a =20;
float avg =2.13;
“getch( )“ function
Syntax: getch();
Decision Constructs
A statement used to control the flow of execution in a
program is called control structure.
The instructions in a program can be organized in three
kinds of control structures to control execution flow.
Decision
Terminal
Input/Output
Operation Connector
Process Module
Review
Sequence structure
13
Review
15
Review
17
The Basic If Statement
Syntax
if (Expression)
Action
Expression
if (Value < 0) {
Is our number negative?
Value = -Value;
}
If Value is less than Value < 0
zero then we need to
update its value to
true false
that of its additive
inverse If Value is not less
than zero then our
Value = -Value number is fine as is
Our number is
now definitely
nonnegative
The If-Else Statement
Syntax
if (Expression)
Action1
else
Action2 Expression
If Expression is true then execute
Action1 otherwise execute Action2 true false
if (v == 0) {
Action1 Action2
cout << "v is 0";
}
else {
cout << "v is not 0";
}
Finding the Max
Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set No, its not. So Value1
to Value2 is at least as large as
Value2. In this case,
Value1 < Value2 Max is set to Value1
true false
if ( number < 0 ){
cout << number << " is negative" << endl;
}
else if ( number > 0 ) {
cout << number << " is positive" << endl;
}
else {
cout << number << " is zero" << endl;
}