CP-6.ppt - Computer Programming Loops Its Logic What is a Loop An iterative statement is called loop A type of control structure that repeats a

CP-6.ppt - Computer Programming Loops Its Logic What is a...

This preview shows page 1 - 10 out of 28 pages.

Computer Programming
Image of page 1
10/10/19 2 Loops & Its Logic What is a Loop? An iterative statement is called loop. A type of control structure that repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met. Repetition A single statement A set of statements
Image of page 2
10/10/19 3 Types of Loop Types of Loop Counter loop (for loop) Conditional loops (while, do while) Types of Loop For Loop While Loop Do While Loop
Image of page 3
10/10/19 4 For Loop Operation Initialization Expression Body of Loop Increment Expression Exit Test Expression True False
Image of page 4
10/10/19 5 For Loop Structure for ( j=0 ; j < 5 ; j++ ) statement ; Initialization Expression Increment Expression Single Statement Loop Body Test Expression keyword
Image of page 5
10/10/19 6 For Loop Structure for ( j=0 ; j < 5 ; j++ ) { statement ; statement ; statement ; } Initialization Expression Test Expression Increment Expression Multiple Statement Loop Body keyword
Image of page 6
10/10/19 7 For Loop // demonstrate simple for loop # include <iostream> int main() { int j; for (j=0 ; j < 5 ; j++) cout << j * j << “ “; return 0; } Output: 0 1 4 9 16
Image of page 7
10/10/19 8 For Loop Variations Multiple initialization and increment Expressions for ( x=0 , j=0 ; j < 5 ; j++ , x++) Increment or Decrement for ( j=5 ; j > 0 ; j-- ) for ( j=0 ; j <10 ; j+=2 ) controlling
Image of page 8
The Infinite loop A loop that does not terminate is called Infinite loop.
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 28 pages?

  • Fall '18
  • Zia Ul Rehman
  • Control flow, Do while loop, Loop Operation

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes