Lecture 6
Version 1.0
The for Loop
The break Statement
The continue Statement
Version 1.0
Rushdi Shams, Dept of CSE, KUET, Bangladesh2
The for Loop
 The for loop allows us to specify three
things in a single lin...
Rushdi Shams, Dept of CSE, KUET, Bangladesh3
General form of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh4
Example of for loop
 This program prints number 1 to 10
Rushdi Shams, Dept of CSE, KUET, Bangladesh5
Structure of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh6
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh7
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh8
Variation of for loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh9
Variation of for loop: Infinite
loop
Rushdi Shams, Dept of CSE, KUET, Bangladesh10
Observing for loops
The initialization, loop-continuation
condition and incr...
Rushdi Shams, Dept of CSE, KUET, Bangladesh11
Observing for loops
There can be a decrement as well (or you
can say a negat...
Rushdi Shams, Dept of CSE, KUET, Bangladesh12
Observing for loops
If the loop continuation condition is initially
false, t...
Rushdi Shams, Dept of CSE, KUET, Bangladesh13
Observing for loops
Rushdi Shams, Dept of CSE, KUET, Bangladesh14
Qs
Rushdi Shams, Dept of CSE, KUET, Bangladesh15
Nested for loops
Rushdi Shams, Dept of CSE, KUET, Bangladesh16
Output
Rushdi Shams, Dept of CSE, KUET, Bangladesh17
What will be the output?
Rushdi Shams, Dept of CSE, KUET, Bangladesh18
What will be the output?
Rushdi Shams, Dept of CSE, KUET, Bangladesh19
The break Statement
 We often come across situations where
we want to jump ...
Rushdi Shams, Dept of CSE, KUET, Bangladesh20
The break Statement
 When break is encountered inside any
loop, control aut...
Rushdi Shams, Dept of CSE, KUET, Bangladesh21
The break Statement
Rushdi Shams, Dept of CSE, KUET, Bangladesh22
The continue Statement
 In some programming situations we
want to take the ...
Rushdi Shams, Dept of CSE, KUET, Bangladesh23
The continue Statement
 The keyword continue allows us to do
this. When con...
Rushdi Shams, Dept of CSE, KUET, Bangladesh24
The continue Statement
Upcoming SlideShare
Loading in …5
×

C-06

290 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
290
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C-06

  1. 1. Lecture 6 Version 1.0 The for Loop The break Statement The continue Statement Version 1.0
  2. 2. Rushdi Shams, Dept of CSE, KUET, Bangladesh2 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.
  3. 3. Rushdi Shams, Dept of CSE, KUET, Bangladesh3 General form of for loop
  4. 4. Rushdi Shams, Dept of CSE, KUET, Bangladesh4 Example of for loop  This program prints number 1 to 10
  5. 5. Rushdi Shams, Dept of CSE, KUET, Bangladesh5 Structure of for loop
  6. 6. Rushdi Shams, Dept of CSE, KUET, Bangladesh6 Variation of for loop
  7. 7. Rushdi Shams, Dept of CSE, KUET, Bangladesh7 Variation of for loop
  8. 8. Rushdi Shams, Dept of CSE, KUET, Bangladesh8 Variation of for loop
  9. 9. Rushdi Shams, Dept of CSE, KUET, Bangladesh9 Variation of for loop: Infinite loop
  10. 10. Rushdi Shams, Dept of CSE, KUET, Bangladesh10 Observing for loops The initialization, loop-continuation condition and increment/decrement can contain arithmetic expressions. Assume that 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)
  11. 11. Rushdi Shams, Dept of CSE, KUET, Bangladesh11 Observing for loops There can be a decrement as well (or you can say a negative increment). For example-
  12. 12. Rushdi Shams, Dept of CSE, KUET, Bangladesh12 Observing for loops If the loop continuation condition is initially false, the body portion of the loop is not performed.
  13. 13. Rushdi Shams, Dept of CSE, KUET, Bangladesh13 Observing for loops
  14. 14. Rushdi Shams, Dept of CSE, KUET, Bangladesh14 Qs
  15. 15. Rushdi Shams, Dept of CSE, KUET, Bangladesh15 Nested for loops
  16. 16. Rushdi Shams, Dept of CSE, KUET, Bangladesh16 Output
  17. 17. Rushdi Shams, Dept of CSE, KUET, Bangladesh17 What will be the output?
  18. 18. Rushdi Shams, Dept of CSE, KUET, Bangladesh18 What will be the output?
  19. 19. Rushdi Shams, Dept of CSE, KUET, Bangladesh19 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.
  20. 20. Rushdi Shams, Dept of CSE, KUET, Bangladesh20 The break Statement  When break is encountered inside any loop, control automatically passes to the first statement after the loop.
  21. 21. Rushdi Shams, Dept of CSE, KUET, Bangladesh21 The break Statement
  22. 22. Rushdi Shams, Dept of CSE, KUET, Bangladesh22 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.
  23. 23. Rushdi Shams, Dept of CSE, KUET, Bangladesh23 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.
  24. 24. Rushdi Shams, Dept of CSE, KUET, Bangladesh24 The continue Statement

×