Table of Contents

Part 1 — Conditions

1.1 What Is a Condition?

A condition is a yes/no question that your program asks about its data.

In C++ the answer to such a question is a value of type bool: either true or false.

Syntax. A condition is any expression that produces true/false:

n % 2 == 0      // is n even?
x >= 1          // is x at least 1?

Think first. Before writing a condition, say the question out loud : "Is the number divisible by 3?" Then translate it to a boolean expression.

1.2 Why Do We Need Conditions?

Without conditions, a program does the same thing for every input. Almost every problem needs the program to decide:

Simple example