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

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

View Full Document Right Arrow Icon
    1 Boolean
Background image of page 1

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

View Full DocumentRight Arrow Icon
    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"); }
Background image of page 2
    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!"); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
    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!
Background image of page 4
    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); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
    6 while  loops
Background image of page 6
    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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
  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.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online