[go: up one dir, main page]

0% found this document useful (0 votes)
6 views12 pages

loop

The document explains loops in programming, specifically in C, detailing their purpose to repeat code until a condition is met. It describes three types of loops: for, while, and do-while, along with their syntax and examples. Additionally, it covers loop control statements and infinite loops, illustrating how they function in C programming.

Uploaded by

cargha440
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)
6 views12 pages

loop

The document explains loops in programming, specifically in C, detailing their purpose to repeat code until a condition is met. It describes three types of loops: for, while, and do-while, along with their syntax and examples. Additionally, it covers loop control statements and infinite loops, illustrating how they function in C programming.

Uploaded by

cargha440
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/ 12

Loops in programming are used to repeat a block of code until the specified

condition is met. A loop statement allows programmers to execute a


statement or group of statements multiple times without repetition of code.

// C program to illustrate need of loops

#include <stdio.h>

Int main()

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Printf( “Hello World\n”);

Return 0;

Output
Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

There are mainly two types of loops in C Programming:

Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-
controlled loops.

Exit Controlled loops: In Exit controlled loops the test condition is evaluated
at the end of the loop body. The loop body will execute at least once,
irrespective of whether the condition is true or false. do-while Loop is Exit
Controlled loop.

Loops in c

Loop Type Description

For loop first Initializes, then condition check, then executes the body and
at last, the update is done.

While loop first Initializes, then condition checks, and then executes the
body, and updating can be inside the body.

Do-while loop do-while first executes the body and then the condition
check is done.
For Loop

For loop in C programming is a repetition control structure that allows


programmers to write a loop that will be executed a specific number of
times. For loop enables programmers to perform n number of steps together
in a single line.

Syntax:

For (initialize expression; test expression; update expression)

//

// body of for loop

//

Example:

For(int I = 0; I < n; ++i)

Printf(“Body of for loop which will execute till n”);

In for loop, a loop variable is used to control the loop. Firstly we initialize the
loop variable with some value, then check its test condition. If the statement
is true then control will move to the body and the body of for loop will be
executed. Steps will be repeated till the exit condition becomes true. If the
test condition will be false then it will stop.

Initialization Expression: In this expression, we assign a loop variable or loop


counter to some value. For example: int i=1;

Test Expression: In this expression, test conditions are performed. If the


condition evaluates to true then the loop body will be executed and then an
update of the loop variable is done. If the test expression becomes false then
the control will exit from the loop. for example, i<=9;

Update Expression: After execution of the loop body loop variable is updated
by some value it could be incremented, decremented, multiplied, or divided
by any value.

For loop Equivalent Flow Diagram:

For loop in c

Example:

// C program to illustrate for loop

#include <stdio.h>

// Driver code

Int main()

Int I = 0;

For (I = 1; I <= 10; i++)

Printf( “Hello World\n”);

Return 0;

}
Output

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

While Loop

While loop does not depend upon the number of iterations. In for loop the
number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition. If the test
condition will become false then it will break from the while loop else body
will be executed.

Syntax:

Initialization_expression;

While (test_expression)

// body of the while loop

Update_expression;
}

Flow Diagram for while loop:

While loop in C

// C program to illustrate

// while loop

#include <stdio.h>

// Driver code

Int main()

// Initialization expression

Int I = 2;

// Test expression

While(I < 10)

// loop body

Printf( “Hello World\n”);

// update expression

I++;

}
Return 0;

Output

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Do-while Loop

The do-while loop is similar to a while loop but the only difference lies in the
do-while loop test condition which is tested at the end of the body. In the do-
while loop, the loop body will execute at least once irrespective of the test
condition.

Syntax:

Initialization_expression;

Do

// body of do-while loop

Update_expression;
} while (test_expression);

Do while loop in C

// C program to illustrate

// do-while loop

#include <stdio.h>

// Driver code

Int main()

// Initialization expression

Int I = 2;

Do

// loop body

Printf( “Hello World\n”);

// Update expression

I++;

// Test expression

} while (I < 1);

Return 0;
}

Output

Hello World

Above program will evaluate (i<1) as false since I = 2. But still, as it is a do-
while loop the body will be executed once.

Loop Control Statements

Loop control statements in C programming are used to change execution


from its normal sequence.

NameDescription

Break statement the break statement is used to terminate the switch and
loop statement. It transfers the execution to the statement immediately
following the loop or switch.

Continue statement continue statement skips the remainder body and


immediately resets its condition before reiterating it.

Goto statement goto statement transfers the control to the labeled


statement.

Infinite Loop

An infinite loop is executed when the test expression never becomes false
and the body of the loop is executed repeatedly. A program is stuck in an
Infinite loop when the condition is always true. Mostly this is an error that
can be resolved by using Loop Control statements.

Using for loop:

// C program to demonstrate infinite


// loops using for loop

#include <stdio.h>

// Driver code

Int main ()

Int I;

// This is an infinite for loop

// as the condition expression

// is blank

For ( ; ; )

Printf(“This loop will run forever.\n”);

Return 0;

Output

This loop will run forever.

This loop will run forever.

This loop will run forever.

Using While loop:


// C program to demonstrate

// infinite loop using while

// loop

#include <stdio.h>

// Driver code

Int main()

While (1)

Printf(“This loop will run forever.\n”);

Return 0;

Output

This loop will run forever.

This loop will run forever.

This loop will run forever.

Using the do-while loop:

// C program to demonstrate

// infinite loop using do-while


// loop

#include <stdio.h>

// Driver code

Int main()

Do

Printf(“This loop will run forever.\n”);

} while (1);

Return 0;

Output

This loop will run forever.

This loop will run forever.

This loop will run forever.

You might also like