Chapter 3 – Conditionals – logical operators and truth tables

Chapter 3: Program Statements Notes

Control Flow:

  1. A conditional statement is sometimes called a selection statement.
  2. Each decision is based on a boolean expression.
  3. Three types of loop statements: while, do and for statements.

The if statement:

ifStatementFlowChart

Shorthand “if” statement: a ternary operator, inline if (iif) or ternary if

y = ( x >= 0 ) ? x : -x

which is a convenient  way of writing

if ( x >= 0 )
   y = x;
else
   y = -x

…..

  1. Equality operators
  2. Relational operators

equalityRelationOps

The if-else statement:

ifElseStatemtFC

Nested if statements:

nestedifs

Boolean expressions:

Logical operators:

logicalOps

Truth Table:

truthTable

Logical AND and OR operators:

andOrTruthTable

 

Example of truth table for a specific condition:

truthTableExample