For Loop With Example and Program

Pretest and Posttest in Loops

A pretest is a condition that is tested at the beginning of loop. The body of pretest loop is never executed if the condition is false the first time it is tested.

Example

The while loop is a pretest loop because it tests the expression before each iteration. The body of loop does not execute if the condition is false.

n=0;

while(n == 0)

{

loop body;

}

A posttest is a condition that is tested at the end of the loop. It means that the statements in the loop body will be executed at least once.

Example:

The do-while loop is a posttest loop and it tests its expression after each iteration. The body of loop executes at least once even if the condition is false

do

{

loop body

}

while(n == 0)

for Loop

for loop executes one or more statements for a specified number of times. This loop is also called counter-controlled loop. It is the most flexible loop. That is why, it is the most frequently-used loop by the programmers.

For Loop With Example and Program

Syntax:

The syntax of for loop is as follows:

for (initialization, condition; increment/decrement) 

{

statement 1; 

statement 2;

-------------

statement N;

}

Initialization

It specifies the starting value of counter variable. One or many variables can be initialized in this part. To initialize many variables, each variable is separated by comma.

Condition

The condition is given as a relational expression. The statement is executed only if the given condition is true. If the condition is false, the statement is never executed.

Increment/decrement

This part of loop specifies the change in counter variable after each execution of the loop. To change many variables, each variable must be separated by comma.

statement

It represents the body of the loop. If the body of loop contains more than one statement, they are written in curly braces (). It is called the body of the loop.

Working of for Loop

The number of iterations depends on the initialization, condition and increment/decrement The initialization is used to initialize a variable known as loop counter or loop variable. The initialization is executed only once when the control enters the loop.

The condition is evaluated after initialization. The control executes the loop body if the condition is true. Then increment/decrement part is executed that changes the value of counter variable. The control again moves to the condition. This process continues while the condition is true. The loop is terminated when the condition becomes false.

C++ program that displays counting from 1 to 5 using for loop.

#include <iostream.h>

#include <conio h>

void main()

{

int n;

clrscr()

for(n=1, n <= 5 ,n++)

cout<<n<<endl;

getch()

}

Output:

1

2

3

4

5

How above Program Works?

In the above example, the variable n is initialized 1. The termination condition is n <= 10. It means that the loop will continue while the value of counter is less than or equal to 10. The loop will terminate when the value of counter is greater than 10. The third part contains ++ It indicates that the value of n will be incremented by 1 after each execution of the loop.

Write a program that finds the sum of the squares of integers from 1 to n. Where n is a positive value entered by the user (ie Sum= 1 ^ 2 + 2 ^ 2 + 3 ^ 2 +...+n^ 2 ).

#include <iostream.h>

#include <conio.h>

void main()

{

int n, c;

long int sum;

sum = 0 ;

cout<<"Enter a number";

cin>>n;

for(c=1 c<=n; c++) 

sum = sum + (c * c); 

cout<<"Sum is "<<sum;

getch();

}

Output:

Enter a number: 5

Sum of square: 55

Explanation:

The above program declares three variables c. n and sum. It inputs a positive number from the user and stores this number in the variable n. In the initialization part of loop, e is initialized to 1. 

The condition statement c<=n uses n that is entered by the user. The loop will execute n times. The increment part increments the counter variable by 1

In each iteration, the statement sum sum + (cc) is executed. It takes the square of the counter variable c'and adds it to the variable sum. 

The loop runs n times and the squares of numbers from 1 to n are summed up. After completing for loop the cout statement is executed that displays the sum of the squares of number from 1 to n.

Post a Comment

0 Comments