While and Do while Loop Lop in C Program.
Hi Everyone,
In Previous blogs we discussed about for loop and nested for loop with some programs. Today we will discuss about other loops that are while and do while loop in C program. . To see how for loop and nested for loop is working you can see my below blogs for your reference.
https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html
https://dheeraj60.blogspot.com/2020/06/nested-for-loop-with-simple-pattern.htmlLet’s start with while and do –while loop in C Program.
Loop: A loop is mainly used for executing a block of statements repeatedly until a given condition gives false same as we discussed in for loop.
While Loop: While loop evaluates the test expression inside the parenthesis ().If the expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until the test expression is evaluated to false. If the test expression is false, the loop will terminate.
Syntax of While Loop in C.
while (condition test)
{
//Statements that needs to be executed repeatedly
// Increment (++) or Decrement (--) Operation
}
Simple Program to understand how while loop work in C. If I want to print number from 1 to 10 then we use below program using while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
while (i <= 10)
{
printf("%d \n", i);
i++;
}
return 0;
}
Here variable i is initialized with value 1 and then it has been tested for the condition that is i <= 10. If condition returns true then the statements inside the body of while loop are executed else control comes out of the loop. Value of i is incremented using ++ operator then it has been tested again for the loop condition. It will run till the condition satisfied and print number from 1 to 10 . You can change the condition as per your requirement.
Below is the output of above program in Compiler Code Block.
Let’s see one more Program for Table using while loop. We will take input from user for any number and give the output of multiplication of that table . We already done this by using for loop but here we using while loop and syntax is different as first we give the condition if it’s true then statement and last increment.
Below is the Program .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mult, i = 1;
printf("Please Enter any Number:");
scanf("%d", &mult);
printf("Multiplication table of %d: ", mult);
while (i <= 10) {
printf("%d x %d = %d", mult, i, mult * i);
i++;
}
return 0;
}
In above program we use first check the condition while (i <= 10) then statement and in last we do the increment , here what the user will give any input number output will be displayed for that number table.
Below is the Output of above program in Code Block.
Here you can enter any number to print the table as per your need.
DO –While loop : Let’s understand do –While loop which is very similar to while loop but While loop is executed only when condition is true. Whereas, do-while loop is executed for first time irrespective of the condition is true or false. After executing while loop for first time, then condition is checked. So simply we can say that if a condition is false at the first place then the do while would run once, but the while loop would not run at all if condition is false.
Syntax of Do-while loop:
do
{
//Statements
}
while(condition test);
Here in Syntax only we see condition is checked after the statement so first time it will execute only, but then it will check the condition. Let’s understand with program.
Program by using do-while loop which will give the value of variable 0 to 10;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
do
{
printf("Value of i is: %d\n", i);
i++;
}
while (i<=10);
return 0;
}
Note : In Do while program will run for first time and if we make any changes in condition that not satisfy then program terminates , but first time it will run only .
Output of above program in compiler Code block is as below :
Let’s take one more example of table which we have written in while loop let’s do with do-while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1,mult=0;
printf(" Please enter any number: ");
scanf("%d",&mult);
do{
printf("%d \n",(mult*i));
i++;
}while(i<=10);
return 0;
}
Here variable mult pass by user with any number and it will give the table of that number.
Below is the output of above Program in compiler code block.
As I mentioned in the beginning of this blog that do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop . But while loop only executes if condition is true.
Now we are familiar with all the loops in C and also the conditional statements . For reference you can see my previous blog of C. Please go through this blog and previous blogs and start with Simple Mathematical programs like check whether number is odd or even , prime number, other basic program and pattern related program I already written for your reference . I am sure you can easily write the program in C now. But I want you to write some complex programs by using loops and conditional statements and if any issue let me know. In next blog I will come up with some interesting topics in C program which will be useful for you to write some complex program with your own control. Please feel free to reach me anytime.
Thanks
Nice explanation
ReplyDeleteSay, you got a nice article post.Really thank you! Really Great.
ReplyDeletebest machine learning course in hyderabad
machine learning training in hyderabad