0% found this document useful (0 votes)
3K views

Looping Statements in C

There are three common types of loops in C programming: while loops, for loops, and do-while loops. While and for loops are entry-controlled loops where the test condition is checked before the loop body executes. Do-while loops are exit-controlled as the test condition is checked after executing the loop body at least once. Loops can be nested, such as a for loop within another for loop. Break and continue statements allow skipping parts of a loop or exiting a loop early. An infinite loop is an error where the test condition always evaluates to true, causing the loop to repeat indefinitely without stopping.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Looping Statements in C

There are three common types of loops in C programming: while loops, for loops, and do-while loops. While and for loops are entry-controlled loops where the test condition is checked before the loop body executes. Do-while loops are exit-controlled as the test condition is checked after executing the loop body at least once. Loops can be nested, such as a for loop within another for loop. Break and continue statements allow skipping parts of a loop or exiting a loop early. An infinite loop is an error where the test condition always evaluates to true, causing the loop to repeat indefinitely without stopping.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Looping statements in C:

Loops in programming come into use when we need to repeatedly execute a block
of statements. For example: Suppose we want to print “Hello” 10 times.
This can be done in two ways as shown below:

   for (i = 1; i <= 10; i++)

    {
        printf( "Hello\n");     // prints hello for 10 times
    }
  
 Types of loops:
There are mainly two types of loops:

1. Entry Controlled loops: In this type of loops the test condition is tested before
entering the loop body. For Loop and While Loop are entry controlled loops.

2. Exit Controlled Loops: In this type of loops the test condition is tested at the
end of loop body. do – while loop is exit controlled loop.
There are 3 types of Loop in C language, namely:
1. while loop
2. for loop
3. do while loop

While loop:
while loop can be addressed as an entry control loop. It is completed in 3 steps.
 Variable initialization.
 Condition.
 Variable increment or decrement.

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++;
}
}

Output:

1 2 3 4 5 6 7 8 9 10
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;

The for loop is executed as follows:


1. It first evaluates the initialization code.
2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and 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);
}
}

Output:
1 2 3 4 5 6 7 8 9 10
Do while loop

In some situations it is necessary to execute body of the loop before testing the
condition.
Such situations can be handled with the help of 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
{
//body.
}
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);
}

Output:

5 10 15 20 25 30 35 40 45 50
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 ;
}
}

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);
}
}
}

Output:

1
21
321
4321
54321
Jumping Out of Loops(Jumping statements)
Sometimes, while executing a loop, it becomes necessary to skip a part of the loop
or to leave the loop as soon as certain condition becomes true.
This is known as jumping out of loop.
1) break statement
When break statement is encountered inside a loop, the loop is immediately exited
and the program continues with the statement immediately following the loop.

2) continue statement
It causes the control to go directly to the test-condition and then continue the loop
process. On encountering continue, cursor leave the current cycle of loop, and starts
with the next cycle.
Write about an Infinite Loop?

An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a
functional exit so that it repeats indefinitely. An infinite loop occurs when a
condition always evaluates to true. Usually, this is an error.

Example:

  
#include <stdio.h>
  
int main ()
{
    int i;
  
    for ( ; ; ) // This is an infinite for loop as the condition

    {
       printf("This loop will run forever.\n");
    }
  
return 0;
}

Output:

You might also like