Looping Concepts in C: Best 5 Minutes Guide

Looping Concepts in C

The while Loop

A while loop statement repeatedly executes a statement that is in the brackets as long as a given condition in brackets is true and it will stop executing when the condition becomes false.

While loop in C
While loop in C

This diagram shows the basic syntax of while loop execution and below there is a program of while loop.

Example of while loop in C

#include 
int main()
{
   int count=1;
   while (count <= 4)
   {
  printf("%d ", count);
  count++;
   }
   return 0;
}

Output on Screen:

1
2
3
4

Note: if the condition in the while loop becomes more problematic it will execute on compile time as infinite and cannot control until we stop the execution.

Tips for while loop:

  • Use where we need targeted situation
  • Do not forget braces at start and endpoint it is a syntax error in c.
  • Do not forget the semicolon.

Using the while Loop for Input Validation

The while loop can be used to create input routines that repeat until acceptable data is entered.

While loop in C example

An example of a While loop for input validation

While (age < 13 || age > 99) {
  printf("Invalid age, re-enter: ");
  sacanf(“%d”,age);
}

Do while loop in C

The do-while loop is a post-test loop, which means its expression is tested after each iteration.

See also  Conditional Statements in C: Quick 5 Minutes Guide
do while loop in C
do-while loop in C

In the above diagram, it shows us that everything is in the do braces and at the end, we insert while loop braces and it will execute at the end after everything is run at compile time.

Why we use the do-while loop?

The C dowhile loop is used to iterate a part of the program several times. If the number of iterations is not fixed and you must have to execute the loop at least once, it is recommended to use the dowhile loop. The C dowhile loop is executed at least once because the condition is checked after the loop body.
Example


#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}
while (j<=3);
return 0;
}

Output on Screen:

Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3

The difference between while and do-while loop

  1. The while loop checks the condition at the starting of the loop and if the condition is satisfied statement inside the loop, is executed. As against, in the do-while loop, the condition is checked after the execution of all statements in the body of the loop.
  2. If the condition in a while loop is false, not a single statement inside the loop is executed. In contrast, if the condition in the ‘do-while loop is false, then also the body of the loop is executed at least once then the condition is tested.
  3. The while loop is also known as the entry-controlled and pre-checking loop because in this loop, the body of the loop is executed prior to checking the condition. Conversely, the alternate name for the do-while loop is the exit-controlled and post-checking loop, the reason behind this is that the checking of the loop condition is followed by the execution of the body of the loop.
  4. The syntax of a do-while loop includes a semi-colon to terminate the loop. On the contrary, there is no use of the semi-colon in the while loop syntax.
See also  Recursion In C: All-inclusive Just In 5 Minutes

For Loop

The for loop is a pretest loop (which means it is more versatile) that combines the initialization, testing, and updating of a loop control variable in a single loop header.

syntax for for loop in C
the syntax for loop in C

The above diagram, shows us the basic foam of for loop the initial statement is called the statement which executes the for loop and it will execute on the base of test condition which is in the second place and at the end, it checks the update statement.

Example of for loop

// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; i--)
    {
     printf("%d ", i);
     }
return 0;
}

Output on Screen:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, goodbye!

Nested for loop in C

Nested for loop is used to calculate the sum of two 2-dimensional matrices. The program consists of three nested loops where the outer loop runs equal to the size of the row and the inner loop runs equal to the size of the column. Basic Syntax of nested loop. It can also be used to print some specific patterns for the sake of the user. It is also called a loop in a loop.

Further visit: Branching Conditional Statements in C

The syntax for nesting for loop in C


for ( initial_condition; testcondition; incrementcondition ) {
   for ( initial_condition; testcondition; incrementcondition ) {
      statement(s);
   }
   statement(s); // put as many as you want!
}

Example of nesting for loop

#include <stdio.h>
int main()
{
    int i=1,j;
    while (i <= 5)
    {
        j=1;
        while (j <= i)
        {
            printf("%d ",j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output on Screen:

See also  6 Basic Types of Operators In C

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

How to break out of a loop in C

Break; is the keyword we need to use to break out of a loop in C.

The break statement gets you out of a loop. No matter what the loop’s ending condition, break suddenly. The program continues with the next statement immediately following the loop. Break stops only the loop in which it resides. It also does break from nested loops.

Share and Enjoy !

One thought on “Looping Concepts in C: Best 5 Minutes Guide

  1. Pingback: Looping concepts in C – TechTravelHub

Leave a Reply

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