The “for” loop:
The for loop statement comprises of 3 actions.=> The 3 actions are
“initialize expression”, “Test Condition expression” and “updation expression” ”
=> The expressions are separated by Semi-Colons (;).
=> The loop variable should be assigned with a starting and final value.
=> Each time the updated value is checked by the loop itself.
=> Increment / Decrement is the numerical value added or subtracted to the variable in each round of the loop.
Syntax:
for(initialize expression; test condition; updation )
{
Statement-1;
Statement-2;
}
(i) The initialization sets a loop to an initial value. This statement is executed only once.
(ii) The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop.
=> The for loop continues to execute as long as conditional test is satisfied.
=> When the condition becomes false the control of the program exits from the body of for loop and executes next statements after the body of the loop.
(iii) The updation(increment or decrement operations) decides how to make changes in the loop.
? The body of the loop may contain either a single statement or multiple statements.
for loop can be specified by different ways as shown
Print the first five numbers starting from one together with their squares.
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( ) ;
for(i = 1; i <=5; i++)
printf(“\n Number: %d its Square: %d”, i, i*i);
getch( );
}
Output :
Number: 1 its Square: 1
Number: 2 its Square: 4
Number: 3 its Square: 9
Number: 4 its Square: 16
Number: 5 its Square: 25
Program to display from 1 to 15 using for loop and i=i+1.
# include<stdio.h>
# include<conio.h>
main( )
{
int i;
clrscr( );
printf(“\n The Numbers of 1 to 15 are:”);
for(i=1; i < =15; i=i+1)
printf(“\n%d ”, i);
getch( );
}
Output :
The Numbers of 1 to 15 are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Nested “for” loop:
=> We can also use loop within loops.
=> i.e. one for statement within another for statement is allowed in C. (or 'C' allows multiple for loops in the nested forms).
=> In nested for loops one or more for statements are included in the body of the loop.
* ANSI C allows up to 15 levels of nesting. Some compilers permit even more.
=> Two loops can be nested as follows.
Syntax:
for( initialize ; test condition ; updation) /* outer loop */
{
for(initialize ; test condition ; updation) /* inner loop */
{
Body of loop;
}
}
? The outer loop controls the rows while the inner loop controls the columns.
-----------------------------------------
for(row =1; row<=rowmax ; ++ row)
{
for (column =1;column<=colmax; ++ column)
{
y = row * column;
printf(“%4d”, y);
}
printf( “\n”);
}
------------------------------------
Program to perform subtraction of 2 loop variables. Use nested for loops.
# include<stdio.h>
# include<conio.h>
void main( )
{
int a, b, sub;
clrscr( );
“Programming with C” course material
Department of CSE, GIT, GU Page 58
for (a=3; a > =1; a - - )
{
for(b=1;b<=2;b++)
{
sub = a – b;
printf(“a=%d b=%d a-b = %d \n”, a,b, sub);
}
}
getch( );
}
Output:
a=3 b =1 a-b =2
a=3 b =2 a-b =1
a=2 b =1 a-b =1
a=2 b =2 a-b =0
a=1 b =1 a-b =0
a=1 b =2 a-b =-1
Programs on “for” LOOP:
=> Print the first 5 numbers starting from one together with their squares and cubes.
# include<stdio.h>
# include<conio.h>
main( )
{
int i;
clrscr( ) ;
for(i=1; i<=5; i++)
printf(“\n Number:%d its square: %d its cube: %d”, i, i*i, i*i*i);
getch( );
}
Output:
Number: 1 its square: 1 its cube: 1
Number: 2 its square: 4 its cube: 8
Number: 3 its square: 9 its cube: 27
Number: 4 its square: 16 its cube: 64
Number: 5 its square: 25 its cube: 125.
=> Write a program to print five entered numbers in Ascending Order.
# include<stdio.h>
# include<conio.h>
main ( )
{
int a, b, c, d, e, Sum = 0, i;
clrscr( ) ;
printf(“ \n Enter five Numbers:”);
scanf( “ %d %d %d %d %d %d”, &a, &b, &c, &d, &e);
printf( “ \ n Numbers in Ascending Order is :”)
Sum = a + b + c + d + e;
for(i=1; i<=Sum; i++)
{
if(i = = a || i = =b || i = = c || i = = d || i = = e)
{
printf(“ %3d “,i);
}
}
getch();
}
Output:
Enter five numbers: 5 8 7 4 1
Numbers in Ascending Order is : 1 4 5 7 8
Examples on Nested “for” loop:
(1) Write a program to display the stars as shown below
*
* *
* * *
* * * *
* * * * *
# include<stdio.h>
# include<conio.h>
main ( )
{
int x, i, j ;
printf(“How many lines stars (*) should be print f? :”);
scanf(“%d”, &x);
for(i=1; i<=x; i++)
{
for (j=1; j < =i; j++)
{
printf( “*”);
}
printf( “ \n”);
}
getch( );
}
Output:
How many lines stars (*) should be print d ? : 5
*
* *
* * *
* * * *
* * * * *
Write a program to display the series of numbers as below
# include<stdio.h>
# include<conio.h>
main( )
{
int i,j,x;
clrscr( );
printf(“ \n Enter Value of x :”);
scanf(“ %d”, &x);
clrscr( ) ; /*optional*/
for(j=1; j < =x, j++)
{
for(i=1; i < = j; i++)
printf(“%3d”, i);
printf(“ \n”);
}
1
1 2
1 2 3
1 2 3 4
4 3 2 1
3 2 1
2 1
1
printf(“ \n”);
for(j=x; j>=1; j--)
{
for(i=j; i>=1;i--)
printf(“%3d”, i);
printf(“ \n”);
}
getch( );
}
Output:
Enter value of X : 4
1 comments:
Nice information!
Please take some time to visit my blog @
for loop problem in C
Thanks!
Post a Comment