C++ Short Hand If Else

Short Hand If…Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.

It can be used to replace multiple lines of code with a single line, and is often used to replace simple if else statements: Continue reading C++ Short Hand If Else

C++ Else If

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

Continue reading C++ Else If

C++ Else

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

Example

int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

Continue reading C++ Else

C++ If … Else

C++ Conditions and If Statements

You already know that C++ supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Continue reading C++ If … Else

C++ Boolean Examples

Real Life Example

Let’s think of a “real life example” where we need to find out if a person is old enough to vote.

In the example below, we use the >= comparison operator to find out if the age (25) is greater than OR equal to the voting age limit, which is set to 18: Continue reading C++ Boolean Examples

C++ Boolean Expressions

Boolean Expression

A Boolean expression returns a boolean value, which is either 1 (true) or 0 (false).

This is useful for building logic and finding answers.

You can use a comparison operator, such as the greater than (>) operator, to find out if an expression (or variable) is true or false : Continue reading C++ Boolean Expressions

C++ Booleans

C++ Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, C++ has a bool data type, which can take the values true (1) or false (0). Continue reading C++ Booleans

C++ C-Style Strings

C-Style Strings

C-style strings are created with the char type instead of string.

The name comes from the C language, which, unlike many other programming languages, does not have a string type for easily creating string variables. Instead, you must use the char type and create an array of characters to make a “string” in C. Continue reading C++ C-Style Strings

C++ String Namespace

Omitting Namespace

You might see some C++ programs that run without the standard namespace library. The using namespace std line can be omitted and replaced with the std keyword, followed by the :: operator for string (and cout) objects:

Example

#include <iostream>
#include <string>
// using namespace std; - Remove this line

int main() {
  std::string greeting = "Hello";
  std::cout << greeting;
  return 0;
}

Continue reading C++ String Namespace