Basic C programming 3: If statement and complex condition¶

Yuto Omae @ Nihon University, Japan¶

  • We use online compiler and debugger for c/c++.
  • https://www.onlinegdb.com/online_c_compiler

Comparison Operators (See Basic C programming 2):

  • a > b: Whether a is greater than b.
  • a >= b: Whether a is greater than or equal to b.
  • a < b: Whether a is less than b.
  • a <= b: Whether a is less than or equal to b.
  • a == b: Whether a and b are equal.
  • a != b: Whether a and b are not equal.

Combination of multiple condition:

  • condition 1 && condition 2: Whether both of the two conditions are satisfied.
  • condition 1 || condition 2: Whether one of the two conditions is satisfied.

Code 1: AND operator (&&)¶

  • The following is code that is processed only when two conditions are satisfied.
#include <stdio.h>
int main(){
    int a1, a2;

    // Input two value
    printf("Input two values a1 and a2\n");
    scanf("%d", &a1);
    scanf("%d", &a2);

    // If statement
    if(a1 > 3 && a2 > 3){
        printf("a1 and a2 are greater than 3.");
    }else{
        printf("not satisfied");
    }

    return 0;
}

Key points¶

  • a1 > 3 && a2 > 3 is condition consisting of a1 > 3 and a2 > 3.
  • Only when all conditions are satisfied, the code in If statement is processed.
  • By using && operator, we can arrange multiple conditions, e.g., a1 > 3 && a2 > 3 && a3 > 3 &&....
  • We can arrange any number of conditions.

Works¶

  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 2: OR operator ( | | )¶

  • The following is the code that is processed when at least one in the two conditions is satisfied.
#include <stdio.h>
int main(){
    int a1, a2;

    // Input two value
    printf("Input two values a1 and a2\n");
    scanf("%d", &a1);
    scanf("%d", &a2);

    // If statement
    if(a1 > 3 || a2 > 3){
        printf("a1 or a2 is greater than 3.");
    }else{
        printf("not satisfied");
    }

    return 0;
}

Key points¶

  • a1 > 3 || a2 > 3 is the condition consisting of a1 > 3 and a2 > 3.
  • At least one in the multiple conditions is/are satisfied, the code in If statement is processed.
  • We can arrange any number of conditions by using || operator, e.g., a1 > 3 || a2 > 3 || a3 > 3 ||....

Works¶

  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 3: Nest of IF statement¶

  • The following is code that includes If statement in If statement.

#include <stdio.h>
int main(){
    int a1, a2;

    // Input two value
    printf("Input two values a1 and a2\n");
    scanf("%d", &a1);
    scanf("%d", &a2);

    // first If statement
    if(a1 > 3){
        printf("a1 is greater than 3.\n");
        // second If statement
        if(a2 > 3){
            printf("a1 and a2 are greater than 3.");
        }
    }else{
        printf("not satisfied");
    }

    return 0;
}

Key points¶

  • By first If statement, the first condition a1 > 3 is checked. When the first condition is satisfied, the second condition a2 > 3 is checked by the second If statement.
  • Codes which include If statement in If statement are called Nest.

Works¶

  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 4: Get random number¶

  • A program that always outputs the same result is boring.
  • The following is code for generating random number. By using this, the progam can output different results each time.

#include <stdio.h>
#include <stdlib.h> // for "rand" function
#include <time.h> // for "time" function 

int main() {
    srand(time(0)); // base random seed (only write one time)
    int r;
    int max_rand = 5;
    int min_rand = 3;

    // 1st random
    r = rand() % (max_rand - min_rand + 1) + min_rand; // generate random number
    printf("r = %d\n", r);

    // 2nd random
    r = rand() % (max_rand - min_rand + 1) + min_rand; // generate random number
    printf("r = %d\n", r);

    return 0;
}

Key points¶

  • Write #include <stdlib.h>, #include <time.h>, and srand(time(0)); only one time.
  • By writing rand() % (max_rand - min_rand + 1) + min_rand, random number from min_rand to max_rand can be generated.

Works¶

  • By running the above code, check for generating random numbers.
  • Change min_rand and max_rand, run the code, and check results.

Code 5: Random number and If statement¶

  • By using random number and If statement, we can make simple game.
  • The following is the code that when the person get higher random value is winner.

#include <stdio.h>
#include <stdlib.h> // for "rand" function
#include <time.h> // for "time" function 

int main() {
    // random settings
    srand(time(0));
    int max_rand = 6;
    int min_rand = 1;

    // scores
    int your_score;
    int npc_score;

    // getting scores
    your_score = rand() % (max_rand - min_rand + 1) + min_rand;
    npc_score = rand() % (max_rand - min_rand + 1) + min_rand;

    // result
    printf("You: %d \n", your_score);
    printf("NPC: %d \n", npc_score);

    if(your_score > npc_score){
        printf("You win!");
    }else if(npc_score > your_score){
        printf("NPC win!");
    }else if(npc_score == your_score){
        printf("Same score");
    }

    return 0;
}

Key points¶

  • rand() function gives random values to integer variables your_score and npc_score.
  • The winner is identified by Ifstatement.
  • In the case of same score, the text "Same score" is shown by printf function.

Works¶

  • Run several times and check results.

Assignments¶

  • (1) Define three integer variables a、b、c. After that, input specific values from keyboard into these variables and when all values are same, show same values.
  • (2) Define three integer variables a、b、c. After that, input specific values from keyboard into these variables and when all values are not same, show Different values.
  • (3) Two players roll six-sided dic three times and calculating total values. Write the code that assigns the higher total value as the winner. When two playes' scores are same, show Same score by using printf function.
  • (4) Add the function for input the player names in ealry pahse of program by using scanf function, and show winners name.
  • (5) Generate two random numbers and show the maximum value. If there are two maximums, show two.
  • (6) Generate three random numbers and show the maximum value. If there are two or three maximums, show two or three.