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

C++ User Input Strings

User Input Strings

It is possible to use the extraction operator >> on cin to store a string entered by a user:

Example

string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;

// Type your first name: John
// Your name is: John

However, cin considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only store a single word (even if you type many words): Continue reading C++ User Input Strings

C++ Special Characters

Strings – Special Characters

Because strings must be written within quotes, C++ will misunderstand this string, and generate an error:

string txt = “We are the so-called “Vikings” from the north.”;

The solution to avoid this problem, is to use the backslash escape character. Continue reading C++ Special Characters

C++ Access Strings

Access Strings

You can access the characters in a string by referring to its index number inside square brackets [].

This example prints the first character in myString : Continue reading C++ Access Strings