How to use while loop in C / C++
In last tutorial, we discussed for loop and examples with for loop in C / C++. In this tutorial, we will learn about while
loop in C / C++ and its working with the help of some examples.
The while
loop is used a lot in programming languages, especially C and C++ languages. So let's see the syntax and how to use it.
Syntax of while loop in C / C++
while
loop is also used to repeat a block of code. It is very prone to infinite loops, which is not good for your program.The while
loop will repeat an action without knowing the number of iterations in advance. It just iterates until a stop condition is met or a break, continue, ... statement is encountered.
Syntax of while loop
while(condition) { statement(s); }
Here,
- condition: This is the condition to continue the loop, there are two cases.
- If true: Continue loop execution.
- If false: Exit the loop.
- statement: The statements inside the loop body will be executed if the condition is true.
The while
loop can also be represented by a for loop as follows:
for(;condition;) { statement(s); }
For example: Suppose we want to display "Welcome to learnnc.com" 10 times, then the while
loop would look like this:
int i = 1; while(i<=10){ cout<<"Welcome to learnnc.com\n"; i++; }
Flowchart of while loop in C / C++
How while loop in C / C++ work?
In the while
loop, the condition of the loop will be considered first. If the condition is true then the statements inside the loop will be executed. This will repeat until the condition is false or encounters statements like break
, continue
, ... . Otherwise, if the condition is false then exit the loop.
*Note:
A very important point when using while
loop is the stop condition of the loop. We need to have increment or decrement statements inside the while
loop so that the loop variable changes through each iteration. This way we can end the execution of while
loop otherwise the loop would execute indefinitely.
Infinite while loop in C / C++
while
loop is a loop that never stops. It repeats until the end of the program.Why is there this case? Because, when the condition of the loop is always true, the statements inside the loop are executed. This leads to infinite looping and no stopping.
An example of infinite while loop:
#include <iostream> using namespace std; int main () { int i; while(i=1) { cout << "Infinite while loop!!!\n" << endl; } return 0; }
Explain:
This is the condition of the loop i = 1. There is no statement inside the loop that changes the value of i, so i = 1 is always true. This results in the statements inside the loop being looped indefinitely.
Output:
Difference between for and while loop in C / C++
The difference between for
loop and while
loop is shown in the following table:
Different point | For loop | While loop |
---|---|---|
Syntax | for ( init; condition; increment ) { statement(s); } |
while(condition) { statement(s); } |
In case of used | Knowing the number of iterations in advance | Don't know the number of iterations in advance |
Condition | If there is no condition, the for loop is looped indefinitely | If there are no conditions, the program will error when compiling |
Init | Initialize only once | Initialize after each iteration |
While loop in C / C++ with examples
Here, we have two examples with while
loop in C / C++. These are basic examples when using while
loop in C/C++.
Example 1: Displays numbers from 1 to n, with n being entered from the user.
C++ programming:
#include <iostream> using namespace std; int main() { int n, i = 0; cout<<"Enter number n: "; cin>>n; cout<<"Numbers from 1 to "<<n<<" : \n"; while(i<=n){ cout<<i<<" "; i++; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n, i = 0; printf("Enter number n: "); scanf("%d", &n); printf("Numbers from 1 to %d : \n",n); while(i<=n){ printf("%d ",i); i++; } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 2: Displaying "Welcome to learnnc.com" n times, with n being entered from the user.
C++ programming:
#include <iostream> using namespace std; int main() { int n, i = 0; cout<<"Enter number n: "; cin>>n; while(i<=n){ cout<<"Welcome to learnnc.com!!!\n"; i++; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n, i = 0; printf("Enter number n: "); scanf("%d", &n); printf("Numbers from 1 to %d : \n",n); while(i<=n){ printf("Welcome to learnnc.com!!!\n"); i++; } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
In the next tutorial, we will learn about the do...while
loop in C / C ++, please keep an eye on it. Thank youuuu !!!