The program is an example ofinfinite while loop. Since the value of the variable var is same (there is no ++ or – operator used on this variable, inside the body of loop) the condition var<=2 will be true fore
The update expression is written inside the body of the loop to increment/decrement the variable value, used in the test expression. If this is missing, the loop will run infinitely.C++ While Loop Program Example#include <iostream> using namespace std; int main(){ int i = 1; while (i ...
Flowchart of while Loop Flowchart of C++ while loop Example 1: Display Numbers from 1 to 5 // C++ Program to print numbers from 1 to 5#include<iostream>usingnamespacestd;intmain(){inti =1;// while loop from 1 to 5while(i <=5) {cout<< i <<" "; ++i; }return0; } ...
Example 2: do...while loop // Program to add numbers until the user enters zero #include <stdio.h> int main() { double number, sum = 0; // the body of the loop is executed at least once do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(...
1. while Loop in C While loop executes the code until the condition is false. Syntax: while(condition){ //code } Example: #include<stdio.h> void main() { int i = 20; while( i <=20 ) { printf ("%d " , i ); i++; } } Output: 20...
Example 1inti=0;//loop counter initialized outside of loop 2 3while(i<5)// condition checked at start of loop iterations 4{ 5printf("Loop iteration %d\n", i++);//(i++) Loop counter incremented manually inside loop 6} The expected output for this loop is: ...
Example: Python 1 2 3 4 5 6 7 a = 1 while a < 5: print("condition is true") a = a + 1 else: print("condition is false now") Output: In the above example, the program keeps executing the body of the while loop till the condition is true, meaning that the value of a is...
Now, just as we saw in the for loop, there can be various ways in which we can achieve this result.C while Loop: Example 2Print integers from 1 to 5 using the while loop.#include <stdio.h> int main() { int i; //loop counter //method 1 printf("Method 1...\n"); i=1; ...
Example: For loop inC Compiler // Program to print numbers from 1 to 10#include<stdio.h>intmain(){inti;for(i =0; i <10; i++) {printf("%d\n", i+1); }return0; } Run Code >> The above code prints the numbers from 1 to 10 using aforloop in C. ...
This is an example of while loop in C programming language - In this C program, we are going to print all lowercase alphabets from 'a' to 'z' using while loop.