< Browse > Home /

Advanced Control Flow – Switch Statements

August 22nd, 2008 | 3 Comments | Posted in C++ by Diego | - [Full Entry]

A switch statement allows you to control the flow of your program similar to if and else statements, but can be easier for the user to understand.

The Switch Statement

The structure of a switch statement is the following:

switch(expression) {
    case <constant_expression>  :
    statements
    break;

    case </constant_expression><constant_expression> :
    statements
    break;

    //More cases…

    default :
    statements
}</constant_expression>

Read more »

Basic Control Flow

June 24th, 2008 | No Comments | Posted in C++ by Diego | - [Full Entry]

In the following tutorial, you’ll learn how to use if, and if-else statements. They are useful for deciding on what lines of code to execute based on values of previous variables in your program.

The If Statement

An example is as follows

int x;
if ( condition ) x = 50; //If condition returns TRUE, assign 50 to X.
 

In other words, if the condition you provide returns a boolean value ( is a value that returns either TRUE or FALSE ) TRUE, then the following statements enclosed with {}’s are executed (no need if only one statement follows the if statement). Read more »