Beruflich Dokumente
Kultur Dokumente
Announcements
Monday lab scheduled on 15th August will instead be held on Saturday, 20th August. Monday lab scheduled on 22nd August will instead be held on Saturday, 27th August. Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September. Please approach your tutor for all lab related issues.
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1
Recap
Assignment statement
Type mismatch
If statement
if else
Relational operators
Lec-07
Acknowledgement
Many of the slides on conditional statement and loop have been borrowed from Dr. Arnab Bhattacharyas class notes of ESc101.
Lec-07
Recap: if - else
If your algorithm requires that you take some action if the condition is true, and some other action if the condition is false, then a more general statement is: if ( condition ) {
statements1 } else { statements2 }
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4
Meaning M i Equal to Less than Less than or Equal to Greater than Greater than or Equal to q Not equal to
Example E l count == 10 a<b low <= high age > 18 j >= 0 marks != 0
Lec-07
Lec-07
Multiple conditions
main () { int n; scanf (%d, &n); if (n < 0) printf (number is negative\n) else if (n > 0) printf (number is positive\n) else printf (number is zero\n); }
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8
Multiple else if
if (section == 1) printf (Tutorial Room: TB 101\n) else if (section == 2) printf (Tutorial Room: TB 102\n) else if (section == 3) printf (Tutorial Room: TB 103\n) else if (section == 4) printf (Tutorial Room: TB 104\n) else if (section == 5) printf (Tutorial Room: TB 105\n) else printf (Wrong Section\n);
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9
Nested if
if (condition1) if (condition2) statement1 else statement2
what will be executed when condition 1 is true and condition 2 is false condition 1 is false and condition 2 is true both conditions are true both conditions are false
Lec-07
10
Nested if
Confusion galore Rule: else is associated with nearest if which does not have any other associated else Dont worry about the rule Use braces and indentation for clarity and correctness
Lec-07
11
Loops
Print all numbers between 1 and 100 that are divisible by 7 Algorithm:
1. 2. 3. 4. 5. Initialize x to be 1. Check if x is divisible by 7 If yes, print the value of x Increment x (add 1 to x consider the next number) If x <= 100 go back to step 2
Lec-07
Loops
Loops are a set of statements that are repeated a number of times. Each time (called an iteration), some variable may change. There has to be some condition at which the loop will stop. (We dont want infinite loops.)
Lec-07
14
while statement
while (condition) { statements } Condition evaluates to true/false (boolean) Statements are executed as long as condition is true. Value of condition, if initially true, must change at some appropriate later point to false
Otherwise, we will have an infinite loop
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15
while statement
Print all numbers between 1 and 100 that are divisible by 7 x = 1; while (x <= 100) { (( ) ) if ((x % 7) == 0) printf (%d , x); x = x + 1; }
Lec-07 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 16
Any Questions?
Lec-07
17