Basic C programming 4: While loop statement¶

Yuto Omae @ Nihon University, Japan¶

  • We use online compiler and debugger for c/c++.
  • https://www.onlinegdb.com/online_c_compiler
  • previously explained programs are ended immediately.
  • By using while loop statement, the program is ended only when satisfying given condition.

Code 1: simple while loop¶

  • The following is most simple code including 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;
}

Key points¶

  • When the condition of while(...) is satisfied, the process written in {...} is run, repeatedly.
  • By the statement c = c + 1, the value in c increases by 1. When c reaches 20, it exists out of while loop statement.

Works¶

  • Change condition in while statement and check a result.

Code 2: infinite loop¶

  • The following is a dangerous code called infinite loop.
#include <stdio.h>
int main() {
    int a;

    // while loop
    while(1){
        // input value
        printf("Input a = ");
        scanf("%d", &a);
    }

    return 0;
}

Key points¶

  • If we give 1 to condition of while statement, the condition is always satisfied. Therefore, it cannot exsit while statement.
  • The above code is for input number from keyboard, endlessly.
  • Because the process of this program is never ended, it must be stopped by force end.

Code 3: break statement¶

  • The following is the code that has function for exsit infinit loop.
#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;
}

Key points¶

  • By using break, it can exit from while statement.
  • The above code is ended if we input 0 from keyboard into variable a.

Code 4: number-guessing game¶

  • This code is number-guessing game. By input number from keyboard and matching it to target number, the program is ended.
#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;
}

Key points¶

  • The randomly generated target number is given to the variable target_number.
  • The player select number from keyboard by scanf function.
  • By while(1) the player can repete input.
  • It check whether the player's selection and target value are same or not by If statement, if(your_select == target_number).
  • When the player's selection is lower than target value, the system show Too low by else-iIf statement, if(your_select < target_number).

Assignments¶

  • (1) Explain the function of the following code.
#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;
}
  • (2) The above code cannot be ended. Add the code for end when input 0 to keyboard.
  • (3) Create a practical program or a simple game using the while statement, If statement, and so on. Also, include a explanation of it.