C++ Beginners Guide – Conditional Statements

C++ Logo - C++ Beginners Guide - Conditional Statements
C++ Logo – C++ Beginners Guide

C++ Beginners Guide – Conditional statements. C++ uses all the expected conditional statements found in most programming languages. Most of the examples below apply to all programing language and not just C++. The alternative conditional statement however is not found in many languages. If anyone reading this spots an error please let me know. The purpose of these guides is both for revision and to help others.

  • If
  • If – Else
  • If – Else If – Else
  • Switch Statements
  • The alternative format in C++

If Statement – C++ Beginners Guide – Conditional Statements

If Statement diagram -  C++ Beginners Guide - Conditional
If Statement diagram
if(variable == 0) 
{
    cout<< "v is 0";
}

If – Else Statement

If Else statement diagram
If Else statement diagram
if(variable == 0) 
{
    cout<< "variable is 0";
}
else
{
    cout << "variable is not 0";
}

If – Else If – Else Statement

If - Else If - Else Statement diagram
If – Else If – Else Statement diagram
if (variable < 0 )
{
    cout<< "variable is negative";
}
else if (variable > 0)
{
    cout<< "variable is positive";
}
else
{
    cout << "variable is 0";
}

Switch Statement

Switch statement Diagram
Switch statement Diagram
switch (character) 
    {
        case 'a': case 'A':
        cout << character << " is an upper or lower case letter a" << endl;
            break;
        case 'e':
            cout << character << " is a lower case letter e" << endl;
            break;
        case 'E':
            cout << character << " is an upper case letter E" << endl;
            break;
        case 'i': case 'I':
            cout << character << " is an upper or lower case i" << endl;
            break;
        case 'o': case 'O':
            cout << character << " is an upper or lower case o" << endl;
            break;
        case 'u': case 'U':
            cout << character << " is an upper or lower case u" << endl;
            break;
        default:
            cout << character << "is a consonant" << endl;
    }

Other Languages – C++ Beginners Guide – Conditional Statements

The Diagrams above apply to all languages which support these types of conditional statements. Anyone with C# will find that the above is almost if not identical to the C# syntax for the same kind of conditional statements.

Alternative conditional statements

If statements in C++ can be written in a compressed format using a question mark.

Expression ? Return action 1 : Return action 2

If the condition set in the expression is met then return action one, if it is not met then return action 2.

Eg.

int x;
float y;
int value = (x < 10 ? 10 : x);
float total = (y > 0.0 ? sqrt(y) : 0.0);

In the example above, the variable called value will be set to 10 if the integer x is less than 10, but it will be set to the same value as x if x is greater than or equal to 10;

Similarly the total variable will be set to 0 if the value of y is less than or equal to 0, otherwise it will be set to the square root of y.

Related Articles – C++ Beginners Guide – Conditional Statements

More to come soon

Related Post

3 thoughts on “C++ Beginners Guide – Conditional Statements

Leave a Reply

Your email address will not be published. Required fields are marked *