Loops In C Tutorial

Loops In C Tutorial

Last updated on 08th Oct 2020, Blog, Tutorials

About author

Sharan ((Sr Technical Project Manager, Capgemini ) )

She is Possessing 8+ Years Of Experience in C & C++. Her Passion lies in Developing Entrepreneurs & Activities. Also, Rendered her intelligence to the Enthusiastic JOB Seekers.

(5.0) | 14547 Ratings 1786

Loops are very useful when you want to perform a task repeatedly. Loop’s body has a set of statements, which gets executed on every iteration until a given condition is met.

We have three types of loops in C. The working of these loops are almost similar, however they are being used in different scenarios. You may need to choose the loop based on the requirement.

Below are the tutorial links on each type of loop (for, while, do-while) & loop control statements(break, continue, goto).

  1. 1. for loop: This is the most commonly used loop in C language. The syntax and flow of this loop is simple and easy to learn. However there are few cases when you may prefer any other loop, instead of this.
  2. 2. while loop: This is used when you need to execute a block of statements repeatedly until a given condition is met. Read this tutorial to understand the flow of this loop.
  3. 3. do-While loop: It is similar to the while loop, the only difference is that it evaluates the test condition after execution of the statements enclosed in the loop body.
  4. 4. break statement: It is used with various loops (for, while and do-While) and switch case statements. When a break statement is encountered inside a loop, the control comes out of the loop. When it gets encountered in switch-case, the control comes out of the switch case and continues execution with the statement following switch-case body.
  5. 5. continue statement: Continue statement is used inside loops. Whenever it is encountered inside a loop, control directly jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the loop’s body for the current iteration.
  6. 6. goto statement: When goto statement is encountered in a C program, the control jumps to the mentioned label. It is rarely used as it makes the program complex and confusing.
Subscribe For Free Demo

Error: Contact form not found.

How it Works?

The below diagram depicts a loop execution,

Loop-Flow-In-C

As per the above diagram, if the Test Condition is true, then the loop is executed, and if it is false then the execution breaks out of the loop. After the loop is successfully executed the execution again starts from the Loop entry and again checks for the Test condition, and this keeps on repeating.

The sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of the loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed, and execution breaks out of the loop.

Types of Loop

There are 3 types of Loop in C language, namely:

  1. 1. while loop
  2. 2. for loop
  3. 3. do while loop

1. while loop

while loop can be addressed as an entry control loop. It is completed in 3 steps.

  • Variable initialization.(e.g int x = 0;)
  • condition(e.g while(x <= 10))
  • Variable increment or decrement ( x++ or x– or x = x + 2 )

Syntax :

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

Example: Program to print first 10 natural numbers

  • #include<stdio.h>
  • void main( )
  • {
  •     int x;
  •     x = 1;
  •     while(x <= 10)
  •     {
  •         printf(“%d\t”, x);
  •         /* below statement means, do x = x+1, increment x by 1*/
  •         x++;
  •     }
  • }
  1. 1 2 3 4 5 6 7 8 9 10

2. for loop

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop.. General format is,

  • for(initialization; condition; increment/decrement)
  • {
  •     statement-block;
  • }

In the loop we have exactly two semicolons, one after initialization and second after the condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. But it can have only one condition.

The for loop is executed as follows:

  1. 1. It first evaluates the initialization code.
  2. 2. Then it checks the condition expression.
  3. 3. If it is true, it executes the for-loop body.
  4. 4. Then it evaluates the increment/decrement condition and again follows from step 2.
  5. When the condition expression becomes false, it exits the loop.

Example: Program to print first 10 natural numbers

  • #include<stdio.h>
  • void main( )
  • {
  •     int x;
  •     for(x = 1; x <= 10; x++)
  •     {
  •         printf(“%d\t”, x);
  •     }
  • }
  1. 1 2 3 4 5 6 7 8 9 10

3. Nested for loop

We can also have nested for loops, i.e one for loop inside another for loop. Basic syntax is,

  • for(initialization; condition; increment/decrement)
  • {
  •     for(initialization; condition; increment/decrement)
  •     {
  •         statement ;
  •     }
  • }
Course Curriculum

Get Practical Oriented C Training form Industry Experts Trainers

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

Example: Program to print half Pyramid of numbers

  • #include<stdio.h>
  • void main( )
  • {
  •     int i, j;
  •     /* first for loop */
  •     for(i = 1; i < 5; i++)
  •     {
  •         printf(“\n”);
  •         /* second for loop inside the first */
  •         for(j = i; j > 0; j–)
  •         {
  •             printf(“%d”, j);
  •         }
  •     }
  • }
  1. 1. 1
  2. 2. 21
  3. 3. 321
  4. 4. 4321
  5. 5. 54321

4. do while loop

In some situations it is necessary to execute the body of the loop before testing the condition. Such situations can be handled with the help of a do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that the body of the loop will be executed at least once, even though the starting condition inside while is initialized to be false. General syntax is,

  • do
  • {
  •     …..
  •     …..
  • }

while(condition)

Example: Program to print first 10 multiples of 5.

  • #include<stdio.h>
  • void main()
  • {
  •     int a, i;
  •     a = 5;
  •     i = 1;
  •     do
  •     {
  •         printf(“%d\t”, a*i);
  •         i++;
  •     } 
  •     while(i <= 10);
  • }
  1. 5 10 15 20 25 30 35 40 45 50

5. Jumping Out of Loops

Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as a certain condition becomes true. This is known as jumping out of a loop.

1) break statement

When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

Braek-Statement
C and C++ Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

2) continue statement

It causes the control to go directly to the test-condition and then continue the loop process. On encountering continue, the cursor leaves the current cycle of the loop, and starts with the next cycle.

Continue-Statement

Conclusion

Above are the loops that are defined in the C programming language. To select a particular loop for solving the problem or writing the program, the program has to be very careful with the requirements of the client. The program has to analyze the problem, what type of checks are required like pre and post check. Looping in C or in any programming language is one of the key concepts. There are generally two types that are entry controlled and exit controlled loop. The loops or statement blocks execute a number of times until the condition becomes false. So, it is better to analyze the issue or problem properly and select the loop accordingly for better performance of the program and memory usage.

Are you looking training with Right Jobs?

Contact Us

Popular Courses