C - SPLessons
boy2 Chapter 15

Loops in C

C - SPLessons

Loops in C

shape Description

Computers are great at crunching piles of data, and piles of data usually takes the form of sequences, streams, tables and so on. Such data sets usually need to be iterated over. At the very least, some statements may need to be executed more than once. Loops in C and iteration statements are used for this purpose.

Loops in C are a block of statements on which repetitive execution is performed until the condition is satisfied.

shape Loop
Types

C-language has three basic types of loops:

For Loop

shape Description

For loop consists of statements which can be executed until the condition becomes false.

shape Syntax

for(initialization; condition; decrement or increment iterator)
{
//statements;
}

Flow of control for ”for” loop

1)Initialization : In initialization section, the variables are declared and initialized. Initialization will be executed only once and if it satisfies it goes to condition section.

2)Condition : In condition section, the condition is checked. If true, then control enters the loop and the statements are executed.Then the control goes to increment section. But if the condition is false, the loop is terminated.

3)Increment or Decrement : Once the loop gets executed the flow of control jumps back to Increment or Decrement section. Here the value gets incremented (or) decremented and again the control goes to condition section. This section is optional.

shape Flow chart

shape Example

#include<stdio.h>;
int main() 
{ 
	int i; 
	for(i=0;i<10;i++)
	{ 
		printf("%d ",i); 
	}
} 

Output:

0 1 2 3 4 5 6 7 8 9

While Loop

shape Description

While loop is considered when it is unknown how many times to execute.

Compiler only checks the condition is true or false. The While loop will only execute the statement if the expression is true. After the statement is executed, it will once again evaluate the expression and repeat as needed.

shape Syntax

initialization;
while(condition)
{
//statements;
variable increment or decrement;
}

shape Flowchart

shape Example

#include<stdio.h>
#include<conio.h>
void main( ) 
{ 
	int x; x=1; 
	while(x<=5) 
	{ 
		printf("%d\t", x); x++;
	}
	getch(); 
}

Output:

1       2       3       4       5

Do..While Loop

shape Description

do…while loop is mainly used when want to execute the loop statements at least once, irrespective of the condition given. Those statements are kept in do and then the condition is checked.

shape Syntax

initialization;
do
{
//statements;
increment or decrement;
}
while(condition);
{
//statements;
}

shape Flow chart

shape Example

#include<stdio.h>
#include<conio.h>
int main( ) 
{ 
	int x; 
	x=6;
	do
	 { 
		printf("splessons"); 
	 }
	while(x<=5);
	 { 
		printf("\t %d \t", x);
		x++; 
	 } 
	getch();
}

Output:

splessons        6

Summary

shape Key Points

  • Loops performs repeated execution to make a condition become true.
  • for loop initializes variables, checks condition and increments/decrements. This can be used when there is sequential input/output.
  • While loop executes after the condition becomes true.
  • do-while loop executes at-least once even if the condition is false.

shape Programming
Tips

In for loop, initialization cannot be changed once the execution goes to condition checking. So, better check it before execution.