Core C++

Branch Statements

if


Video Tutorials

Lesson 7: If, Else If, Else

Example 1

if (bCondition) {
    // This is executed if the value of the 'bCondition' is true.
    // Otherwise, it is skipped.
}

Example 2

if (dX > 0.0 || dY < 2.0) {
    // This is executed if the value of 'dX' is greater than 0
    // or if 'dY' is less than 2.0. However, if 'dX' is greater
    // than zero, the second condition is not checked.
}

if, else


Video Tutorials

Lesson 7: If, Else If, Else

Example

if (bCondition) {
    // This is executed only if the value of 'bCondition' is true.
} else {
    // This is executed only if the value of 'bCondition' is false.
}

if, else if


Video Tutorials

Lesson 7: If, Else If, Else

Example

if (bCondition1) {
    // This is executed only if the value of 'bCondition1' is true.
} else if (bCondition2){
    // This is executed only if the value of 'bCondition2' is true
    // and the value of 'bCondition1' is false.
}

if, else if, else


Video Tutorials

Lesson 7: If, Else If, Else

Example 1

if (bCondition1) {
    // This is executed only if the value of 'bCondition1' is true.
} else if (bCondition2){
    // This is executed only if the value of 'bCondition2' is true and
    // the value of 'bCondition1' is false.
} else {
    // This is executed only if the value of 'bCondition2' is false and
    // the value of 'bCondition1' is false.
}

Example 2

if (bCondition1) {
    // This is executed only if the value of 'bCondition1' is true.
} else if (bCondition2){
    // This is executed only if the value of 'bCondition2' is true and
    // the value of 'bCondition1' is false.
} else if (bCondition3){
    // This is executed only if the value of 'bCondition3' is true and
    // the value of 'bCondition1' and 'bCondition2' are false.
} else {
    // This is executed only if 'bCondition1', 'bCondition2', and
    // 'bCondition3' are false.
}

switch


Video Tutorials

Lesson 27: Switch Statements

Example 1

// switch with two branches
switch (iX) {
    case 0:
    {
        // Executed only if 'iX' is 0
        break;
    }
    case 1:
    {
        // Executed only if 'iX' is 1
        break;
    }
}

Example 2

// default branch added
switch (iX) {
    case 0:
    {
        // Executed only if 'iX' is 0
        break;
    }
    case 1:
    {
        // Executed only if 'iX' is 1
        break;
    }
    default:
    {
        // Executed if 'iX' is not 0 or 1
        break;
    }
}

Example 3

// Nested cases
switch (iX) {
    case 0:
    {
        // Executed only if 'iX' is 0
        case 1:
        {
            // Executed only if 'iX' is 0 or 1
            break;
        }
    }
    default:
    {
        // Executed if 'iX' is not 0 or 1
        break;
    }
}

Example 4

// Multi-value branch
switch (iX) {
    case 0:
    case 1:
    case 2:
    {
        // Executed if 'iX' is 0, 1, or 2
        break;
    }
    default:
    {
        // Executed if 'iX' is not 0, 1, or 2
        break;
    }
}

Example 5

// Using a switch with char with multiple multi-valued branches
switch (cChar) {
    case 'a':
    case 'b':
    {
        // Executed if 'cChar' is 'a' or 'b'
        break;
    }
    case 'd':
    case 'D':
    {
        // Executed if 'cChar' is 'd' or 'D'
        break;
    }
    default:
    {
        // Executed if 'cChar' is not 'a', 'b', 'd', or 'D'
        break;
    }
}

Example 6

// switch using an enum
enum EDirection {keUp, keDown, keLeft, keRight}
switch (eMyDirection) {
    case keUp:
    {
        // Executed only if 'eMyDirection' is 'keUp'
        break;
    }
    case keDown:
    case keLeft:
    {
        // Executed only if 'eMyDirection' is 'keDown' or 'keLeft'
        break;
    }
    default:
    {
        // Executed if 'eMyDirection' is not keUp', 'keDown',
        // or 'keLeft'
        break;
    }
}
 

© 2007–2024 XoaX.net LLC. All rights reserved.