Skip to main content

while loop in C

while loop in C:

In this session we are going to discuss about while loop and do while loop in C.
If you have not red the first session in which I have discussed about for loop the link for the same will be present below this article.

1. while loop in C:

  • As we have seen earlier in for loops it requires initialization, condition, increment or decrement directly after it is declared as shown below.
for(initialization, condition, increment)
  • But the same is not true in the case of while loop in while loop the initialization is done first outside the loop, the condition is written after the loop and the increment or decrement operator within the loop.

Basic Syntax:

int i=1; (initialization)
while (condition)
{
code if condition is true;
i++;← (increment operator)
}

Explanation:

  • From the above syntax we can see that the initialization (i=1) is done before the loop is executed.
  • The condition for which loop is executed  is written within parenthesis after writing while.
  • The increment i++ is done when it enters the loop within curly brackets.
  • The loop gets executed only if the condition is true on the contrary if the condition is false the loop gets terminated at the very same time.

Example Code:

Write a program to find sum of first n natural number by accepting value of n from user.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,s=0;
clrscr();
printf("\n enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s+i;
i++;
}
printf("\n the sum of first %d natural numbers is %d",n,s);
getch();
}

Output:

enter the value of n
5
the sum of first 5 natural numbers is 15

Explanation of the above code:

  • In the above code we have declared three variables of datatype integer n is used for storing the input, s is used for sum and i is used for initialization and execution of loop.
  • As the value of n in the above code is 5 so the while loop will first check the condition (i<=n) i.e (1<=5) which is true in this case so the code in the loop will get executed.
  • Now we have s=s+i
  • Here it happens like this s=0+1.
  • Because the initial value of s is 0 and i is 1. After this the new value 1 is assigned to s and now its value is 1.
  • After this increment takes place and the value of i is increased to 2. The loop again checks the condition (2<=5) which is true and executes the code again so now again it is s=s+i i.e s=1+2 so the current value of 3 is assigned to s.
  • The loop goes so on and stops when the value of i becomes greater than 5.

2. do while loop in C:

In do while loop the loop is executed once automatically without checking the condition..
Then the increment and decrement takes place and finally the condition is checked.
Initialization is done before the loop is executed, same as in the case of while loop. The condition is written at end after the code.

Basic Syntax:

int i=1;←(initialization)
do
{
code for loop;
i++;← (increment operator)
}while (condition); 

Note:

  • There is no semicolon after do but it is present after the condition.
  • The code is written within curly brackets.

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("%d",i);
i++;
}while(i<1)
getch();
}

Output:

1

Explanation of the above code:

In the above code we have initialized the value of i=1 and we have the condition that i should be less than 1 which is not true in this case then also 1 gets printed.
This is because in do while loop the loop gets executed once without checking the condition.

If you want to practice programs on loops checkout my further post of for and while loop programs in c


If you not have studied for loop yet below is the link for same 

Subscribe to my site by simply clicking the subscribe button so you get notified whenever I upload a new article.

If you have any doubts please comment down below.

Please share the article with your friends who wants to learn C programming.

Comments

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS: 1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases. 1.1 Header Files: Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h". Example:               #include<stdio.h>                                 ↓                    ⇡          Tells computer to                  header file  with           include the header                 extension .h          file                                     T here are many header files for different functions such as the following:          <conio.h> <math.h> <string.h

How to start programming for beginners

HOW TO START PROGRAMMING: 1. What is Programming? Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs. Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program. Programming can also be used to build a software to determine whether a particular number is prime or not. 2. Can anyone learn Programming ? The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming.  The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming? If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now. The other important Factor is Money if you become a m

Basic C Programming Questions

Basic Coding Questions: Exercise 1: Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth. Ans: #include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); } The above image code is written in code blocks Output: enter the length and breadth of rectangle 20(press enter) 10 (press enter) the area of rectangle is 200 and the perimeter is 60 Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers. Taking input of length and breadth together by using scanf function. Substituting the values of length and breadth stored in l and b