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
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
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
10/10/19 4 For Loop Operation Initialization Expression Body of Loop Increment Expression Exit Test Expression True False
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
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
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
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
The Infinite loop A loop that does not terminate is called Infinite loop.

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