To write an if statement, write the keyword if, then inside parentheses () insert a boolean value, and then in curly brackets curly braces, so nothing is printed. If StatementsĪn if statement checks a boolean value and only executes a block of code if that value is true. But for now, keep in mind that boolean values contain true or false, and you can use operators like &, ||, and ! on them. There is a whole field of study devoted to boolean logic, so check that out if it sounds interesting. (If this bothers you because it doesn’t account for animals like bats, beavers, and dolphins… come up with your own isMammal variable!) Then it points the isMammal variable to that value. It then takes those values and feeds them into the & operator, which creates yet another boolean value. The and operator evaluates to true whenever the two boolean values on either side of it are also true.įirst this code takes the opposite of the canSwim and canFly variables to create two new boolean values. You can combine two boolean values using the and operator, which looks like two ampersands: &. Similar to how you can add two numbers using the + operator to get a third number, or subtract them using the - operator, you can also operate on two boolean values to get a third boolean value. First you take the inequality temperature > 80 and get a boolean value from it (in this case, true), and then you point the isHot variable to that value. If that line of code is confusing, try reading what’s on the right side of the = equals sign first. So at the end of this code, isHot is holding the boolean value of true. In this case, the inequality is true because 85 is greater than 80. Then it creates a variable named isHot and sets it equal to the result of the inequality temperature > 80. This code creates a variable named temperature and sets it equal to 85.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |