Conditional Statements

The if/else statement, comparisons and the && and || logical connectives.

A program becomes useful when it can make decisions: "if the number is even — print one thing, otherwise — another". That is what the if/else statement is for. Comparison operators: == (equal), != (not equal), < , > , <= , >=. Watch out for the classic mistake: comparison is written with two equals signs ==. A single = is assignment, and inside an if it almost always means a bug.
C++

Enter a number — the program will say whether it is even

Conditions can be combined: • && — logical AND: both conditions must hold; • || — logical OR: one is enough; • ! — negation. Multiple branches are handled with an if / else if / else chain. Below is a program that finds the largest of three numbers.
C++

Enter three numbers, for example: 3 9 5

Task: read a person's age and print "school" if it is between 7 and 17 inclusive, otherwise "other". You will need the && connective. Then solve the attached problem "Even or Odd" and mark the lesson as completed.

Practice problems

Доска