while
loop statement, the program is ended only when satisfying given condition.while
loop statement.#include <stdio.h>
int main() {
int c=0;
// while loop
while(c < 20){
c = c + 1;
printf("c = %d\n", c);
}
return 0;
}
while(...)
is satisfied, the process written in {...}
is run, repeatedly.c = c + 1
, the value in c
increases by 1. When c
reaches 20, it exists out of while
loop statement.#include <stdio.h>
int main() {
int a;
// while loop
while(1){
// input value
printf("Input a = ");
scanf("%d", &a);
}
return 0;
}
1
to condition of while
statement, the condition is always satisfied. Therefore, it cannot exsit while
statement.#include <stdio.h>
int main() {
int a;
// while loop
while(1){
// input value
printf("Input a = ");
scanf("%d", &a);
// break statement
if(a == 0){
printf("---end---");
break;
}
}
return 0;
}
break
, it can exit from while
statement.0
from keyboard into variable a
.#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int target_number, your_select;
int min_n = 1;
int max_n = 100;
// make target number
srand(time(0));
target_number = rand() % (max_n - min_n + 1) + min_n;
printf("--- Guess the number between 1 and 100 ---\n");
while(1){
// selecting your number
printf("Select your number: ");
scanf("%d", &your_select);
// check correct or not
if(your_select == target_number){
printf("Correct! The number was %d.\n", target_number);
break;
}else if(your_select < target_number){
printf("Too low! Try again:\n");
}else{
printf("Too high! Try again:\n");
}
printf("-------------------------------------\n");
}
return 0;
}
target_number
.scanf
function.while(1)
the player can repete input.If
statement, if(your_select == target_number)
.else-iIf
statement, if(your_select < target_number)
.#include <stdio.h>
int main() {
int input_num;
int total_num = 0;
while (1) {
printf("Input number = ");
scanf("%d", &input_num);
total_num = total_num + input_num;
printf("Now total = %d \n", total_num);
printf("---------------\n");
}
return 0;
}
0
to keyboard.while
statement, If
statement, and so on. Also, include a explanation of it.