If you're seeing this message, it means we're having trouble loading external resources on our website. Show
If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked. You’ve seen that values have different types. A type tells the computer what kind of data a value is, or what type of value a variable holds. So far you’ve mostly worked with the 1 type.This tutorial introduces the 2 type, which can only hold two possible values: 3 or 4. This tutorial also introduces if statements, which allow you to perform different actions depending on the value of a boolean.Remember that a type tells the computer what kind of value a variable will hold. For example, the 5 type holds whole numbers, the 1 type holds decimal numbers, and the 7 type holds text. You create a variable by giving it a type, a name, and then a value:
You can create a 2 variable the same way, but it can only hold two possible values: 3 or 4.This might not seem very useful yet, but it will become more handy in a couple paragraphs. Keep reading! 1 variables wouldn’t be very useful if you had to decide their values ahead of time. Instead, you can obtain 2 values through relational operators. You might have called these inequalities in algebra class, where you compared two sides with symbols like 3 less than, 4 greater than, or 5 equal to. Here’s an example:
This code creates a 1 variable named 7 and sets it equal to 8. It then creates a 2 variable named 0 and sets it equal to the result of the inequality 1. In this case, the inequality is 3 because 8 is greater than 4. So at the end of this code, 0 is holding the 2 value of 3.If that line of code is confusing, try reading the right side first. First you take the inequality 1 and get a 2 value from it (in this case, 3), and then you point the 0 variable to that value.Similar to how you can add two 1 values using the 3 operator to get a third 1 value, or subtract them using the 5 operator, you can also operate on two 2 values to get a third 2 value.AndYou can combine two 2 values using the and operator, which looks like two ampersands: 9. The and operator evaluates to 3 whenever the two 2 values on either side of it are also 3.
The 3 variable will only be 3 when both 5 and 6 are also true. If either one of them is false, then 3 will also be false.Again, it might make more sense to read the right side first. First the code evaluates the 9 operator, which creates a 2 value of 3. Then it points the 3 variable to that value.OrThe or operator evaluates to true if either of the two 2 values on either side of it is 3.To use the or operator, type two pipes 4 (they’re above the enter key, or 5) between two 2 values:
The 7 variables will be 3 if either the 9 or 0 variables are 3.First the code evaluates the 4 operator which creates a 2 value (in this case it’s 3), and then it points the 7 variable to that value.NotIn addition to operating on two 2 values, you can also calculate the opposite of a single 2 value. The opposite of 3 is 4, and the opposite of 4 is 3.This is called the not operator, and you use it by typing an exclamation point 2 before the value you want to switch.
Each line of this code evaluates the 2 operator which creates a new 2 value based on the opposite of whatever follows it, and then it points a 2 variable to that value.Combining OperatorsYou can also combine these operators to form more complicated logic. So you can do things like this:
First this code takes the opposite of the 5 and 6 variables to create two new 2 values. It then takes those values and feeds them into the 9 operator, which creates yet another 2 value. Then it points the 1 variable to that value.(If this bothers you because it doesn’t account for animals like bats, beavers, and dolphins… check out the homework!) There is a whole field of study devoted to 2 logic, so check that out if it sounds interesting. But for now, keep in mind that 2 values contain 3 or 4, and you can use operators like 9, 4, and 2 on them.An 9 statement checks a 2 value and only executes a block of code if that value is 3.To write an 9 statement, write the keyword 9, then inside parentheses 04 insert a 2 value, and then in curly brackets 06 write the code that should only execute when that value is 3. That code is called the body of the 9 statement.Here’s an example that draws a congratulations message, but only if your grade is an A:
This code uses an 9 statement to check whether 0 is 3, and if it is, it draws a green background, changes the fill color to black and the text size to 12, and then writes 13 on the screen.Since 7 is 8 (and 8 is greater than 4), then you see the congratulations message:If 0 is 4 (if 7 is less than 4), then the program doesn’t do anything. Try changing 7 to 23, and you’ll see a blank window:Since 0 is now 4, the body of the 9 statement is not executed, and it skips over all of the code inside the body.Boolean ExpressionsSo far, all of the examples have separated the inequality and the 9 statement into two steps: the code first created a 2 variable from an inequality, and then it used that variable in an 9 statement. But you can combine them into a single step:
This does the exact same thing as the old code, except now the inequality (which evaluates to a 2 value) is inside the 9 statement instead of being split into its own step. Either approach is fine, so you should use whichever style makes the most sense to you.An 9 statement executes some code if its 2 value is 3, and it skips that code if the 2 value is 4. But what if you want to do one thing if the value is 3 and a different thing if it’s 4? Sounds like a job for an 39 statement!To use an 39 statement, type the 39 keyword after an 9 statement, and then inside curly brackets 06 put the code you want to execute when the 9 statement’s 2 evaluates to 4:
This code uses an 9 statement to check whether 7 is greater than or equal to 4. Since 23 is less than 4, that inequality is 4, so the code inside the 9 statement is skipped. Instead, the program jumps to the code inside the body of the 39 statement, which draws a red background and displays the “Study more!” message.You can think about the code like this: “If the score is greater than or equal to 90, then display the ‘Congratulations!’ message. Otherwise, display the ‘Study more!’ message instead.” An 9 statement executes some code if its 2 evaluates to 3, and an 39 statement executes code if it evaluates to 4. But what if you want to take different actions depending on multiple cases? This is where 60 statements come in handy.An 60 statement is like a mix between an 39 statement and an 9 statement. You put an 60 statement after an 9 statement, and if the 9 statement evaluates to 4, then the 60 statement’s 2 is evaluated:
This code uses an 9 statement to check whether 7 is greater than or equal to 4. Since 23 is less than 4, that inequality is 4, so the code inside the 9 statement is skipped. The program jumps down to the 60 statement and checks the 2 expression inside the 60 statement. Since 23 is greater than 81, the inequality is 3, and the code inside the body of the 60 is executed. The code then draws a blue background and and displays the “Good job!” message.You can think about the code like this: “If the score is greater than or equal to 90, then display the ‘Congratulations!’ message. Otherwise check whether the score is greater than or equal to 80. If it is, then display the `Good job!’ message instead.” If 7 was 8, then only the code inside the body of the 9 statement would run. The code inside the 60 statement would be skipped. And if 7 was 89, then the 2 expressions of both the 9 statement and the 60 statement would evaluate to 4, so neither one of their bodies would be executed.You can follow an 9 statement with multiple 60 statements, and you can follow an 60 statement with an 39 statement. So you could expand your program to detect every grade: 0This code uses a series of 9, 99 and 39 statements to run this logic:
Try changing the 7 variable to see the different messages.Avoiding Unnecessary ChecksKeep in mind that only one body of code is entered in a series of 06 statements. As soon as the code enters one of the 9 or 60 statements, it stops checking subsequent 60 statements.Look at this line of code, from the above program: Notice that the code only checks whether 02. But for the grade to be a B, it also has to be 11. Why doesn’t the code also check for that?The code doesn’t check to make sure that 12, because it already knows that it is. If 7 was 14, then the first 9 statement would have been entered, and the code wouldn’t even reach this line.Think about it this way: first the code checks whether the grade is an A. If it is, then it displays the “Congratulations!” message and doesn’t check any other grades since it already knows the grade is an A. If it’s not an A, then it knows that 12 and it keeps checking other grades.That’s why the code can check whether the grade is a B by only checking whether 17. It it is, then it displays the “Good job!” message and doesn’t check against any other grades since it already knows the grade is a B. If it’s not a B, then it knows that 18, and it continues that pattern for the rest of the program.
When you code an if statement within another if statement The statements are?A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
When you code an if statement within another if statement The statements are quizlet?Terms in this set (20) When you code an if statement within another if statement, the statements are nested.
When an if statement is nested in the else clause of another statement?When an if statement is nested in the else clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true. The scope of a variable is limited to the block in which it is defined.
What is the nested if statement?Nested IF functions, meaning one IF function inside of another, allows you to test multiple criteria and increases the number of possible outcomes. We want to determine a student's grade based on their score. If Bob's score in B2 is greater than or equal to 90, return an A.
|