Weekly questions to consider when creating your posts. These questions are a jumping off point to asking your own questions, pose new ideas, and provide explanations and examples backed by careful analysis. Apply class concepts, terms, and theories too. Refer to Assignment 12 for additional details.
- Why do we use if/else statements?
- What is the basic syntax of if/else if statements?
- What is the purpose of isNaN?
- How do we evaluate conditions if we have &&? What about ||?
- How are if statements different then else-if and else? Does an else statement take a Boolean Expression?
Sophie Chen says
We use if/else statements when we have a conditions that we want to test. For example, the condition should come first in the “if” statement. If that condition is satisfied, then the code under that block executes and the rest of the statement is skipped over. However, if the condition is not met, the console will move down and execute the code in the “else” bracket. The basic if/else syntax is as follows:
If (){
} else {
}
Alternatively, you can write more than one condition by using the “if else” statement. There can be as many conditions as you want. The “if” condition statement comes first, and then the other conditions will come under “if else” statements, followed up by an “else” if none of the conditions are met. Typically, if there are many conditions to be tested, programmers will write a “switch” statement, which essentially does the same thing but is just written a little differently.
If statements do not need an “if else” or an “else”—sometimes, you only want to run a program under a certain condition and no other conditions. “Else” statements do not take conditions or Boolean expressions as they are designed to run when no other conditions are met.
Jinzhou Situ says
Hi, Sophie,
I think your explanation of the if/else statement is very detailed. I am familiar with the specific usage and principle of the if/else statement through your narration, this seemingly simple string of characters actually contains a lot of basic knowledge about rewriting code.
Julia Grugan says
Hi Sophie, thanks for your response! I thought your explanation was very detailed, and useful to study from. Do you find one syntax easiest to write? For example. I think a switch statement is easier to write, but I have more experience writing if/else statements, so I find it difficult to change my thinking for a switch statement. Let me know what you think!
Anthony Marquis says
Hi Sophie, I thought that your post really helped me understand if statements and what it entails and what is involved when making theese what if statements overall.
Sophie Chen says
We use if/else statements when we have a conditions that we want to test. For example, the condition should come first in the “if” statement. If that condition is satisfied, then the code under that block executes and the rest of the statement is skipped over. However, if the condition is not met, the console will move down and execute the code in the “else” bracket. The basic if/else syntax is as follows:
If (*condition 1*){
*code that will execute if condition 1 is met*
} else {
*code that will execute if condition 1 is not met*
}
Alternatively, you can write more than one condition by using the “if else” statement. There can be as many conditions as you want. The “if” condition statement comes first, and then the other conditions will come under “if else” statements, followed up by an “else” if none of the conditions are met. Typically, if there are many conditions to be tested, programmers will write a “switch” statement, which essentially does the same thing but is just written a little differently.
If statements do not need an “if else” or an “else”—sometimes, you only want to run a program under a certain condition and no other conditions. “Else” statements do not take conditions or Boolean expressions as they are designed to run when no other conditions are met.
I reposted my comment because I didn’t realize some characters changed the formatting
Justin Bonfiglio says
Hey Sophie,
I’d like to say that I love the example you gave us for the basic syntax of an if/else statement. It really shows us that the if can be answered and if not the else part is 100% going to give an answer. If you wanted to extend it more or even for just one more line you can add an if/else line in the middle that will allow the code to cycle through the another option before it automatically tells you that the condition was not met. For your example its not needed, but that just a helpful piece of information that could help later on in the course work.
Madeline Elia says
Hi Sophie,
I really like your explanation of the if/else syntax. It’s very simple and straightforward, but explained well. I also like what you said about “else” statements not following other conditions or Boolean expressions, since they are basically like the last resort for the if/else statement. If/else statements can get complicated, especially when else/if’s are introduced, so keeping in mind that the stand-alone “else” signals the end of that statement can be helpful for people who are trying to read it, let alone type it.
Jinzhou Situ says
In writing code, if/else statements represent a specific line of code when a specified condition is true. The basic syntax of if/else if statements is if(Apple_eat) {
// Executes when the Apple eat is true
} else {
// Executes when the Apple eat is false
}.
The isNaN() function is used in code writing to check whether the value in the code is NaN.
When we have && in the code, we need to count the operands of the code from left to right, and if all the values are true, then return the value of the last operation. || stands for or in the code, if one or both operands are true, then || returns the Boolean value true, otherwise returns false.
If in the code, it will be executed regardless of whether the condition is met, but else if will stop the execution of the program if only one condition is met. If the same condition in the code is false, then use else to execute the code.
Julia Grugan says
Hi Jinzhou, thanks for your post! I’m still unclear as to what NaN means, could you please clear that up for me? I just looked it up, and found out it means “not a number,” but it might be useful in the future to spell that out for anyone who might not know. Also, when would this function be used? Thanks!
Justin Bonfiglio says
A basic syntax for this could be the days of the week example that said if we enter 1-7 it would give us a day and if anything else is entered its automatically not a day of the week. It looks like this.
If (day == 1) {
valueToReturn = ‘Monday’;
} else if (day == 2) {
valueToReturn = ‘Tuesday’;
and so on till you reach
} else {
valueToReturn = ‘not a valid day of the week’;
}
For the term isNaN, it is used to tell the system that what is being entered is not a real number or “Not-a-Number”. This can happen if someone enters in letters somewhere they are not supposed to be or sometimes when there are negative numbers entered.
For the operators && and ||, they stand for AND (&&) which means that the Boolean will be true if and only if all the requirements are fulfilled. On the other hand the sign || is for OR and will make the Boolean true of any of the requirements are fulfilled, even if its only one of five.
When it comes to using the different statements like if, else-if, or else, it all depends on what you are trying to achieve. If you are trying to achieve one task with no other tasks involved and if statements would be the best, but if you are trying to use check multiple things for certain answers depending on the results you use and else-if/else statement. For the case of these last else-if/else statements they also can’t take Boolean statements like an if statement can do.
Montajim Khan says
Hi Justin,
I like how you explained to the && and || operators because I had a hard time understanding them myself. Reading your post helped me understand what && and || are. Also, the if, else-if, or else were a bit confusing, but your post with your examples made things clear. Such as, one task is an if statement, else-if/else statement is multiple statements.
Justin Bonfiglio says
We use if/else statements in a way that allows us to continue a chain if the answer is false and end the chain if the answer is true. We use an example of this in the week 11 assignment when we have to determine which category to put each investor and if they are broke, not enough assets, or their score.
A basic syntax for this could be the days of the week example that said if we enter 1-7 it would give us a day and if anything else is entered its automatically not a day of the week. It looks like this.
If (day == 1) {
valueToReturn = ‘Monday’;
} else if (day == 2) {
valueToReturn = ‘Tuesday’;
and so on till you reach
} else {
valueToReturn = ‘not a valid day of the week’;
}
For the term isNaN, it is used to tell the system that what is being entered is not a real number or “Not-a-Number”. This can happen if someone enters in letters somewhere they are not supposed to be or sometimes when there are negative numbers entered.
For the operators && and ||, they stand for AND (&&) which means that the Boolean will be true if and only if all the requirements are fulfilled. On the other hand the sign || is for OR and will make the Boolean true of any of the requirements are fulfilled, even if its only one of five.
When it comes to using the different statements like if, else-if, or else, it all depends on what you are trying to achieve. If you are trying to achieve one task with no other tasks involved and if statements would be the best, but if you are trying to use check multiple things for certain answers depending on the results you use and else-if/else statement. For the case of these last else-if/else statements they also can’t take Boolean statements like an if statement can do.
Had to repost since the first part didn’t copy over from the word doc.
Cassidy Mantle says
Hi Justin,
I enjoyed reading your response but apart from that I liked how you implemented the syntax and statements that were necessary in a list form. Personally this is an easier way for me to interpret what was necessary as I am a photogenic/visual learner, therefore this layout made it easier for me to understand.
Montajim Khan says
We use if/else statement to examine if the condition is true or false. If statements is where the condition of code is true. Else statement is same condition to be false. Lastly else if is for new test if the first condition is false. The basic syntax of if/else statements is from class that helped me understand what if/else statements are.
if (something_is_true){
do something;
}else{
do_something_different
}
“isNaN” stands for is not-a-number when it’s true its value is not a number. It helps converting “the value to a number before testing it” for example
//This returns true;
isNaN(‘Hello’);
//This returns false;
Number.isNaN(‘Hello’);
To evaluate conditions “&&” “if” its true then the condition if block will be executed. False the else statement will be executed. With “||” if both are true the code inside the if statement will be executed.
Sources:
https://www.w3schools.com/jsref/jsref_if.asp#:~:text=Use%20if%20to%20specify%20a,the%20first%20condition%20is%20false
https://www.w3schools.com/jsref/jsref_isnan.asp#:~:text=isNaN()%20returns%20true%20if,a%20number%20before%20testing%20it.
Isel Sainte says
I like how you were able to provide the two links to better describe isNAN and what if/else statements are. Then you showed their syntax yourself.
Assia Snineh says
If a specified condition is true, the if/else statement executes a block of code. If the condition is not met, another block of code can be run. The if/else statement is a type of “Conditional” Statement in JavaScript, which is used to perform different actions based on different conditions. The isNaN() function in JavaScript is used to determine whether a given value is an illegal number or not. If the value is a NaN, it returns true; otherwise, it returns false. It is not the same as the Number. If the boolean evaluates to true, the if statement executes code; otherwise, the else statement executes code.
Yi-Lun Ma says
Hi Assia,
I like how you explain the isNaN() and the meaning of the if/else for the code in the JavaScript. You have explain the way how if/else work to make me know.
Madeline Elia says
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!”);
}
Cassidy Mantle says
Hi Madeline,
I enjoyed reading your response. As I stated previously the format that you used to exemplify the function and statements needed in Java/HTML makes it easier for me to actually interpret. Therefore seeing them in a list view helps me as a visual learner.
Nicholas DiGiuseppe says
Hi Madeline,
For quite some time, if/else statements stumped me tremendously, but your post does a fantastic job of clearing up some of the questions that beginners like myself might be asking. While JavaScript is not everyone’s cup of tea, after this class, I can still say that after learning about what it is and how useful it can be, it really made me appreciate what benefits it can bring for the future of the digital world and business.
Ishaan Joshi says
Hello Madeline, you did a good job breaking down what if/else statements represent. These statements were interesting because when you like at it in the context of real life applications it is interesting to see how these statements fit in. These programs can be so complex to the point that they can have all systems ready for any number of possibilities and that’s what makes it so interesting.
Yi-Lun Ma says
If/else statements are used in programming to make choices based on whether a given condition is true or false. This enables the program to run various code blocks based on the outcome of the evaluation. If/else statements are a fundamental building block of most programming languages and are employed in a variety of contexts. For instance, they can be utilized to check user input, manage errors, control program flow, or execute various code pathways based on specific conditions. In general, if/else statements let programmers write more adaptable, dynamic code that can handle many inputs or circumstances. However, they must be used carefully because too many nested if/else statements can make code hard to comprehend and maintain. In addition, it’s crucial to
Isel Sainte says
Great job, defining what if/else statements are. I like how you were able to point out why programmers are able to use them. Also, I liked how you mentioned being careful. Many times, during assignments I rushed to finish and would make a mistake and have to go back and fix it.
Aidan Morgan says
Hey Yi-Lun, I loved your analysis on if/else statements. I found your description breakdown to be very easy to follow and helpful for those who are unfamiliar with the function. I do agree that if/else statements can make programmers’ jobs easier as they do not have to stress over writing more code while the statement does the work for them.
Isel Sainte says
We use if/else statements to determine if the statement within the expression within the parentheses is true or false.
The basic syntax of if/else if statements are:
let name = prompt();
if (){
alert();
} else {
alert();
}
The purpose of isNAN is to identify if the expression is not a real number and it would be determined by displaying true or false. Conditions that have && are letting us know both the 1st and 2nd expressions are true. Now conditions with || are letting us know if either the 1st or 2nd expressions are true. The if statements are different from else if statements and else statements because if statements are what is going to be executed.
Ryan Scharko says
Hi Isel, I like your explanation on how true/false statements are utilized in the statement because it helped me understand the example you had provided a ton easier. Additionally, your explanation of isNaN was great too, I had made a post about it as well.
Aidan Morgan says
If/else statements are used to run code on dependent on a condition being true or false. The condition in an if/else statement is nested within the parentheses of the function. As the condition is passed, it will be decided whether it is true or false. If the condition is true, the code will execute one way, however, if the condition is false, the code will execute a different way. The if statement is used for a true condition, while else is used for the false condition. An example of an if/else statement could be written as
if (x == 5) {
alert(“This number is equal to 5”);
} else {
alert(“This number is not equal to 5”);
} .
With this example, the user would input a number to take the spot of x, if that number is 5 the condition will be true and the if statement will run. If it does not equal 5, the else statement will run. The use of isNaN is to identify whether or not the value used is an illegal number or not. If/else statements can be more complex by using && and II operators. The && operator means that the condition is true is when both the first and second expression of the statement are both true and the II operator is true when either the first or the second expression evaluate to true in the condition.
Robert DiBrino says
Hi Aidan! You did a great job explaining the if/else statements. The example was particularly helpful in further understanding how the statements could be written. Prior to this class I had little knowledge of coding and how complex it could be. I have found myself on a few occasions moving too quickly through an assignment, make a mistake and having to go back and fix it.
Shivam Joshi says
Why do we use if/else statements?
We use if/else statements in programming to execute different blocks of code based on certain conditions. This allows us to create programs that can make decisions and react to different inputs or situations.
What is the basic syntax of if/else if statements?
The basic syntax of if/else if statements is as follows:
if (condition1) {
// code block to execute if condition1 is true
} else if (condition2) {
// code block to execute if condition1 is false and condition2 is true
} else {
// code block to execute if all previous conditions are false
}
In this syntax, the if keyword is followed by a condition in parentheses. If this condition is true, the code block inside the curly braces will be executed. If the condition is false, the program will move on to the next condition in the else if statement. If all previous conditions are false, the program will execute the code block inside the else statement.
What is the purpose of isNaN?
isNaN is a function in JavaScript that stands for “is Not a Number”. It is used to determine whether a value is not a number. This can be useful in situations where you want to check whether a user input is a valid number, for example.
How do we evaluate conditions if we have &&? What about ||?
In JavaScript, && is the logical AND operator and || is the logical OR operator. When we evaluate conditions with &&, both conditions must be true for the overall condition to be true. When we evaluate conditions with ||, either one or both of the conditions must be true for the overall condition to be true.
Sheree Owens says
Hi Shivam! Your explanation of the basic syntax and isNan was GREAT and your example gave me a better understanding! When coding it is important to understand if user inputs are valid numbers. I also, think your example of conditional statements for true or false statements explains why code blocks are executed or not.
Sheree Owens says
Conditional statements are used to perform different actions based on different conditions. Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Example:
If the hour is less than 10, create a “Good day” greeting, otherwise “Good evening”:
if (hour < 10) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
The result of the greeting will be:
Good day
Nicholas DiGiuseppe says
If we want to test conditions, then we use the if/else statements. When we are using the “if” statement, then the condition should be applied first. If the condition gets the thumbs-up for satisfaction, then the entire rest of the statement is leaped over. Now, if the condition isn’t met, then we proceed to move down into the “else” bracket to execute the code. We also have the isNaN() function which we use in order to see if the code is indeed an NaN. When the code contains two ‘and’ symbols, (&&) then we start from left and finish to the right, counting the operands of the code. We would then return to the last operation value if every single value is true. ‘||’ is used for ‘or’ statements which means that we would return the Boolean value true as long as there is at least one operand that is true.
Lamine Karamoko says
I still have a low understanding of the if/else statements but it is a learning process. I know that based on the previous assignments, if/else statements can be used to decide based conditions or to set conditions. These condition statements would let us know if the function is true or false. If the condition happens to be true, it will continue the loop until it is proven false. If the condition happens to be false, the coding block will be exterminated. These if/else statements could be made in easy scripts, to difficult algorithms. Normally, for these conditions, you would have to make sure that it makes sense and put certain variables right before others. I personally think that this is just the surface level of what if/else statements could really do. Even now, it can still get a little bit tricky, but as they say, practice makes perfect.
Levan Lobjanidze says
Thank you for the post. From my experience, understanding if/else statements gets easier if we remember our earlier readings and exercises on swim lanes and systems. Just like swim lanes where we used if statement in diamond shapes to construct flow of process, if/else statements help us to construct JavaScript code.
Levan Lobjanidze says
If/else statements in JavaScript are used to direct code to make decisions. Before we learned about the JavaScript code, we went through the swim lanes, where we used a similar if/else method to visualize different scenarios. Like the functions in JavaScript, the if/else statements are also often used and popular. Decisions that are made are known as conditional statements, and those decisions are made based on if something is true or false. To receive a desirable result from JavaScript code, we use conditional operators. Conditional operators can be used to compare two or more things and receive a true or false answer. For conditional operators, we use different variations of = !&I symbols provided in our book. These statements can be just simple ones as well as very complex ones. More complex expressions can be written with variables, functions, raw values arranged by conditional operators. Eventually we will still get back the true or false result.
Ishaan Joshi says
If/else statements help decide based on a condition and help programs execute different actions depending on true or false states. They help control the flow of a program and make the program adaptable in constantly changing situations. If/else statements can be beneficial for companies because they can adapt to the needs of their company. If/else statements help understand the state of a specific situation and can use to make certain steps without having to reprogram because if/else statements are designed to be adaptable to all kinds of problems. An example would be checking the stock of an item being sold and displaying options to the customer accordingly. If/else statements can also help solve errors because, in the case of a mistake, an if/else statement can detect such errors and automatically guide itself to fix them or guide itself to the next step when an error is made. This is in many ways can save time and money.
Anthony Marquis says
We tend to use if and else statements only if/else statement is used to execute a block of code based on a condition. For example, When we play video games, if we click quit on the menu bar, it will ask the question “do you really want to quit this game” then if we say “yes” it will close, and if we say no the game will continue. Its a what if kind of scenario. The syntax is: if (condition1) { // code block 1 } else if (condition2) { // code block 2 } else { // code block 3 } The purpose of isNAN is used to check whether the given value is a NaN or not. If && evaluates its first operand and is false, it does not evaluate its second operand. If || evaluates its first operand as true, it does not evaluate its second operand. If statements differ then else-if by executing if the boolean expression is true */ } else { /* statement (s) will execute if the boolean expression is false */ }.
Preston Cain says
In Java script if else statements are used when the parameters of the variable have to meet a certain ‘standard’ of the function. This more so makes it so that the function you call in your work will output the correct answer. For example if your code says if the number is under 50 it must go to the else statement then a different part of the function is used instead of the first part (the if) this allows for the users to set certain parameters around a function thus making the function be able to answer multiple problems based on the if else statements being conditional of each other. The isNaN() function is used in programming as a way of saying true or false to whether the value inside of the code is a number, basically telling the code that the value is not a number.