Iteration In C: Use Of While And Do-While Loop With Examples

While And Do-While Loop In C

Today we will learn how to use While and Do-While loops in C. In previous chapter I had explained for loop and its usages, please go through the link to explore "For loop".
Iterative Statement In C: For Loop 

While Loop -


While loop iterates on condition basis, while is the keyword and we put condition within the bracket (). This loop does Initialization, condition checking and increment or decrements process separately.  

Syntax : -


Initialize the condition


while ( condition ) {


  execute code
  

  update condition

}

1. First initialize the condition

2. While loop checks condition


3. If condition satisfies then piece of code will execute.


4. Update the condition.


5. Again pointer will go to while loop to check condition. Loop will continue

   until condition is true.

#Program 1

Write a program to get length of any name.

# include <stdio.h>


void main() {


char name [10] ;

int i =0, length =0; //initializing the counter


printf (" Enter your name ");

fgets(name , 10 ,stdin);


while (name [i] !='\0') {


length ++;

i++;

}

printf (" Length of name %s is %d ", name, length);

}
First ask user's name, I have used fgets method instead of scanfThe scanf method do not accept space, for more detail please click on the link -
Difference between fgets and scanf method in C. 

In C set of characters store into array and array index starts with 0 and ends when user press enter. The last character of name would be null character ('\0').


Initialize i=0, i is the counter that will read character's array.


While will iterate until character of name is null.


If character is not null then length will increased by 1.


update the i counter i++ to check next character of name.


Note: Carefully handle the condition otherwise it may cause infinite loop 
condition i.e. condition will never satisfy and loop will not break.


For example: Here i is counter to read characters one by one, so that 
with every iteration we must increment i counter, in case if we mention i= i -1 it will cause infinite loop because condition will never satisfy.


Do-While Loop -


Do-While loop also work as While loop but the main difference is that While loop first checks condition and then runs its flow. Do-While loop first runs the flow once and then check the condition.

#Program 2

Write a program to ask user his/her choice and operate appropriate
operation.

choice = 1,  calculate area of rectangle

choice = 2,  calculate area of square
choice = 3,  calculate area of circle
choice = 4,  exit

# include <stdio.h>


void main() {


int choice , length, width,square;

float radius, area;


printf ("1 -  area of rectangle \n");

printf ("2 -  area of circle \n");

printf ("3 -  area of square \n");

printf ("4 -  exit \n");


do {


printf(" \n Please enter your choice - ");

scanf ("%d" ,&choice);


switch (choice) {


case 1:

printf(" Enter length and width ");

scanf ("%d%d" ,&length,&width);

area = length * width;

break;


case 2:

printf(" Enter radius ");

scanf ("%f" ,&radius);

area = 3.14*radius*radius;

break;


case 3:

printf(" Enter side ");

scanf ("%d%d" ,&side);

area = side* side;

break;


case 4:

area = 0.0;

break;


default:

area = 0.0;

printf ("\n Invalid choice, Please try again");

}

printf("\n Area is - %f", area);


} while( choice !=4);


}
In the above example you can see without any checking we ask choice from
user, once user enters his/her choice then perform the operation and then 
checks the condition.


Thanks for reading, please do share and comment your suggestions.

No comments

Powered by Blogger.