while loop in C programming language
while loop in the C programming language
In computer programming languages the concept of loops are one of the most core components that the programmers and developers use in order to iterate or repeat some piece of code. It will not only increase the reusability of code but also enhances the performance of our code. The looping statement that we will cover today in this blog post is while loop in C programming language. The while loop is also referred to as entry controlled loop that repeats and and executes a piece of code as long as the specified condition is true. We use while loop when we are not sure that how many times a specific piece of code should be iterated. Now before going deep into while loop in C programming language let us look at the basic syntax and working of while loop in C program.
In computer programming languages there is a concept of control flow statements. In control flow statements we specify some conditions, and based on those conditions whether they are true or false we run some iterative piece of code, if the condition is true, and terminate the control statement if the condition is false.
There are mainly three types of control flow statements that we use in our programs to get the desired result, these three types of control statements are
Three types of Control flow statements
1) do-while loop
2) while loop
3) for loop
In today's post, we will discuss the while loop control statement. while loop is also known as a pre-tested loop in which before running the piece of code the condition is examined first. If the condition is true only then the while loop will execute and if the condition is false then the while loop statement terminates.
In these control statements, the piece of code is iterated according to the given condition. Now let us look at the syntax of the while loop statement. Below is the syntax of the while loop in c programming.
Basic syntax and working of while loop in C programming language
int i; while (condition) { // code to be executed here // increment or decrement variable i++ }
In the above while loop syntax coding part we can see that we have used the while key word followed by opening and closing round brackets inside which we have specified a condition. After that we have simply opening and closing curly brackets. Now with in these curly brackets we can write some piece of code as per the requirement of our programming logic. And remember two main points that the conditional variable of while loop must be declared and initialized just above the while loop body, and incremented the variable just before the closing curly bracket.
After that the working of while loop is simple for as long as the condition is true. And when the while loop condition is false the while loop will terminate. So this is the basic syntax and working of the while loop in C programming language.
First coding example
#include<stdio.h> void main() { int i = 1; while (i <= 5) { printf("%d\n", i); i++; } _getch(); }
In the above coding example of while loop in C programming language we can see that at the beginning of the code we have simply declared and initialized the value of variable i whose data type is integer like this,
int i = 1;After that we have simply created a while loop with key word followed by round brackets with specified condition like this,
while (i <= 5)And after that we have opening and closing curly brackets with in which we have defined the body of while loop code. And with in while loop body we are simply printing the value of variable i from 1 to 5. And atlast before the closing bracket we have simply incremented the value of variable i like this,
i++;
At this point the condition is true because the value in variable i is equal to 1 and according to the condition, while loop will run unless the value of i is less or equal to 5 like this,
int i = 1; while (i <= 5)
So this is the first C programming coding example of while loop. Now let us move to the next coding example of while loop in C program.
Second coding example
#include<stdio.h> void main() { int TableNumber, TableLength, i = 1; printf("Enter Table Number = "); scanf_s("%d", &TableNumber); printf("Enter Table Length = "); scanf_s("%d", &TableLength); while (i <= TableLength) { printf("%d * %d = %d\n", TableNumber, i, TableNumber * i); i++; } _getch(); }
In the above coding example of while loop in C programming language we are simply generating table of any specific number upto some specific range of length like 1 to 10 or 1 to 20.
In this coding example we will take two inputs from the user, TableNumber and TableLength. TableNumber will decide the number whose table we want to generate and TableLength will specify the starting to ending range of table. And we have used while loop to implement this.
With in while loop we have set the condition the while loop should run unless the value of variable i is less then or equal to TableLength like this,
while (i <= TableLength)which means that if TableLength is 10, the while loop will show the table of any specific number from 1 to 10.
And before the closing curly bracket we have simply incremented the value of variable i like this,
i++;
So this is the basic coding example of while loop in C programming language.
Congratulations! like always we have successfully implemented the while loop in C programming language.
Comments
Post a Comment