You are on page 1of 4

__________________________________________________________________

__________________________________________________________________
CONTROL STRUCTURES – FOR STATEMENT
___________________________________

OBJECTIVES:

At the end of the lesson, the students should be able to:

 identify the syntax and use of for statement;


 compare while statement with for statement; and
 use for statement in solving problems with iteration.

OVERVIEW
Looping statements are statements in Turbo C that allows certain operation to be repeated several
times. An example of this is the while statement.

FOR STATEMENT

This looping statement is capable of performing iteration using the parameters on its
general form unlike while statement that has separate declarations of its other iteration
parameters.

Syntax

For(initialization; condition; increment/decrement)


Statement(s);

1. The three main parts of for statement


a. The initialization statement is executed before the loop starts. This is used to
initialize the loop control variable or any other variable needed within the loop.
b. The condition is relational expression that determines when the loop will exit. This is
used to limit the number of times the loop will execute. If the condition is TRUE the
loop continues to execute.
c. The increment is executed at the end of each traversal through the loop. It defines
how the loop control variable will change each time the loop is repeated.
2. These three major sections must be separated by semicolons.
3. The for loop continues to execute as long as the condition is true. Once the condition
becomes false, program execution resumes on the statement following the for loop.

Sample Declaration
for (x=1;x<=5;x++)

Sample Program

/*This program using for statement will display numbers 5 to 1*/


#include<stdio.h>
main()
{ int counter;
clrscr;
printf(“The numbers 1 to 5 in descending order \n”);
for (counter=5; counter <=1; counter--)
{
printf(“%d”, counter);
}
getch();
}

Sample Output

The numbers 1 to 5 in descending order


5
4
3
2

Sample Program

#include<stdio.h>
/* This program can determine the sum of 5 input integers*/
main()
{
int i, sum, n;
for(i=0;i<5;i++)
{
printf(“Enter numbers: \n”);
scanf(“%d”, &n);
sum+=n;
}
printf(“Sum of 5 numbers is: %d”, n);
}
Sample Output

Enter numbers:
1
3
5
7
2
Sum of 5 numbers is: 18

SUMMARY

For statement is a statement that can perform iteration. It has three (3)
major components:

 Initialization statement
 Condition
 Increment / decrement

You might also like