What is a loop?

The repetition of the program can be obtained by using the conditional repetitive type of flow. It would check the condition of the program after each iteration and depending upon the result, it would either repeat or stop the execution.
Thus, the loop is a repetitive structure used in a program to operate a statement for a given number of times in order to carry out a specific task.

A looping structure contains the following parts:

Control Variable: A variable, which starts with an initial value and determines the duration of the repetition is known as control variable.

Body of the loop: A set of statements which are executed simultaneously within the loop.

Test Condition: A test condition is the condition which decides whether the loop has to be repeated or terminated. It is presented in each looping statement.
If the control enters the body of the loop for execution it repeats otherwise terminates.

Step Value: The step value in a looping structure determines the increment or decrement of the control variable unless the test condition is false.

For Loop in C 

In for loop structure, the statements are repeated for fixed number of times. The control terminates after repeating the statement for a given number of times.

Syntax of for loop:

for (initialization statement; test condition; increment/decrement expression)
{

// body of for loop

}

 

Example:

#include <stdio.h>
int main()
{
int a;
for (a=1; a<20; a++)
{
print (“ %d ” , a)
}
return 0;
}

 

Different ways of using for loop

Omitting expressions
A for loop can run effectively after omitting some of the parameters.

Example:
for (; a<=10; a++)
{
print (“ %d ” , a)
}

In the above statement, it does not require initialization of the variable. Hence, the loop starts with a garbage initial value and operates the loop body unless the test condition is false.

Note: You must apply “;” even the initialization within the for statement is ignored.

 

Null Loop Or Body Less Loop
A for loop, which does not include any statement as body of the loop is called bull loop or empty loop.

Example:

for (int a=1; a<=10; a++);

A semicolon (;) placed after closing of brackets in for loop indicates that the loop does not execute any statement under it or body of the loop is not available.

Purpose: The purpose of using such loop structure is simply to create a delay during execution.

 

Infinite Loop Or Endless Loop
You can also design the structure of for loop which will never end. Such loop is called infinite loop or endless loop.

Example:
for (a=1; a<=10; )
{
print (“ %d ” , a)
}

In the looping structure shown above increment/decrement expression is missing. Hence, the value of a will always remain 1. As a result, the test condition will never be false and loop will get executed infinitely.

 

Switch Case Statement

A multi branching statement in which control jumps to perform a particular action out of a number of actions depending upon a switch value is known ad switch case statement.

A switch statement is associated with a number of blocks. Each block is defined under a specific case, and the control gets transferred to a particular case which matches with the given switch value. Each case ends with a break statement, which can be used as a case terminator. Break statement passes the control out of the switch block.

A special case called default case is used in switch statement which gets automatically followed if no case matches with the given switch value.

Syntax:
switch (expression)
{
case 1:
                 // statements

break;

case 2:

              // statements

break;

case 3:

                // statements

break;

……
……
……

default:

            // default statement
}

 

Example:

#include <stdio.h>
void main()
{
int n;
printf (“Enter a number”);
scanf (“ % d ”, & n);
switch(n)
{
case 1: printf (“Monday”);
break;
case 2: printf (“Tuesday”),
break;
case 3: printf (“Wednesday”);
break;
case 4: printf (“Thursday”);
break;
case 5: printf (“Friday”);
break;
case 6: printf (“Saturday”);
break;
default: printf (“Invalid Input”);
}
}

Leave a Reply

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