Presentation is loading. Please wait.

Presentation is loading. Please wait.

For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.

Similar presentations


Presentation on theme: "For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for."— Presentation transcript:

1 For Loop Lecture No 8

2 Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop.

3 Three-expression for loops This type of for loop is found in nearly all languages which share a common heritage with the C programming language. It is characterized by a three- parameter loop control expression; consisting of an initializer, a loop-test, and a counting expression. A representative example in C is: This type of for loop is found in nearly all languages which share a common heritage with the C programming language. It is characterized by a three- parameter loop control expression; consisting of an initializer, a loop-test, and a counting expression. A representative example in C is: for (i = 0; i < 10; i++) for (i = 0; i < 10; i++) { /* loop body */ /* loop body */ }

4 Three-expression for loops The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even ifcontinue is called - and is usually responsible for altering the loop variable. The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even ifcontinue is called - and is usually responsible for altering the loop variable.

5 Flow Chart of For Loop

6 Syntax The syntax for a three-expression for loop is nearly identical in all languages that have it, after accounting for different styles of block termination and so on. The syntax for a three-expression for loop is nearly identical in all languages that have it, after accounting for different styles of block termination and so on. In C and C++ similar languages: In C and C++ similar languages: for (counter = 1; counter <= 5; counter++) //statement; for (counter = 1; counter <= 5; counter++) //statement;

7 For Loop Example 1 int i,j; int i,j; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“\nA”); printf(“\nA”); } getch() getch() }

8 For Loop Example 2 Find product of number. The program must perform it at least 10 times. Find product of number. The program must perform it at least 10 times. int i,j,a; int i,j,a; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“Enter value here = ”); printf(“Enter value here = ”); scanf(%d,&a); scanf(%d,&a); a=a*a; a=a*a; printf(“The product is %d”,a); printf(“The product is %d”,a); } getch() getch() }

9 Nested For Loops The nested for loop is called the loop inside the loop. e.g The nested for loop is called the loop inside the loop. e.g int i,j,a; int i,j,a; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“ \n ”); printf(“ \n ”); for(j=1; j<=10; j++) for(j=1; j<=10; j++) printf(“A”); printf(“A”); } getch() getch() }

10 For Loop Examples 3 Make any Table (Take the Input from User) using (for loop)Print Make any Table (Take the Input from User) using (for loop)Print

11 #include #include void main() void main() { clrscr(); clrscr(); int a,b,c; int a,b,c; Printf("Enter the Value of Table=“); Printf("Enter the Value of Table=“); Scanf(“%d”,&a); Scanf(“%d”,&a); for (b=1; b<=10; b++) for (b=1; b<=10; b++) { c=a*b; c=a*b; Printf(“\n%d * %d = %d”,a,b,c); Printf(“\n%d * %d = %d”,a,b,c); } getch(); getch(); }

12 For Loop Example 4 Print Sum of Even and Odd Numbers from 10-20 (for loop) Print Sum of Even and Odd Numbers from 10-20 (for loop)

13 void main() {clrscr(); int a,c,d; c=0;d=0; for (a=10; a<=20; a++) {if(a%2==0){ c=c+a; c=c+a; }else{d=d+a;}} printf("Sum of Even Numbers from 10-20= %d “, c); printf("\nSum of Odd Numbers from 10-20= %d“, d); getch();}

14 For Loop Example 5 Make increasing stars arrangements as fallow? Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

15 int i,j; int i,j; clrscr(); clrscr(); for(i=1; i<=15; i++) for(i=1; i<=15; i++) { for(j=1; j<=i; j++) for(j=1; j<=i; j++) { printf(" *"); printf(" *"); } printf("\n"); printf("\n"); } getch(); getch(); }

16 For Loop Example 6 Make increasing stars arrangements as fallow? Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

17 int i,j; int i,j; clrscr(); clrscr(); for(i=1; i<=10; i++) for(i=1; i<=10; i++) { { for(j=10; j>=i; j--) for(j=10; j>=i; j--) { { printf(" *"); printf(" *"); } } printf("\n"); printf("\n"); } } getch(); getch(); } }

18 Assignment Questions 1 Make mirrored shape of Example 4. Make mirrored shape of Example 4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

19 Assignment Questions 2 Make mirrored shape of Example 5 Make mirrored shape of Example 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Download ppt "For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for."

Similar presentations


Ads by Google