Chapter 3: Basic Control Flow

大綱

Comparison operators

The if statement

The ternary conditional operator

  • () ? :

Loops

Key points

  • You use the Boolean data type Bool to represent true and false.

  • The comparison operators, all of which return a Boolean, are:

    Equal: ==

    Not equal: !=

    Less than: <

    Greater than: >

    Less than or equal: <=

    Greater than or equal: >=

  • You can use Boolean logic to combine comparison conditions.

  • You use if statements to make simple decisions based on a condition.

  • You use else and else-if within an if statement to extend the decision-making beyond a single condition.

  • Short circuiting ensures that only the minimal required parts of a Boolean expression are evaluated.

  • You can use the ternary operator in place of simple if statements.

  • Variables and constants belong to a certain scope, beyond which you cannot use them. A scope inherits visible variables and constants from its parent.

  • while loops allow you to perform a certain task a number of times until a condition is met.

  • The break statement lets you break out of a loop.

Last updated