RAD Studio |  |

if, else

Category

Operators

C++ Syntax

Copy Code

    if ( <condition1> )  <statement1>

Copy Code

    if ( <condition1> )  <statement1>;
     else  <statement2>;

Copy Code

    if ( <condition1> )  <statement1>;
     else if ( <condition2> )  <statement2>;
     else  <statement3>;

Copy Code

    if ( <condition1> )
    {
      if ( <condition2> ) {
      <statement1>
      <statement2>
      }
      else <statement3>
    }
    else
      <statement4>

Description

Use if to implement a conditional statement.

You can declare variables in the condition expression. For example,

Copy Code

    if (int val = func(arg))

is valid syntax. The variable val is in scope for the if statement and extends to an else block when it exists.

The condition statement must convert to a bool type. Otherwise, the condition is ill-formed.

When <condition> evaluates to true, <statement1> executes.

If <condition> is false, <statement2> executes.

The else keyword is optional, but no statements can come between an if statement and an else.

The #if and #else preprocessor statements (directives) look similar to the if and else statements, but have very different effects. They control which source file lines are compiled and which are ignored.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!