Key Concepts

Review core concepts you need to learn to master this subject

if Statement in C++

if (a == 10) { // code goes here }

In C++, an if statement is used to test an expression for truth.

  • If the condition evaluates to true, then the code within the if statement block is executed.

else Clause in C++

if (a == 10) { // code goes here }

In C++, an else clause can be added to an if statement.

  • If the condition evaluates to true, code in the if part is executed.
  • If the condition evaluates to false, code in the else part is executed.

switch Statement in C++

if (a == 10) { // code goes here }

In C++, a switch statement is an alternative to the if, else if, else statement.

switch statement contains an expression and then various cases. The value of the expression is compared with the value of each case. If there is a match, the code within starts to execute.

The break keyword tells the computer to exit the block and not check any other cases.

default is executed when no case matches. It’s essentially the else.

Relational Operators in C++

if (a == 10) { // code goes here }

In C++, relational operators are used to compare two values. The result of a comparison is either true or false.

The relational operators in C++ are as follows:

  • == equal to

  • != not equal to

  • > greater than

  • < less than

  • >= greater than or equal to

  • <= less than or equal to

else if Statement in C++

if (a == 10) { // code goes here }

In C++, one or more else if statements can be added in between the if and else.

  • If the condition evaluates to true, code in the if part is executed.
  • If the condition evaluates to false and the condition in else if is true, code in the else if part is executed.
  • Else, code in the else part is executed.

Logical Operators in C++

if (a == 10) { // code goes here }

In C++, logical operators can be used to combine two different conditions.

  • && the and logical operator
  • || the or logical operator
  • ! the not logical operator

The && logical operator requires both conditions to be true. The || logical operator requires either of the condition to be true. The ! logical operator negates the result.

Chevron Left Icon
Conditionals & Logic
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Every program we’ve seen so far has only had one possible path of execution — they all execute line by line, from top to bottom. And every time you run one of those programs, it gives you the same …
  2. 2
    Before we dive deep into the syntax of the if statement, let’s do a demo! Here, we have coinflip.cpp program that simulates a coin toss: - 50% of the time, it’s Heads. - 50% of the time, i…
  3. 3
    An if statement is used to test an expression for truth and execute some code based on it. Here’s a simple form of the if statement: if\ (condition)\ {statements} If the condition is true, t…
  4. 4
    When writing conditional statements, sometimes we need to use different types of operators to compare values. These operators are called relational operators. To have a condition, we need relati…
  5. 5
    We can also add an else clause to an if statement to provide code that will only be executed if the condition is false. Here’s a form of an if statement that includes an else clause: if\ (conditio…
  6. 6
    So what happens if you want more than two possible outcomes? This is where else if comes in! if\ (condition)\ {statement1} else\ if\ (condition)\ {statement2} else\ {statement3} The els…
  7. 7
    Now that we know how if, else if, else work, we can write programs that have multiple outcomes. Programs with multiple outcomes are so common that C++ provides a special statement for it… the swi…
  8. 8
    Congratulations!  Here are some of the major concepts: - An if statement checks a condition and will execute a task if that condition evaluates to true. - if / else statements make binary decisi…
  1. 1
    Often, when we are trying to create a control flow for our program, we’ll encounter situations where the logic cannot be satisfied with a single condition. Logical operators are used to combine …
  2. 2
    The and logical operator is denoted by &&. It returns true if the condition on the left and the condition on the right are both true. Otherwise, it returns false. Here’s the truth table: | a …
  3. 3
    The or logical operator is denoted by ||. It returns true when the condition on the left is true or the condition on the right is true. Only one of them needs to be true. Here’s the truth table…
  4. 4
    The not logical operator is denoted by !. It reverses the bool outcome of the expression that immediately follows. Here’s the truth table: | a | !a | | — | — | | false | true | | true | fals…
  5. 5
    Awesome! In this mini-lesson, we’ve added more operators to our toolbox: - &&: the and logical operator - ||: the or logical operator - !: the not logical operator

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo