470 likes | 1.1k Views
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.
E N D
For Loop Lecture No 8
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.
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 */ }
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.
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;
For Loop Example 1 • int i,j; • for(i=1; i<=10; i++) • { • printf(“\nA”); • } • getch() • }
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() • }
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() • }
For Loop Examples 3 • Make any Table (Take the Input from User) using (for loop)Print
#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(); • }
For Loop Example 4 • Print Sum of Even and Odd Numbers from 10-20 (for loop)
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(); }
For Loop Example 5 • Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
int i,j; clrscr(); for(i=1; i<=15; i++) { for(j=1; j<=i; j++) { printf(" *"); } printf("\n"); } getch(); }
For Loop Example 6 • Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
int i,j; • clrscr(); • for(i=1; i<=10; i++) • { • for(j=10; j>=i; j--) • { • printf(" *"); • } • printf("\n"); • } • getch(); • }
Assignment Questions 1 • Make mirrored shape of Example 4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Assignment Questions 2 • Make mirrored shape of Example 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *