Search⌘ K

if-else Statement

Explore how to use if-else and if-else-if statements in C++ to manage multiple conditions and control program flow. Learn to increase code efficiency by executing only the relevant code blocks based on conditions, and understand their use in scenarios like grading systems.

The simple if statement consists of a single code block that would be run if the condition is met; otherwise, it will be skipped. What if there are multiple blocks with their own selection criteria? Can we use multiple if statements, one after the other? Let’s find out.

Multiple if statements

There are many scenarios where you have to select between multiple conditions and select the code to execute based on the condition met. For instance, consider you have been given a task to create a grading system for school, and you have the following criteria for grades:

  • Less than 50% is F

  • 50% to 59% is D ...