CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August 5, 2011
Overview Computational problems requiring conditional evaluation Flow charts Basic control structures in C++ for conditional execution if statement if else structure Conditional expressions if else if ladder structure switch statement Labs Next week Lectures next week General announcement 2
Flow charts Flow charts are used to graphically show the sequence of execution of our instructions. Consider the following program segment for adding two numbers cin >> a; cin >> b; sum = a+b; cout << sum; 3
Flow chart for adding two numbers Input a Input b sum = a + b Output sum 4
Flow chart... It is useful to graphically show the sequence of execution of our instructions. Flow charts are often used for this purpose Input a Input b sum = a + b Output sum In flow charts, we do not have to write instructions in c++, (although we can) Purpose is to show the flow of control of the algorithm of our programs 5
A simple computational problem Starting from a point, a person walks at a steady speed of 2 Km per hour for 1 hour. Write a program to determine the distance traveled by the person in some given time t. Distance Time Formula for calculating distance, is clearly: d = 2 * t 6
C++ program to solve the problem /* Program to calculate distance travelled in a given time CS101, Autumn 2011-12*/ #include <iostream> using namespace std; int main() { float time, distance; cout << give value of time\n ; cin >> time; distance = 2.0 * time; cout << Distance travelled is: << distance << endl; return 0; } 7
A problem requiring conditional execution Starting from a point, a person walks at a steady speed of 2 Km per hour for 1 hour. The person then starts running at a speed of 4 Km per hour. He continues to run for 2 hours and then stops. Write a program to determine the distance traveled by the person in some given time t. Distance Time 8
Another problem requiring conditional execution Cost of a ticket for a bus journey between two towns Rs 25.50. However, for a child of age less than 12 years, it is Rs 12.75. Given the age of the passenger, write a program to calculate the cost of a ticket. int age; float ticket_cost; cout << give age of the passenger: ; cin >> age; How do we write instruction to calculate the correct cost? 9
Cost of a ticket... In the case of the problem of computing cost of ticket, we want to instruct the machine, to calculate as follows: If the age is less than 12 years then calculate the cost of ticket as Rs 12.75 else Calculate the cost of the ticket as Rs 25.5 In our program, We need to execute either the first or the second assignment operation based on the value of age, but never both 10
Cost of a ticket... Thus, for computing the cost of ticket, we do not want to execute our instructions in a simple sequence We first want to the machine to examine the value of age. Then depending on whether the age is < 12 or not, We want the computer to execute either the first or the second instruction but never both This conditional execution of either the first or the second assignment is possible using the if instruction of c++ 11
Flow chart for calculating ticket_cost (tc) cin >> age False age < 12? True tc = 25.50 tc = 12.75 cout << tc 12
if statement in C++ C++ provides a control structure to enforce conditional execution General format if (condition) statement1; else statement2; Condition in parentheses must be a valid comparison of values (count <= N) (new_value old_value < 1.0E-5) (age >= 12) (age < 12) First, the condition is evaluated as either true or false If the condition is true, statement1 is executed If the condition is false, statement2 is executed Both statements are never executed 13
General flow chart for an if statement 14
Bus ticket problem if (age < 12) ticket_cost = 12.75; else ticket_cost = 25.50; 15
Multiple actions within any true-false branch We should always use a C++ block Any set of c++ statements, enclosed in a pair of braces { } constitute a block, logically equivalent to a single statement Wherever we can write a statement, a block can be written 16
C++ Program // Program to calculate cost of bus ticket #include <iostream> using namespace std; int main() { int age; float ticket_cost; cout << give age of passenger: ; cin >> age; if (age < 12) {ticket_cost = 12.75}; else {ticket_cost = 25.50}; cout << cost of ticket is << ticket_cost; return 0; } 17
C++ Program // Program to calculate cost of bus ticket #include <iostream> using namespace std; int main() { int age; float ticket_cost; cout << give age of passenger: ; cin >> age; if (age >= 12) {ticket_cost = 25.5;} else {ticket_cost = 12.75;} cout << cost of ticket is << ticket_cost << endl; return 0; } 18
if else if ladder If additional Concession available to old people ( age > 60) Cost of ticket for them is Rs 20 Flow chart: 19
Modified program int age; float ticket_cost; cout << give age ; cin >> age; if (age > 60) {ticket_cost = 20;} else if (age >= 12) {ticket_cost = 25.50;} else {ticket_cost = 12.75;} cout << cost is << ticket_cost; 20
Alternate version of the program int age; float ticket_cost; cout << give age ; cin >> age; If (age < 12) {ticket_cost = 12.75;} else if (age < 60) {ticket_cost = 25.50;} else {ticket_cost = 20.00;} cout << cost is << ticket_cost; return 0; 21
The two versions int age; float ticket_cost; cout << give age ; cin >> age; if (age > 60) {ticket_cost = 20;} else if (age >= 12) {ticket_cost = 25.50;} else {ticket_cost = 12.75;} cout << cost is << ticket_cost; return 0; int age; float ticket_cost; cout << give age ; cin >> age; If (age < 12) {ticket_cost = 12.75;} else if (age < 60) else {ticket_cost = 25.50;} {ticket_cost = 20.00;} cout << cost is << ticket_cost; return 0; 22
Finding maximum of two numbers Given two integer numbers a and b, find the maximum of these Flow chart: 23
Program to find maximum of two numbers int a, b, max; cin >> a >> b; if (a > b) {max = a;} else {max = b;} cout << max; 24
Alternate logic 25
Two versions of program to find maximum // Version I int a, b, max; cin >> a >> b; if (a > b) {max = a;} else {max = b;} cout << max; // Version II int a, b, max; cin >> a >> b; max = a; if (b > max) {max = b;} cout << max; 26
Finding maximum of three numbers Given 3 integer numbers a, b, and c, find the maximum of these 27
Program // Program to find maximum of 3 given numbers int a, b, c, max; cin >> a >> b >> c; if (a > b) { if (b > c) { max = a;} else {if (a > c) {max = a;} else {max = c;} } } else { if (b > c) {max = b;} else { max = c;} } cout << max; 28
Maximum of three numbers alternate logic 29
Two program versions to find maximum of 3 numbers // Version I int a, b, c, max; cin >> a >> b >> c; if (a > b) { if (b > c) { max = a;} else {if (a > c) {max = a;} else {max = c;} } } else { if (b > c) {max = b;} else { max = c;} } cout << max; // Version II int a, b, c, max; cin >> a >> b >> c; max = a; if (b > max) {max = b;} if (c > max) {max = c;} cout << max; 30
Finding maximum of 5 numbers int a, b, c, d, e, max; cin >> a >> b >> c >> d >> e; max = a; if (b > max) {max = b;} if (c > max) {max = c;} if (d > max) {max = d;} if (e > max) {max = e;} cout << max; 31
Conditional expression A comparison of two values results in true or false These are called logical or Boolean values A comparion operation is also treated as an expression Evaluates to true or false C ++ has a special data type called bool C++ has a numerical equivalent for these two logical values false is converted to value 0 true is converted to value 1 32
Conditional expressions a op b where op is <, >, <=, >=, ==,!= == is equal to!= is not equal to Conditions can be combined: (a > 0) && (a <= 9) : true if a is a positive digit. && : conjunction. and : disjunction. or! : negation. not 33
Examples of conditions 34
The switch statement In our algorithms, we need to branch out to one of multiple different instructions. Branching decision is based on the value of an expression 35
Switch statement switch (expression){ case constant1 : statement sequence 1; break; case constant2 : statement sequence 2; break; - - default: statement sequence; } 36
A problem suited for use of switch 37
38