C - SPLessons

Chapter 15

Loops in C

Loops in C

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.

C-language has three basic types of loops:

For Loop

For loop consists of statements which can be executed until the condition becomes false.
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.

#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

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.

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

#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

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.

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

#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

  • 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.

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