if-else Statement

Learn how the if-else statement works in C++.

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 60% is D

  • 60% to 70% is C

  • 70% to 80% is B

  • 80% to 90% is A

  • Above 90% is A+

You can not put all the conditions in a single if statement. You can put conditions for each in a different if statements by the knowledge we have at the moment. Let's visualize it using a coding example:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy