Basic C programming 2: If statement and single condition¶

Yuto Omae @ Nihon University, Japan¶

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

Comparison Operators:

  • 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.

Code 1: If statement¶

By using if statements, we can implement processing that depends on the state of variables.


#include <stdio.h>
int main(){
    int a = 5;
    printf("a is %d \n", a);

    if(a > 3){ // If statement
        printf("a is greater than 3.");
    }

    return 0;
}

Key points¶

  • The above code processes the printf function when a is greater than 3.

Works¶

  • Set int a = 2 and check for whether printf function is processed or not.
  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 2: If-else statement¶

To add a process for when the condition is not satisfied, use else statement.


#include <stdio.h>
int main(){
    int a = 1;
    printf("a is %d \n", a);

    if(a > 4){ // If statement
        printf("a is greater than 4.");
    }else{ // else statement
        printf("a is not greater than 4.");
    }

    return 0;
}

Key points¶

  • The above code does not satisfy a>4. Therefore, only the inside of the else statement is run.

Works¶

  • Set int a = 5 and check result.
  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 3: If-elseif statement¶

  • If the condition written in if statement is not satisfied, we can give another condition by using elseif statement.

#include <stdio.h>
int main(){
    int a = 1;
    printf("a is %d \n", a);

    if(a > 4){ // If statement
        printf("a is greater than 4.");
    }else if(a > 2){ // else if statement
        printf("a is greater than 2.");
    }else if(a > 0){ // else if statement
        printf("a is greater than 0.");
    }

    return 0;
}

Key points¶

  • Any number of else if statements can be used.

Works¶

  • Set various values to a and check results.
  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 4: If-elseif-else statement¶

  • An else statement can be added to an If-elseif statement.

#include <stdio.h>
int main(){
    int a = -10;
    printf("a is %d \n", a);

    if(a > 4){ // If statement
        printf("a is greater than 4.");
    }else if(a > 2){ // else if statement
        printf("a is greater than 2.");
    }else if(a > 0){ // else if statement
        printf("a is greater than 0.");
    }else{ // else statement
        printf("a is NOT greater than 0");
    }

    return 0;
}

Key points¶

  • The else statement is added at the end of all conditions.
  • The reason is the else statement is the process for that all conditions are not satisfied.

Works¶

  • Set various values to a and check results.
  • Write code including other comparison opereters, e.g., >=, <, <=, ==, !=.

Code 5: Combination of scanf and if statement¶

The following is code for input value from keyboard and substituting it to a, after that, checking whther a is greater than 4 or not.


#include <stdio.h>
int main(){

    // input from key board
    int a;
    printf("Input: ");
    scanf("%d", &a);
    printf("a is %d \n", a);

    // If-else statement
    if(a > 4){
        printf("a is greater than 4.");
    }else{
        printf("a is NOT greater than 4");
    }

    return 0;
}

Assignments¶

  • (1) Enter the two double-type variables, after that, calculate division / of them. If calculation result is greater than 5, display greater than 5. If it is not satisfied that condition, display not satisfied.
  • (2) Explain the detail of the following programs.
#include <stdio.h>
int main(){
    double a1, a2, result;
    int ope;

    // Select operator
    printf("Select operator [1: +, 2: -, 3: *, 4: /]\n");
    scanf("%d", &ope);

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

    // Results
    if(ope == 1){
        result = a1 + a2;
        printf("a1 + b2 = %lf", result);
    }else if(ope == 2){
        result = a1 - a2;
        printf("a1 - b2 = %lf", result);
    }else if(ope == 3){
        result = a1 * a2;
        printf("a1 * b2 = %lf", result);
    }else if(ope == 4){
        result = a1 / a2;
        printf("a1 / b2 = %lf", result);
    }else{
        printf("Warning: Select only from 1 to 4! \a");
    }

    return 0;
}
  • (3) Enter the width and height by using scanf, after that solving the area of a triangle or rectangle. The user can select a triangle or rectangle from keyboard like assignment (1).

  • (4) Create a slightly complex and practical code using if, if-else, if-elseif or if-elseif-else statements and various comparison operator.

Submission: Please submit following materials.

  • (A) a screenshot of the program code
  • (B) a screenshot of the running results
  • (C) Detailed explanation by text.
  • Note: Please use the submission function of oma portal system, not use word and pdf file.