This article will contain examples of code that make use of nested if-else statements along with arithmetic operations and assignment expressions as well as print and scan format functions.

Example 0

Nested if-else with arithmetic expression plus printf and scanf functions.

The first example calculates the percentage of your score and gives you your grade based on the calculated percentage.

#include <stdio.h>

int main() {

    int myScore;
    int maxScore;
    float percentage;
    char grade;
    char subject[150];

    printf("Enter the subject you took a test of: ");
    scanf("%[^\\n]", &subject);

    printf("Enter the overall score of the test: ");
    scanf("%d", &maxScore);

    printf("Enter your score (0-%d): ", maxScore);
    scanf("%d", &myScore);

    if (myScore < 0 || myScore > maxScore) {
        printf("Invalid score! Please enter a score between 0 and %d.\\n", maxScore);
    } else {
    
        percentage = (float)myScore / maxScore * 100;

        if (percentage >= 95) {
            grade = 'A';
        } else if (percentage >= 90) {
            grade = 'B';
        } else if (percentage >= 85) {
            grade = 'C';
        } else if (percentage >= 70) {
            grade = 'D';
        } else {
            grade = 'F';
        }

        printf("\\nYour percentage is: %.2f%%.\\n", percentage);
        
        if (grade == 'F') {
            printf("Which means your grade is %c in %s, and is below passing grade.\\n", grade, subject);
        }else if (grade == 'D') {
            printf("Which means your grade is %c in %s, and you barely passed the test.\\n", grade, subject);
        }else if (grade == 'A'){
            printf("Which means your grade is %c in %s, which also makes it an outstanding grade!\\n", grade, subject);
        }else {
            printf("Which means your grade is %c in %s.\\n", grade, subject);
        }
    }

    return 0;
}

Breakdown of the code

Variable Declaration

We’ll start by declaring variables to their datatypes:

/* 
Create/declare a 'score' and 'maxScore' variable
which will be both an integer type of variable (int).
*/
int myScore;
int maxScore;

/* 
Declare a 'percentage' variable which will be a
float type of variable (numbers that contain decimal point -> 1.50)
*/
float percentage;

/*
Lastly, declare a 'grade' and 'subject' variable 
that are both character type variables (char)
*/
char grade;
char subject[150];

Inputting Values

We’ll then enter or input data to score, maxScore, and subject using the scanf() function:

/* 

%[^\\n] is a format specifier that reads a string or alphanumeric characters
until a newline is encountered (i.e. when you press enter).
This means you can input a string with spaces in it. 

*/
printf("Enter the subject you took a test of: ");
scanf("%[^\\n]", &subject);

printf("Enter the overall score of the test: ");
scanf("%d", &maxScore);

printf("Enter your score (0-%d): ", maxScore);
scanf("%d", &myScore);

Lastly, we’ll use if-else and nested if-else statements to determine your grade based on the overall score of the test (maxScore) and your overall score of the test (myScore).

The Percentage Formula

But before we begin, let’s first determine the formula that calculates your percentage to be used in the nested if-else statement:

$$ percentage = (\cfrac{myScore}{maxScore})100 $$

Which, if you’ll convert this to our code, becomes:

percentage = myScore / maxScore * 100;

But there’s a problem! Variables myScore and maxScore, and 100 are all integer values.

So, as an example myScore / maxScore, if myScore is 75 and maxScore is 100, it becomes 0.75 if you divide them. But they’re not floats, they’re both integers, so 0.75 either becomes 0 or 1.