Course Content
Basic of C programming
0/14
Functions, Array and Structures
0/11
C Programming Pointers
0/1
C Pointers & Arrays
0/1
C Pointers & Functions
0/1
C Memory Allocation
0/1
C Programming String
0/1
C String Functions
0/1
C Files Input/Output
0/1
C Enumeration
0/1
C Preprocessors
0/1
C Standard Library
0/1
C Programming Tutorial
About Lesson

## C Loops

In computer programming, loops are used to repeat a block of code. Loops in Programming come into use when we need to repeatedly a block of statements.
For example, let’s say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.

In programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.

### Types of Loops

There are 3 types of loops in C++:
1)Â `for`Â loop
2)Â `while`Â loop
3)Â `do...while`Â loop

In privious tutorial we discuss about CÂ `For`Â Loop & In this tutorial focuses on CÂ `while`Â loop &Â `do-while`Â loop.

## while Loop

• whileÂ Loop is a Entry Controlled Loop or we can say repetition control structure.
• TheÂ whileÂ loop is used in situations where we do not know the exact number of iterations of loop beforehand.
• The loop execution is terminated on the basic of the test condition.

Syntax :

``while(condition) {Â Â // statements inside the body of the loop }``

### How while loop works?

• AÂ whileÂ loop evaluates theÂ condition.
• If theÂ conditionÂ evaluates toÂ true, the code inside theÂ whileÂ loop is executed.
• TheÂ conditionÂ is evaluated again.
• This process continues until theÂ conditionÂ isÂ false.
• When theÂ conditionÂ evaluates toÂ false, the loop terminates.

To learn more about test expression (when the test expression is evaluated to true and false), check outÂ relationalÂ andÂ logical operators.

### Example : while loop

``// Program to print a statement 3 times#include <stdio.h>int main() {Â Â int i = 1;Â Â while (i <= 3)Â Â {Â Â Â Â printf("Hello world \n");Â Â Â Â i++;Â Â }Â Â return 0;}``

Output

`Hello World Hello world Hello world `

Â

Working of while loop in above example :

Iteration Variable condition Action
1st `i = 1` `true` Hello World is printed and `i` is increased to `2`.
2nd `i = 2` `true` Hello World is printed 2nd time and `i` is increased to `3`.
3rd `i = 3` `true` Hello World is printed 3rd time and `i` is increased to `4`
4th `i = 4` `false` The loop is terminated

## Nested while Loop

AÂ whileÂ loop within anotherÂ whileÂ loop is calledÂ Nested whileÂ loop.

Syntax :

``while (condition){Â Â while (condition)Â Â {Â Â Â Â // body of inner while-loop Â Â }Â Â // body of outer while-loop }``

### Example: Nested while loop

``// Program to display a triangular pattern #include<stdio.h>int main() {int rows, i = 1;printf("Enter the number of rows : ");scanf("%d", &rows);while (i <= rows) {Â Â int j = 1;Â Â while(j <= i) {Â Â Â Â printf("*  ");Â Â Â Â j++;Â Â Â }Â Â printf("\n");Â Â i++;Â }return 0;}``

Output

`Enter the number of rows: 5*  *  *   *  *  *  *  *  *  **  *  *  *  *`

In this program, the outer loop iterates fromÂ `1`Â toÂ rows.

The inner loop iterates fromÂ `1`Â toÂ i. Inside the inner loop, we print the characterÂ `'*'`.

• Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.
• If you don’t understand above example, We recommended you to know little more aboutÂ Nested Loop.

## do…while Loop

• LikeÂ whileÂ loop theÂ do-whileÂ loop execution is also terminated on the basic ofÂ test condition.
• TheÂ do-whileÂ loop is exit-controlled loop.
• InÂ do-whileÂ loop the loop body will execute at least once irrespective ofÂ test condition.

Â

Syntax :

``do {// body of loop;}while (condition);``

### How do-while loop works?

• The body of the loop is executed at first. Then theÂ `condition`Â is evaluated.
• If theÂ `condition`Â evaluates toÂ `true`, the body of the loop inside theÂ `do`Â statement is executed again.
• TheÂ `condition`Â is evaluated once again.
• If theÂ `condition`Â evaluates toÂ `true`, the body of the loop inside theÂ `do`Â statement is executed again.
• This process continues until theÂ `condition`Â evaluates toÂ `false`. Then the loop terminates.

### Example : Do-while Loop

``// Program to print numbers from 1 to 5#include <stdio.h>int main() {Â Â int i = 1; Â Â do {Â Â Â Â printf("%d ",i);Â Â Â Â ++i;Â Â }Â Â while (i <= 5);Â Â return 0;}``

Output

`1 2 3 4 5`

Working of while loop in above example :

Iteration Variable Condition Action
Â  `i = 1` not checked 1 is printed and `i` is increased to 2
1st `i = 2` `true` 2 is printed and `i` is increased to 3
2nd `i = 3` `true` 3 is printed and `i` is increased to 4
3rd `i = 4` `true` 4 is printed and `i` is increased to 5
4th `i = 5` `true` 5 is printed and `i` is increased to 6
5th `i = 6` `false` The loop is terminated

## Nested do-while Loop

AÂ do-whileÂ loop within anotherÂ do-whileÂ loop is calledÂ Nested do-whileÂ loop.

Syntax :

``do {Â Â do {Â Â Â Â // body of inner do-while-loop Â Â }while (condition);Â Â // body of outer do-while-loop }while (condition);``

Example:

``// C program to display a triangular pattern #include <stdio.h>int main() {Â Â int rows, i,j;Â Â i = 1;Â Â printf("Enter the number of rows : ");Â Â scanf("%d", &rows);Â Â do {Â Â Â Â j = 1;Â Â Â Â do{Â Â Â Â Â Â printf("* ");Â Â Â Â Â Â j++;Â Â } while(j <= i);Â Â printf("\n");Â Â i++;Â Â } while(i <= rows);Â Â return 0;}``

Output

`Enter the number of rows: 5*    *  *   *  *  *  *  *  *  **  *  *  *  *`

Note :Â To know the difference ofÂ forÂ loop andÂ while loop,Â
To know the difference ofÂ whileÂ andÂ do-while loop,Â

## Loop Control Statements

These control statements change the execution of the loop from its normal execution. The loop control structures are â€“
1.Â break statementÂ â€“Â It is used to end theÂ loopÂ orÂ switchÂ statement and transfers execution to the statement immediately following the loop or switch.
2.Â continue statementÂ â€“Â It skip some statements according to the given condition.
3.Â goto statementÂ â€“Â It transfer control to the labeled statement.

error: Content is protected !!