Skip to main content

Loop In C Language

Loop In C Language 


A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages −

Loop Architecture

C programming language provides the following types of loops to handle looping requirements.

Sr.No.Loop Type & Description
1while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

2for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3do...while loop

It is more like a while statement, except that it tests the condition at the end of the loop body.

4nested loops

You can use one or more loops inside any other while, for, or do..while loop.


While Loop in C

A while loop is the most straightforward looping structure. While loop syntax in C programming language is as follows:


Syntax of While Loop in C:

while (condition) {
             statements;
}

It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.


After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body.


In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It is different in do while loop which we will see shortly.

Following program illustrates while loop in C programming example:

#include<stdio.h>
#include<conio.h>
int main()
{
	int num=1;	//initializing the variable
	while(num<=10)	//while loop with condition
	{
		printf("%d\n",num);
		num++;		//incrementing operation
	}
	return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10



Do-While loop in C

A do…while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.

Syntax of do while loop in C programming language is as follows:

Syntax of Do-While Loop in C:

 do {
  statements
} while (expression);

As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.

The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;)

The following loop program in C illustrates the working of a do-while loop:

Below is a do-while loop in C example to print a table of number 2:

#include<stdio.h>
#include<conio.h>
int main()
{
	int num=1;	//initializing the variable
	do	//do-while loop 
	{
		printf("%d\n",2*num);
		num++;		//incrementing operation
	}while(num<=10);
	return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20
For loop in C

A for loop is a more efficient loop structure in ‘C’ programming. The general structure of for loop syntax in C is as follows:

Syntax of For Loop in C:

for (initial value; condition; incrementation or decrementation ) 
{
  statements;
}
  • The initial value of the for loop is performed only once.
  • The condition is a Boolean expression that tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
  • The incrementation/decrementation increases (or decreases) the counter by a set value.

Following program illustrates the for loop in C programming example:

#include<stdio.h>
int main()
{
	int number;
	for(number=1;number<=10;number++)	//for loop to print 1-10 numbers
	{
		printf("%d\n",number);		//to print the number
	}
	return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

The above program prints the number series from 1-10 using for loop.



Comments

Popular posts from this blog

IMPORTANCE OF C

IMPORTANCE OF C The increasing popularity of C is probably due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. In fact, many of the C compilers available in the market are written in C.     Programs written in C are eficient and fast. This is due to its variety of data types and powerful operators. It is many times faster than BASIC. For example, a program to increment a variable from O to 15000 takes about one second in C while it takes more than 50 seconds in an interpreter BASIC.     There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing program    C...

The main Function

         The main Function The main is a part of every C program. C permits diferent forms of main statement. Following forms are allowed. main() int main void main() main(void) void main(void) int main(void) The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main . The keyword void means that the function does not return any information to the operating system and int means that the function retúrns an integer value to the operating system.   When int is specified, the last statement in the program must be "return O" . For the sake of simplicaty, we use the first form in our program. Example:                                  // Programm of Addition       // Writ...

Syntax/Format of C program

C Programs A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file. Before we study the basic building blocks of the C programming language, let us look at a bare minimum C program structure so that we can take it as a reference in the upcoming chapters.     Syntax/Format of simple C program main ()  <--------------------- Function name  {  <------------------------------ Start of program         ........         ........  <------------------- Program statement         ........  }  <------------------------------ End of program      Hello World Example A C program basically consists of the following parts − • Preprocessor Commands • Functions • Variables • Sta...