0) printf("You picked a positive number!\n"); } Conditions are important as they allow us to introduce logic into our programs. Note how the code within this if block will only execute if the condition n > 0 evaluates to true. Conditions of if blocks are always boolean expressions that evaluate to either true or false. We’ll dive into boolean conditions in more detail on the next slide!"> 0) printf("You picked a positive number!\n"); } Conditions are important as they allow us to introduce logic into our programs. Note how the code within this if block will only execute if the condition n > 0 evaluates to true. Conditions of if blocks are always boolean expressions that evaluate to either true or false. We’ll dive into boolean conditions in more detail on the next slide!">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditions and Boolean Expressions

Similar presentations


Presentation on theme: "Conditions and Boolean Expressions"— Presentation transcript:

1 Conditions and Boolean Expressions
The conditionals and boolean expression blocks you've used in Scratch can all be recreated in C!

2 If #include <cs50.h> #include <stdio.h> int main(void) {
printf("Give me an integer: "); int n = GetInt(); if (n > 0) printf("You picked a positive number!\n"); } Conditions are important as they allow us to introduce logic into our programs. Note how the code within this if block will only execute if the condition n > 0 evaluates to true. Conditions of if blocks are always boolean expressions that evaluate to either true or false. We’ll dive into boolean conditions in more detail on the next slide!

3 Boolean Expressions <= > Evaluates to either true or false.
>= == != ! Boolean expressions like the ones on the left can only evaluate to boolean values of true or false. You can use them to compare values, e.g. 4 < 3 evaluates to false and 4 <= 3 evaluates to true. Note that == is used to compare two numbers for equality. ! means "not", and negates the values of a boolean expression. So !(4 < 3) evaluates to true. Additionally, in C, anything with a value of zero (e.g. int i = 0) evaluates to false, and any other value evaluates to true.

4 Combining Boolean Expressions
Logical OR: || if (x < 0 || x > 100) { printf("invalid\n"); } Logical AND: && if (x >= 0 && x <= 100) { printf("valid\n"); } You can combine boolean expressions using the logical AND (&&) and OR (||) operators. Here, invalid will print if the value of x is less than 0 or greater than 100, and valid will print if the value of x is between 0 and 100, inclusive.

5 If... Else int main(void) { printf("Give me an integer: ");
int n = GetInt(); if (n > 0) printf("You picked a positive number!\n"); } else printf("You picked a negative number!\n"); We’ve seen that an if block can stand alone. It can also be paired with else. If n > 0 does not evaluate to be true, the code within else will execute. The important thing to remember is that the code within the if and else blocks are exclusive of each other: the else block will never execute if the if block already did! This may seem trivial for now, but you'll soon see how this logic can get a bit confusing. But anyway, back to the code! 0 is not a negative number, so this program is flawed. We’ll fix it on the next slide.

6 If... Else if... Else int main(void) { int n = GetInt(); if (n > 0)
printf("You picked a positive number!\n"); } else if (n < 0) printf("You picked a negative number!\n"); else printf("You picked 0!\n"); The if... else construct can be extended to include three or more code blocks as in this slide's example. Again, there's no way that more than one of those three printf statements can possibly execute, because once a condition is found to be true and its associated code executes, none of the other conditions will even be evaluated.

7 printf("Enter your grade: "); int n = GetInt(); if (n > 90)
int main(void) { printf("Enter your grade: "); int n = GetInt(); if (n > 90) printf("You got an A!\n"); } if (n > 80) printf("You got a B!\n"); if (n > 70) printf("You got a C!\n"); In contrast, multiple if blocks are not necessarily exclusive. What will print if I enter the number 95? Why? How can we fix this bug?

8 Switch Statements int main(void) {
printf("Give me an integer between 1 and 3: "); int n = GetInt(); switch (n) case 1: printf("You picked a low number.\n"); break; case 2: printf("You picked a medium number.\n"); case 3: printf("You picked a high number.\n"); default: printf("Invalid.\n"); } This syntax is called a switch statement. A switch statement takes a variable (n in this case) and determines which case statement will execute based on the value of that variable. Finally, default defines behavior when no other case statement is executed. ** Note that switch statements can also be used on characters instead of integers. **

9 Ternary Operator #include <cs50.h> #include <stdio.h>
int main(void) { printf("Give me an integer: "); int n = GetInt(); string s = (n > 100) ? "high" : "low"; printf("You picked a %s number!\n", s); } The ternary operator ?: is another kind of conditional. Here's how it works: If n > 100 is true, the value to the left of the colon "high" is assigned to s. If n > 100 is false, the value to the right of the colon "low" is assigned to s. So what will print out if the user inputs a value of 55?


Download ppt "Conditions and Boolean Expressions"

Similar presentations


Ads by Google