{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# day08 - Boolean 1 True or false bool A type to represent...

This preview shows pages 1–9. Sign up to view the full content.

1 Boolean

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 True or false? bool : A type to represent Boolean values Must include stdbool.h Like any other type, you can create variables, parameters, and returns of type bool . Examples: bool minor = (age < 21); bool iLoveCS = true; if ( minor ) { printf("You can't purchase alcohol!\n"); }
3 Boolean functions Functions can return values of type bool . bool isOld(int age) { if (age > 10) { return true; } else { return false; } } Example usage: printf("Enter your age: "); int age; scanf("%d", &age); if ( isOld(age) ) { printf("Wow! You are old!"); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Boolean functions Functions can accept parameters of type bool . void greet(int age, bool isUgly ) { printf("Wow! You are old AND ugly!\n"); } else { printf("Hello! How are you?\n"); } } Example usage: greet(24, true); Output : Wow! You are old AND ugly!
5 Boolean "Zen" Functions that return a Boolean result often have an if/else statement: bool isOld(int age) { if (age > 10) { return true; } else { return false; } } ... but the if/else is sometimes unnecessary. The <test> is a Boolean expression; its true / false value is exactly the value you want to return… so why not just return it directly! bool isOld(int age) { return (age > 10); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 while  loops
7 Definite loops definite loop : A loop that executes a known number of times. The for loops we have seen so far are definite loops. We often use language like "Repeat these statements N times." "For each of these 10 things, …" Examples: Print "hello" 10 times. Find all the prime numbers up to an integer n . Print each odd number between 5 and 127.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Indefinite loops indefinite loop : A loop where it is not obvious in advance how many times it will execute. We often use language like "Keep looping as long as or while this condition is still true." "Don't stop repeating until the following happens." Examples: Print random numbers until a prime number is printed. Continue looping while the user has not typed "n" to quit.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 32

day08 - Boolean 1 True or false bool A type to represent...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online