ICP - Lecture 9

20
CSC 103 Lecture 9 Introduction to Computers and Programming

description

 

Transcript of ICP - Lecture 9

Page 1: ICP - Lecture 9

CSC 103

Lecture 9

Introduction to Computers and Programming

Page 2: ICP - Lecture 9

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

Page 3: ICP - Lecture 9

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

Page 4: ICP - Lecture 9

General form of for loop Statement

4

for ( initialize counter ; test counter ; increment counter )

{

do this ;

and this ;

and this ;

}

Page 5: ICP - Lecture 9

for loop Flow Chart Structure

5

Page 6: ICP - Lecture 9

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

Page 7: ICP - Lecture 9

The for Loop

7

main()

{

int count;

for (count=0; count<10; count++)

printf ("count=%d\n", count);

} Output: count=0 count=1 count=2 count=3 count=4 count=5 count=6 count=7 count=8 count=9

Page 8: ICP - Lecture 9

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

Page 9: ICP - Lecture 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

Page 10: ICP - Lecture 9

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

Page 11: ICP - Lecture 9

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

Page 12: ICP - Lecture 9

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=%d\n", 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

Page 13: ICP - Lecture 9

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

Page 14: ICP - Lecture 9

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=%d\n", count);

However, two or more than two statements can also be used in a loop

14

Page 15: ICP - Lecture 9

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=%d\n", 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

Page 16: ICP - Lecture 9

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

Page 17: ICP - Lecture 9

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

Page 18: ICP - Lecture 9

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

Page 19: ICP - Lecture 9

19

main()

{

int a;

printf("Enter a number: ");

scanf("%d", &a);

int i;

for (i=1; i<=10; i++)

{

printf ("%d x %d \t= %d\n", a, i, a*i);

}

}

Page 20: ICP - Lecture 9

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