C Programming Tutorial 51 -If-Else Statement

In the last blog we went over the If Statement Guessing Game! Check it out first! Are you new here? Start at the beginning, Intro to C!

In the previous blog we had this code:

The problem here is that we are calling return 0 twice. This works in this situation, but what if we wanted to continue executing the program. For example, what if we wanted to do something like this:

Now, we cannot just end the program inside of the if body. This is where the else statement comes in. The else clause is essentially an expansion pack for the if statement that allows you to make a code block if the if expression is false:

Now, only one will be executed.  

This entire thing as a group is often called the if-else statement and allows for either true or false. Later on we will learn about an if statement that allows for multiple cases. This is called the if else-if statement. If we used that, we might be able to have cases such as the guess being too low, the guess being two high, or the guess being just right. Just to give you a sneak peak of what is to come. Read the next blog now!

Leave a Reply

Your email address will not be published. Required fields are marked *