ExampleIn this example, the while loop is used as a conditional loop. The loop continues to repeat till the input received is non-negative.Open Compiler #include <stdio.h> int main(){ // local variable definition char choice = 'a'; int x = 0; // while loop execution while(x >= ...
这个程序首先包含了stdio.h头文件,它提供了printf和scanf等输入输出函数。然后定义了三个函数:forLoopExample、whileLoopExample和doWhileLoopExample。每个函数都使用不同的循环结构来执行任务,并在主函数main中调用这些函数。forLoopExample函数使用for循环打印从1到10的数字。whileLoopExample函数使用while循环,根据用户...
// C program to illustrate while loop #include<stdio.h> intmain() { // initialization expression inti=1; // test expression while(i<6){ printf("Hello World "); // update expression i++; } return0; } C++实现 // C++ program to illustrate while loop #include<iostream> usingnamespace...
Case1 (Normal):Variable ‘i’ is initialized to 0 before ‘do-while’ loop; iteration is increment of counter variable ‘i’; condition is to execute loop till ‘i’ is lesser than value of ‘loop_count’ variable i.e. 5. Case2 (Always FALSE condition): Variables ‘i’ is initialized...
C while Loop: Example 1 Input two integers and find their average using while loop. #include<stdio.h>intmain(){inta,b,avg,count;count=1;while(count<=3){printf("Enter values of a and b:");scanf("%d%d",&a,&b);avg=(a+b)/2;printf("Average =%d",avg);}return0;} ...
Working of do...while loop 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...
while loop Flowchart Syntax while(test condition){ //code to be executed } If the test condition inside the () becomes true, the body of the loop executes else loop terminates without execution. The process repeats until the test condition becomes false. Example: while loop in C // ...
factorial.c #include <stdio.h> int main() { int i = 10; int factorial = 1; while (i > 1) { factorial *= i; i--; } printf("%d\n", factorial); return 0; } In the example, we use the while loop to calculate the 10! factorial. ...
While loop Do while loop For loop 1. While Loop While Loop is used when we do not already know how often to run the loop. In While Loop, we write the condition in parenthesis “()” after the while keyword. If the condition is true then the control enters the body of the while Lo...
Let's see another example. Program to print first 10 natural numbers usingwhileloop #include<stdio.h> void main( ) { int x; x = 1; while(x <= 10) { printf("%d\t", x); /* below statement means, do x = x+1, increment x by 1 */ ...