这个程序首先包含了stdio.h头文件,它提供了printf和scanf等输入输出函数。然后定义了三个函数:forLoopExample、whileLoopExample和doWhileLoopExample。每个函数都使用不同的循环结构来执行任务,并在主函数main中调用这些函数。forLoopExample函数使用for循环打印从1到10的数字。whileLoopExample函数使用while循环,根据用户...
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;} ...
The while loop loops through a block of code as long as a specified condition is true:Syntax while (condition) { // code block to be executed }In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:...
// test expression while(i<6){ printf("Hello World "); // update expression i++; } return0; } C++实现 // C++ program to illustrate while loop #include<iostream> usingnamespacestd; intmain() { // initialization expression inti=1; // test expression while(i<6){ cout<<"Hello World...
Flowchart of do...while Loop 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:...
While Loop Examples: Example 1: /* echo.c -- repeats input */#include <stdio.h>intmain(void){intch;/* * 1. getchar() function reads-in one character from the keyboard * at a time * 2. return type of getchar() is int * 3. getchar() returns integer value corresponding to cha...
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...
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++;
For example, the factorial of 3 is 3 * 2 * 1 = 6. Return the factorial of the input number num. 1 2 int factorial(int num){ } Check Code Video: C for Loop Previous Tutorial: C if...else Statement Next Tutorial: C while and do...while Loop Share on: Did you find th...
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. $ ./factorial 3628800 C while - endless loop Thewhile (1)creates an endless loop. In order to terminate the loop, we use ...