If/else statements run code based on whether a condition is true or false. If the condition of the first statement is the “if” statement, the code will execute that statement. If the conditions are not met, then it will go to the “else” statement and execute that code. You can use if/else statements over multiple conditions by using “if else”. We used this code during the day-of-the-week challenge where a certain number typed in would equate to the day of the week. The basic syntax of an if/ else statement looks like this:
if( this condition is met) {
do this;
} else {
Do this;
}
“If” statements do not always need and “else” if you are only worried about meeting one condition. When you want to check for multiple conditions, you would then use “if/else” statements. It is important to note that “else” statements cannot take the form of a Boolean. Boolean can only golf two possible values, true or false.
Leave a Reply
You must be logged in to post a comment.