1 / 19

For Loop

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 loop  is classified as an iteration statement.

nelia
Download Presentation

For Loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For Loop Lecture No 8

  2. Definition • In computer science a for loop is a programminglanguage 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 whileloop, 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: for (i = 0; i < 10; i++) { /* 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.

  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. • In C and C++ similar languages: • for (counter = 1; counter <= 5; counter++) //statement;

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

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

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

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

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

  12. For Loop Example 4 • 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; } 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? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

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

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

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

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

More Related