Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

An Introduction to

Programming though C++


Krishna. S
Ch. 6: Conditional Execution
Jan 23, 2018
plusplusplusplus
• The difference between y++ and ++y
• x=y++ : evaluate variable y, and then increment y
obtaining a value
• (y++)++ is treated as incrementing a value, not a
variable. Likewise for ++(y++).
• When you do x=++y, you increment the variable y, and
evaluate y (which is now y+1), and assign this to x
• ++(++y) is treated as incrementing a variable, since
the result of the first ++ is already assigned to y.
Likewise for (++y)++.
So far
• simple graphics, variables, data types

• Assignments, operations on variables/


expressions

• Mixing data types in expressions (what happens?)

• Precision needed versus data type

• Simple programming paradigms (repeat loops, assignments inside a repeat loop)

• 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)

• Unlike repeat which can only iterate a fixed number


of times, we will look at loops/blocks which execute
based on some conditions

• We will see some example programs which use these


programming paradigms.
Let us calculate income tax
Write a program to read income and print income tax,
using following rules.
• If income ≤ 1,80,000, then tax = 0.
• If income is between 180,000 and 500,000 then tax =
10% of (income - 180,000).
• If income is between 500,000 and 800,000, then tax =
32,000 + 20% of (income – 500,000).
• If income > 800,000, then tax = 92,000 + 30% of
(income – 800,000).
Cannot write tax calculation program using what you
have learnt so far.
An even simpler problem
Using the rules given earlier, read in the
income of an individual and print a message
indicating whether or not the individual
owes tax.
• Even this simpler problem cannot be done
using what you have learned so far.
• New statement needed: if statement.
Outline
• Basic If statement
– Program to solve the simple problem
• If-else statement
– Better program to solve the simple problem
• Most general if statement form
– How to express complex conditions
– Tax calculation program
– A different way to control the turtle
• The switch statement (not today!)
– Yet another way to control the turtle
• Logical data
Basic if statement
• Form:
if (condition) consequent
• condition: “boolean” expression.
• “boolean” : Should evaluate to “true” or “false”.
• consequent: C++ statement, e.g. assignment.
• If condition evaluates to true, then the
consequent is executed.
• If condition evaluates to false, then
consequent is ignored.
Conditions
• Simple condition = exp1 relop exp2
• relop : relational operator: <, <=, ==, >,
>=, !=
• Operators respectively mean less than, less
than or equal, equal, greater than, greater
than or equal, not equal.
• Condition is considered true if exp1 relates
to exp2 as per the specified relational
operator relop.
Flowchart
• Pictorial representation of a program.
• Statements put inside boxes.
• If box C will possibly be executed after box B,
then put an arrow from B to C.
• Specially convenient for showing conditional
execution, because there can be more than
one “next” statements.
• “Diamond” shaped boxes are used for
condition checks.
“Flowchart” of if
Previous Statement

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

tax = (income −180000)


* 0.1;
False True
income <= 800000

tax = 92000 + tax = 32000 +


(income − 800000)*0.3; (income − 32000)*0.2;

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

• We will write a program which reads


commands from the user, and accordingly
controls the turtle.
– ‘f’ : turtle should execute forward(100).
– ‘r’ : turtle should turn right(90).
– ‘l’ : turtle should turn left(90).
– Stop after 100 commands are executed.
The program
main_program{
char command;
turtleSim();

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

You might also like