Looping Statements in C

 Looping Statements in C

Iterative Statements in C

Control Structure in  C Programming


What is loop in C language?

When a block of code is executed for several number of times to perform the operation is called loop or iteration in c language.  Loop statements allow a statement or  group of statements multiple times until a specific condition satisfies. 

Why to Use Loop?

In a program instead of writing a specific code again and again to perform the operation we can repeat the same segment of code for a finite number of times. 

For example suppose we want to print 10 natural numbers then instead of writing 10 printf() function we can write printf() inside the loop which helps to execute upto 10 iterations. 

Code reusability is the most important feature in C language.

Types of Loops in C language:

1. for Loop

2. while Loop

3. do  while Loop


1. for  Loop in C:

for loop is used when we want to execute a segment of code multiple times to perform the operation. It is used to iterate the statement multiple times. This block of code is executed until the condition is satisfied. 

Click here 👉👉👉 for loop flowchart


Syntax to use for loop,


for (initialization; test_condition; increment/decrement) 

{

//code to be executed

}

The for keyword followed by parenthesis which contain three expressions viz. Initialization, test condition, increment or decrement. Initialization means we have to use initial or start value then target value is given. Test condition is nothing but the stop value. The statement will be executed again and again until the test condition satisfies. In each iteration the value will be incremented  or decremented that instruction is given through this statement. By default the value is incremented or decremented by 1. 

To do so we can use increment (++) or decrement(--) operators in C language.

followed by opening and closing curly brackets which contains the statement that is repeatedly executed until condition satisfies. 


For example

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

{

printf(“%d\t”, i);

}

In the above example the value of i is initialised as 1. This is the start value.  Then target value is given with the help of the test condition. Until the of i becomes less than or equals to 10 the statement written in for loop will be executed again and again. In each iteration the value of i will be incremented by 1.


C program to demonstrate for loop


/**************************************************

 *C program to demonstrates for loop in C language

  **************************************************/

 #include<stdio.h>

#include<conio.h>

 void main()

 {

    clrscr();

     printf("** OUTPUT**\n");

     printf("First 10 natural numbers are\n");

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

     {

     printf("%d\t\n",i);

     }

     printf("End of program");

 }

** OUTPUT**

First 10 natural numbers are

1

2

3

4

5

6

7

8

9

10

End of program


The above program is to display the first 10 natural numbers using a for loop. The value of i is initialized as 1.  The printf() function or statement will be executed repeatedly until condition satisfies. Until 10 does not get. In every iteration the value of i will be incremented by 1. Once conditions are satisfied, iteration will stop and 1 to 10 natural numbers will be displayed on screen. 




टिप्पणी पोस्ट करा

0 टिप्पण्या