You are on page 1of 8

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #8, August 09, 2011

Please switch off your mobile phones.

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-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1

Recap
If statement
Multiple conditions Nested if

Loops
while statement

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Acknowledgement
Many of the slides on loops (both while and for statements and the examples) have been borrowed from Dr. Arnab Bhattacharyas class notes of ESc101.

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: 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-08

Statements 2-5 are being repeated a number of times.


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Recap: 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-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Recap: 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-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

for statement
for (initialization ; condition; update) { statements } Condition evaluates to true/false (boolean) Statements in the loop are executed as long as condition is true. initialization sets the initial values of variables update changes the value of variables d h h l f i bl Value of condition, if initially true, must change at some appropriate later time to false,
Otherwise, infinite loop is created.
Lec-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

for loop
Print all numbers between 1 and 100 that are divisible by 7 for (x = 1; x <= 100; x = x + 1) { if ((x % 7) == 0) p printf (%d , x); ( ) }

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Equivalence of while and for statements


for (initialization; condition; update) { statements; } is equivalent to initialization; while (condition) { statements; update; }
Lec-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Example: Factorial
int i, n, factorial = 1; i n scanf (%d, &n); for (i = 1; i <= n; i = i + 1) { factorial = factorial * i; } printf (Factorial of %d is %d\n, n, factorial);

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Example: Factorial
int i, n, factorial = 1; i n scanf (%d, &n); i = 1; while (i <= n) { factorial = factorial * i; i = i + 1; } printf (Factorial of %d is %d\n, n, factorial);
Lec-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Logical or Boolean operators


Operator p ! && || Meaning g NOT AND OR Example p !(5 <= 2) ((1<2)&&(10<8)) ((10<=20)||(!(1<2))) Value True False True

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Example: power
Compute xy Assume y to be natural number. answer = 1; for ( i = 1; i <= y; i = i + 1) { answer = answer * x; }

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

Example: geometric progression


Given a geometric p g g progression with first term a and a common ration r, print the first n terms. term = a; i = 1; while (i <= n) { printf (%f , term); term = term * r; i = i + 1; }
Lec-08 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

Any Questions?

Lec-08

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

You might also like