C-06

24
Lecture 6 Version 1.0 The for Loop The break Statement The continue Statement Version 1.0

description

 

Transcript of C-06

Page 1: C-06

Lecture 6Version 1.0

The for LoopThe break Statement

The continue StatementVersion 1.0

Page 2: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

2

The for Loop

The for loop allows us to specify three things in a single line:

1. Setting a loop counter to an initial value.2. Testing the loop counter to determine whether its

value has reached the number of repetitions desired.3. Increasing/decreasing the value of loop counter each

time the program segment within the loop is executed.

Page 3: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

3

General form of for loop

Page 4: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

4

Example of for loop This program prints number 1 to 10

Page 5: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

5

Structure of for loop

Page 6: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

6

Variation of for loop

Page 7: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

7

Variation of for loop

Page 8: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

8

Variation of for loop

Page 9: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

9

Variation of for loop: Infinite loop

Page 10: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

10

Observing for loops

The initialization, loop-continuationcondition and increment/decrement cancontain arithmetic expressions. Assumethat x=2 and y=10. The statement-for(j=x; j<=4*x*y; j=j+y/x)

is equivalent to-for(j=2; j<=80; j=j+5)

Page 11: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

11

Observing for loops

There can be a decrement as well (or you

can say a negative increment). Forexample-

Page 12: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

12

Observing for loops

If the loop continuation condition is initially

false, the body portion of the loop is notperformed.

Page 13: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

13

Observing for loops

Page 14: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

14

Qs

Page 15: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

15

Nested for loops

Page 16: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

16

Output

Page 17: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

17

What will be the output?

Page 18: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

18

What will be the output?

Page 19: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

19

The break Statement

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test.

The keyword break allows us to do this.

Page 20: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

20

The break Statement

When break is encountered inside any loop, control automatically passes to the first statement after the loop.

Page 21: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

21

The break Statement

Page 22: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

22

The continue Statement

In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed.

Page 23: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

23

The continue Statement

The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.

Page 24: C-06

Rushdi Shams, Dept of CSE, KUET, Bangladesh

24

The continue Statement