What happens if no conditions are true?

QuestionsQuestions8 SkillsProPython Control StructuresOct, 21 2025
088

If no conditions are true in a series of if, else if, and else statements, the code inside the else block (if it exists) will be executed. If there is no else block, then no action will be taken, and the program will simply continue executing any code that follows the conditional statements.

Here’s an example in C:

#include <stdio.h>

int main() {
    int score = 50;

    if (score >= 90) {
        printf("Excellent! Your grade is A.\n");
    } else if (score >= 80) {
        printf("Great job! Your grade is B.\n");
    } else if (score >= 70) {
        printf("Good work. Your grade is C.\n");
    } else if (score >= 60) {
        printf("You passed. Your grade is D.\n");
    } else {
        printf("Sorry, you failed. Your grade is F.\n");
    }

    return 0;
}

In this example:

  • If score is 50, none of the conditions in the if or else if statements will be true.
  • The program will execute the code in the else block, resulting in the output "Sorry, you failed. Your grade is F."

If there were no else block, the program would simply skip the conditional statements and continue with any subsequent code.

0 Comments

no data
Be the first to share your comment!