Skip to content

DIFFERENT LOOPS IN C

DIFFERENT LOOPS IN C

The loop in c can be characterized as repeating a similar cycle on different occasions till a particular condition is fulfilled.

A program loop therefore consists of two segments, one known as the body of the loop and the other known as a controlled statement. The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.

Depending on the position of the control statement in the loop, a control structure may be classified in two ways:

Entry-Controlled loop: In this the control conditions are tested before the start of the loop execution, if the conditions are not satisfied, then the body of loop will not be executed.

Exit-Controlled loop: In this the test is performed at the end of the body of loop and therefore the body is executed unconditionally for the first time.

A looping process, in general would include the following four steps:

  1. Setting and initialization of a condition variable.
  2. Execution of the statements in the loop,
  3. Test for a specified value of the condition variable for execution of the loop.
  4. Incrementing or updating the condition variable.

The C language provides for three constructs for performing loop operations. They are:

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

The While Loop in C

The simplest of all the looping structures in C is the while statement. The while is an entry-controlled loop.

The test condition is evaluated and if the condition is true, then the body of loop is executed, after execution of the body, the test-condition is once again evaluated and if it is true, the body is executed once again.

This process continues until the test condition becomes false. On exit, the program continues with the statement immediately after the body of a loop.

General form of the while loop:

while (test condition)

{

Body of loop

}

Examples of while loop

Example 1:

Write a program to print all natural numbers from 1 to n.

Code:

======================

#include <stdio.h>

int main()

{

int i, end;

//Input a number from user

 

printf(“Print all natural numbers from 1 to : “);

scanf(“%d”, &end);

//Print natural numbers from 1 to end

 

i=1;

while(i<=end)

{

printf(“%d “, i);

i++;

}

return 0;

}

======================

Output:

*NOTE: The variable i used in the program is called a counter or control variable.

Example 2:

Another example of while statement, which uses the keyboard input is shown below:

======================

character = ‘ ‘;

while (character != ‘Y’ )

{

character = getchar();

}

xxxxxxxxx;

======================

In the above given example, the first character is initialized to ‘ ’. The while statement then begins by testing whether character is not equal to Y.

Since the character was initialized to ‘ ‘, the test is true and the loop statement

character = getchar();

Is executed. The loop statement is executed until the letter Y is pressed. When Y is pressed, the condition becomes false because the character equals Y, and the loop terminates, thus transferring the control to the statement xxxxxxxxx; .

The Do While Loop in C

It is an exit-controlled loop that is first the body of a loop is executed then loop condition is tested in a while statement and if it is true then again the body of the loop is executed. This process continues until the test condition becomes false, and when the condition becomes false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement.

Since the test-condition is evaluated at the bottom of the loop, the do while loop in C construct provides an exit-controlled loop and therefore the body of a loop is always executed at least once.

General form of do while loop:

do

{

Body of loop

}

while( test-condition );

Examples of do while loop

Example 1:

Write a program to print the numbers between 1 and 100 which are multiple of 3.

Code:

======================

#include<stdio.h>

int main()

{

int i = 1; // declare and initialize i to 1

do

{

// check whether i is multiple of 3 not or not

if(i % 3 == 0)

{

printf(“%d “, i); // print the value of i

}

i++; // increment i by 1

 

}

while(i < 100); // stop the loop when i becomes greater than 100

 

return 0;

}

======================

Output:

Example 2:

Another simple example of a do while loop is:

======================

do

{

printf (“ Input a number \n”);

number = getnum();

}

while ( number>0);

======================

This segment of a program reads a number from the keyboard until a zero or a negative number is keyed in, and assigned to the sentinel variable number.

The test conditions may have compound relations as well. For instance, the statement

while ( number>0 && number<100 );

in the above example would cause the loop to be executed as long as the number keyed in lies between 0 and 100.

The For Loop in C

The for loop in c is another entry-controlled loop that provides a more concise loop control structure. The for loop allows us to specify three things about a loop in a single line:

  1. Setting a loop counter to an initial value.
  2. Testing the loop counter to determine whether its value has reached the number of repetitions desired.
  3. Increasing the value of a loop counter each time the body of the loop has been executed.

General form of for loop:

for ( initialization ; test-condition ; increment or decrement )

{

Body of this loop

}

In for loop first the loop variable is initialized then loop conditions are tested and if it is true then the body of loop is executed. After executing the body of loop variable is updated, again the condition is tested and if it is true then again the body of the loop is executed. This process continues until the test condition becomes false.

Examples of for loop

Example 1:

Write a program to calculate the sum of numbers from 1 to 100.

Code:

======================

#include<stdio.h>

int main()

{

int i; // loop variable

int sum = 0; // variable to accumulate sum

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

{

sum += i;

}

printf(“Sum = %d”, sum);

// return 0 to operating system

 

return 0;

}

======================

Output:

Example 2:

Another simple example of the for loop is:

======================

for ( x = 0 ; x<= 9 ; x=x+1 )

{

printf(“%d”,x);

}

printf(“\n”);

======================

This for loop is executed 10 times and prints the digits 0 to 9 in one line. The three sections enclosed within parentheses must be separated by semicolons. Note that there is no semicolon at the end of the increment section, x=x+1.

Comparison Between while, do while, for loops in C

while do while for
n=1;

while( n<10 )

{

_______

_______

n=n+1;

}

n=1;

do

{

_______

_______

n=n+1

}

while(n<=10);

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

{

_______

_______

}

It is known as an entry controlled loop. It is known as an exit controlled loop. It is known as entry controlled loop
Initialization and updating is not a part of syntax. Initialization and updating is not a part of syntax. Initialization and updating is a part of syntax.
There is no semicolon; after the condition in the syntax of the while loop. There is semicolon; after the condition in the syntax of the do while loop. There is no semicolon; after the condition in the syntax of the for loop.

Nested Loops in C

Nesting of a loop means one loop can also be used within another loop.Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops.

Syntax:

Outer loop

{

Inner loop

{

Inner loop statement(s)

}

Outer loop statement(s)

}

Example:

======================

#include <stdio.h>

int main()

{

int a, b;

for(a = 1; a <= 5; a++)

{

for(b = 1; b <= 5; b++)

{

printf(“%d “, b);

}

printf(“\n”);

}

return 0;

}

======================

Output:

Jumps in Loops

Break Statement: This statement is used to exit from the loop. When a break statement is encountered inside a loop, it immediately exits from the loop and control is transferred to the statement after the loop.

General form:

break;

Continue statement: When continue statement is used inside a loop all statements after continue are skipped and control transfers to the next iteration of the loop.

General form:

continue;

goto statement: It transfers the control of a program from one point to another. Learn More about it i next article

General form:

goto label

________

________

label;

nv-author-image

Vaibhav Kapoor

A professional web entrepreneur, WordPress developer and digital marketing strategist with more than 4 years of experience in building a business from scratch. Knowledgeable about SEO, blogging, internet marketing, social media and website development.

Leave a Reply

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

[wpfepp_submission_form form="1"]