Upcoming SlideShare
×

# ICP - Lecture 9

363 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
363
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
20
0
Likes
0
Embeds 0
No embeds

No notes for slide

### ICP - Lecture 9

1. 1. CSC 103 Lecture 9 Introduction to Computers and Programming
2. 2. Loop Control Instructions  When an activity needs to be performed more than once, then the mechanism that meets this need is called Loop  Loop is defined as to perform set of instructions repeatedly  Three major loop structures in C.  The for loop  The while loop  the do-while loop (also called cousin of while loop) 2
3. 3. The for Loop  When an activity needs to be performed a fixed number of times then The for loop is used in such cases.  For example to calculate the paychecks for 120 employees or printout the squares of all the numbers from 1 to 50 etc.  In all such cases and other related the for loop is best suited 3
4. 4. General form of for loop Statement 4 for ( initialize counter ; test counter ; increment counter ) { do this ; and this ; and this ; }
5. 5. for loop Flow Chart Structure 5
6. 6. Some valid for loop expressions 6  for ( i = 10 ; i ; i -- ) printf ( " %d", i ) ;  for ( j=0 ; j < 5 ; j++ ) printf ( " %d", j ) ;  for ( i = 1; i <=10 ; printf ("%d",i++ ) )  for ( scanf ( "%d", &i ) ; i <= 10 ; i++ ) printf ( "%d", i ) ;
7. 7. The for Loop 7 main() { int count; for (count=0; count<10; count++) printf ("count=%dn", count); } Output: count=0 count=1 count=2 count=3 count=4 count=5 count=6 count=7 count=8 count=9
8. 8. Structure of the for Loop for (count=0; count<10; count++)  Parentheses following keyword for contain what we will call the “loop expression”  Loop expression is divided by semicolons into the following three separate expressions:  the “initialize expression” i.e count=0  the “test expression” i.e count<10  the “increment expression” i.e count++  The count variable has a key role, it is used to control the operations of the loop 8
9. 9. Structure of the for Loop 9 the “initialize expression”  count=0, initialize the count variable  It is executed as soon as loop is entered  It can start from any given number  However, loops often start at 1
10. 10. Structure of the for Loop 10 the “test expression”  count<10, tests each time to see the count value  It makes use of relational operator (in this case <)  The loop will be executed till the test expression becomes false  When it become false the loop will be terminated  and control will pass to next statement following loop
11. 11. Structure of the for Loop the “increment expression”  count++ (same as count=count+1), it increments the variable count each time loop is executed  It make use of increment operator i.e. ++  It should be noted that it will not only be incremented but can also be decremented as well. 11
12. 12. The Body of the for Loop  Following the keyword for and the loop expression (as discussed above) is the body of the loop, i.e. the statement (or statements) that will be executed each time round the loop. i.e. in our example, only one statement: printf ("count=%dn", count);  Note that in a for loop don’t place semicolon between loop expression & body of loop, since the keyword for & loop expression don’t constitute a complete C statement For Example: for (count=0; count<10; count++) ; 12
13. 13. Operations of the for Loop  1st initialization expression executed then  2nd test condition examined  If False, the body of the loop will not be executed  If True, the body of the loop be executed  3rd increment expression executed  Note that printf will be executed before increment so the 1st value will be printed “0”  Process will continue till test expression become false. 13
14. 14. Multiple statement in the for Loop  In our previous example only one statement is used in the body of the loop i.e printf ("count=%dn", count);  However, two or more than two statements can also be used in a loop 14
15. 15. Multiple statement in the for Loop 15 main() { int count, total; for (count=0, total=0; count<10; count++) { total = total + count; printf ("count=%d, total=%dn", count, total); } } Output: count=0, total=0 count=1, total=1 count=2, total=3 count=3, total=6 count=4, total=10 count=5, total=15 count=6, total=21 count=7, total=28 count=8, total=36 count=9, total=45
16. 16. Multiple statement in the for Loop  Two points to remember:  The whole package i.e. the opening brace, the statements, and closing brace, is a single C statement. Often called “Compound Statement” or “block”  Each statement within the block is also a C statement and must be terminated with a semicolon as in the usual way  We can also use total += count; it is same as total = total + count; 16
17. 17. Multiple Initialization in for Loop 17  In for loop, we have initialized multiple variables  These variables are separated by comma “,” count=0 and total=0 for (count=0, total=0; count<10; count++)  in this example we really didn’t need to initialize total within the loop, we could also have done: total=0; for (count=0; count<10; count++)  we can also increment two or more variables at the same time  similarly, we can have multiple conditions for (i=10,j=0; i>0,j<10; i--,j++)
18. 18. Exercise 18  Input a number from user and print its table  Program output should look like: Enter a number: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
19. 19. 19 main() { int a; printf("Enter a number: "); scanf("%d", &a); int i; for (i=1; i<=10; i++) { printf ("%d x %d t= %dn", a, i, a*i); } }
20. 20. Change Exercise a bit 20  Input a number from user to print its table. Also, ask user to enter how many times you want to generate the table.  Program output should look like: Enter a number: 2 Enter number of times: 5 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10