Professional Documents
Culture Documents
CH 6
CH 6
• Blocks and life times of variables (how does this help in efficient memory usage?)
Moving Ahead
• More complicated loops (more like C++ and not
simplecpp)
True
Condition
Consequent
False
Next Statement
Program for simple problem
main_program{
float income, tax; cin >> income;
if(income <= 180000)
cout << “No tax owed.” << endl;
if(income > 180000)
cout << “You owe tax.” << endl;
}
// Always checks both conditions.
// If the first condition is true,
// then you know second must be false,
// and vice versa. Cannot be avoided
// using just the basic if statement
Another form of if
if (condition) consequent
else alternate
• The condition is first evaluated. If it is
true, then consequent is executed. If
condition is false, then alternate is
executed.
If else flowchart
Previous Statement
False True
Condition
Alternate Consequent
Next Statement
Better program for simple problem
main_program{
float income, tax; cin >> income;
if(income <= 180000)
cout << “No tax owed.” << endl;
else
cout << “You owe tax.” << endl;
}
// Only one condition check. Thus
// more efficient than previous.
Most general form
if (condition1) consequent1
else if (condition2) consequent2
…
else if (conditionn) consequentn
else alternate
• Evaluate conditions in order.
• Some condition true: execute corresponding
consequent. Do not evaluate subsequent conditions.
• All conditions false: execute alternate.
General if example flowchart (with 3
conditions)
Previous Statement
False True
Condition 1
Consequent 1
False True
Condition 2
Consequent 2
False True
Condition3
Alternate Consequent 3
Next Statement
Tax calculation program
main_program{
float tax,income; cin >> income;
if (income <= 180000) tax = 0;
else if(income <= 500000)
tax = (income – 180000) * 0.1;
else if(income <= 800000)
tax = (income – 500000) * 0.2 + 32000;
else tax = (income – 800000) * 0.3 + 92000;
cout << tax << endl;
}
Read income
False True
income <= 180000
tax = 0;
False True
income <= 500000
print tax
More complex conditions
• condition1 && condition2 : true only if both
true. “AND”
• condition1 || condition2 : true only if at least
one is true. “OR”
• ! condition : true if only if condition is false.
• Components of complex conditions may themselves be
complex conditions, e.g.
!((income < 18000) || (income > 500000))
• Exercise: write tax calculation program using simple if
statement, but using complex conditions if needed.
– Book gives the answer.
Remark
• The consequent in an if statement can be a block
containing several statements. If condition is true, all
statements in the block are executed, in order.
• Likewise the alternate.
• Example: If income is greater than 800000, then both
the statements below get executed.
if (income > 800000){
tax = 92000 + (income – 800000)*0.3;
cout << “In highest tax bracket.\n”;
}
• “\n” : Newline character. Another way besides endl to
insert newline into output.
A different way to control the turtle
repeat(100){
cin >> command;
if (command == 'f') forward(100);
else if (command == 'r') right(90);
else if (command == 'l') left(90);
else cout << "Not a proper command, "
<< command << endl;
}
}
What happens?
If (a>0)
If (b>0) c=5; else c=6;
Will you get
If (a>0)
{If (b>0) c=5; else c=6;} or
If (a>0)
{If (b>0) c=5;} else c=6;
What happens?
#include <simplecpp>
main_program{
int z = 4;
if (++z == 5)
cout << "X\n";
if (z++ == 6)
cout << "Y\n";
if (z == 6)
cout << "Z\n";
}
What happens?
main_program{
int x, y;
x = 3; y = 5;
if ( x * y + x * (x - y) > x + y )
{
if ( x - (-y) * x < x - (x * y) )
cout << "A\n";
else
cout << "B\n";
}
else
{
if ( x * y + (x * x) - y < y * y - x * x)
cout << "C\n";
else
cout << "D\n";
}
}
What happens?
main_program{
int i, j;
i = 3;
j = 4;
if (i+j*2 > 7)
if (i+4 == j+3)
cout << "X\n";
else
cout << "Y\n";
else if (i > j)
cout << "Z\n";
}
End of Topics for Quiz 1
• simplecpp
• variables, assignments, expressions
• repeat loop
• conditionals if-then-else
• Next tuesday Jan 30: problem solving in
class before Quiz 1 on friday Feb 2