If/else statements are used to figure out if a condition is true or false. They play an important role when there are multiple decisions involved. An if/else statement is basically set up like this:
if (something_is_true) {
do something;
} else {
do_something_different;
}
The word “if” is used to identify the expression that will be evaluated. In addition, “else” is used to identify what will happen whether the expression is true or false (the decisions that are involved in the process). The program will run through the statement and check if the initial statement is true. If not, it will move on to the first else statement. It will go down the list until it reaches something that meets the criteria of the if statement. If/else statements can sometimes get confused with else/if statements. Although they do sound similar, there is a difference. An else/if statement is when you have an if/else statement within another. For example:
if (position = 200) && (position < 300)) {
alert (“Do something else!”);
} else {
alert (“Do something even more different!”);
}
Hi Madeline! Your answer was similar to mine! I love how you put an example when talking about if/else statements, it helps the reader understand more clearly.
Hello Madeline,
I found the examples you provided very helpful when reading through your post. Coding is one of those topics that is really hard to understand if you do not have a visual representation or an example of it. When writing my posts I found it very hard to try and explain some of the topics without including an example, so I found one online and used it in hopes of helping my classmates understand my post a little better. Also, you included in there to not get mixed up with an else/if statement which I think is very important when writing these codes and understanding the output of each.