C language Looping Controls

In this chapter we are going to study about:
1. While loop.
2. Do … while loop.
3. For loop.
4. Break
5. Continue
6. Goto
Looping statements are used when we have a set of statements that needs to be executed repeatedly.

1. While loop.

While loop is used when we don’t know how many times a statement needs to be executed.
Syntax:
	while (conditional_statement)
	{
		//statements to be executed
	}

Example: Below example of while loop, that calculates sum of 1 to 10.

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int i = 0;
	int sum = 0;

	while (i <= 10)
	{
		sum += i;
		i++;
	}

	printf("The sum of 1 to 10 is %d\n",sum );

}

Output:

The sum of 1 to 10 is 55

2. Do … while loop.

In while loop, statements are executed if the condition is true. But in some scenarios we need to execute the statements at least once, even if the condition is false. That time we use do..while loop.
Here the statements are executed first and then the conditions are checked.

Syntax:

	do
	{
		//statements to be executed
	}while(condition);

Example:

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int i = 4;

	do
	{
		printf("Here the condition is false, but this statement will be printed once\n");
	}while( i < 3);

}

Output:

Here the condition is false, but this statement will be printed once

3. For loop.

For loop is used instead of while loop.

Below is the syntax of for loop:

for( initialization; condition_checking; increment or decrement)
{
	//statements to be executed repeatedly
}

As you can see that there are 3 parts in for loop:
Initialization: Here we initialize the variable. This is done only once.

Condition_checking: Here we check the condition. If the condition is satisfied then we execute the loop, else we come out of the loop.

Increment or Decrement: This is done once all the statements are executed. This can be a statement or set of statements. This will be executed every-time the loop is completed.

Example: Below is an example of for loop to print numbers 1 to 3.

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int i = 0;

	for(i = 1; i <= 3; i++)
	{
		printf("I = %d\n", i);
	}

}

Output:

I = 1
I = 2
I = 3

Below are the different variations of for loop:

1.	For loop with initialization outside:

i = 0;

for(; i< 10; i++)
2.	Multiple statements in for loop:

for(i = 0, j = 1 ; (i < 10 && j < 20); i++, j++)
3.	Infinite for loop:

for( ; ;)
4.	Nested for loop
for( i = 0 ; i < 3 ; i ++)
{
	for( j = 0; j <4; j ++)
	{
		printf(“ i = %d, j = %d”, i, j);
	}
}

4. Break

Break statement is used to terminate out of the loop. This is often used to come out of the loop after a certain condition is met. When break is used in nested loops, then the break will come out of the innermost loop.
Break can also be used with conditional statements.
“break” is the keyword used.

Example: Below example, we come out of the loop after the count is equal to 5.

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int i = 0;

	printf("Entering loop\n");
	while (i <= 20)
	{
		printf("I = %d\n", i );
		i++;

		if(i > 5)
			break; // Using break here
	}
	printf("Exiting loop\n");

	return 0;
}

Output:

Entering loop
I = 0
I = 1
I = 2
I = 3
I = 4
I = 5
Exiting loop

5. Continue

Continue statement is opposite of break statement. Here continue statement will skip the present iteration and will continue next iteration.
When a condition is satisfied, continue will not exit the loop instead, it will skip the present execution and start from beginning of the loop.

Example: Below example uses continue statement, will skip the print when “I” value is 5.

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int i = 0;

	printf("Entering loop\n");
	while (i <= 8)
	{
		i++;
		if(i == 5) 
			continue; // Using continue statement here
		
		printf("I = %d\n", i );

	}
	printf("Exiting loop\n");

	return 0;
}

Output:

Entering loop
I = 1
I = 2
I = 3
I = 4
I = 6
I = 7
I = 8
Exiting loop

6. Goto

Goto statement is used to do an unconditional jump. Sometime in programming there might be a situation where we have to do a jump without any particular condition, then it might be useful to use “goto” statement.
The goto statement will jump to the “label” mentioned and starts executing from there.

Example: Example for a goto statement to check if a number is even or odd:

#include<stdio.h>

int main(int argc, char const *argv[])
{
	int number = 0;

	printf("Enter a number\n");
	scanf("%d", &number);

	if(number % 2 ==0 )
	{
		goto even;
	}
	else
	{
		goto odd;
	}

even:
	printf("The number is even\n");
	return 0;// We return from here as we don’t want to execute next statement.


odd:
	printf("The number is odd\n");
	return 0;

}

Output 1:

Enter a number
3
The number is odd

Output 2:

Enter a number
4
The number is even
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *