Control Statements

In programming, decision making is used to specify the order in which statements are executed. This is carried out using following control statements.

if statement

Syntax:

 if (testExpression)
{
statement;
}

The if statement evaluates the test expression inside the parenthesis.

If the test expression is evaluated to true (nonzero), statements inside the body of if is executed.

If the test expression is evaluated to false (0), statements inside the body of if is skipped from execution.

If …else statement

The if…else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).

Syntax:

if (testExpression) {
    statement1;// codes inside the body of if}
else {
    statement2;// codes inside the body of else
}

If test expression is true, codes inside the body of if statement is executed and, codes inside the body of else statement is skipped.

If test expression is false, codes inside the body of else statement is executed and, codes inside the body of if statement is skipped.

if…else if …else statement

The if…else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. At that time if…else if…else statement is used.

Syntax:

if (testExpression1) 
{
   // statements to be executed if testExpression1 is true
}
else if(testExpression2) 
{
   // statements to be executed if testExpression1 is false and testExpression2 is true
}
else 
{
   // statements to be executed if all test expressions are false
}
Nested if…else statement

The nested if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Syntax:

if (testExpression1) 
{
   // statements to be executed if testExpression1 is true
}
else if(testExpression2) 
{
   // statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3) 
{
   // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true
}
.
.
else 
{
   // statements to be executed if all test expressions are false
}
Switch Case Statement

If you are checking on the value of a single variable in if…else…if, it is better to use switch statement. The switch statement is often faster than nested if…else (not always).

switch (n)
​{
    case constant1:
        // code to be executed if n is equal to constant1;
        break;
 
    case constant2:
        // code to be executed if n is equal to constant2;
        break;
        .
        .
        .
    default:
        // code to be executed if n doesn't match any constant
}

When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.

The break statement is used to prevent the code running into the next case.

Conditional Operator ?:

Syntax:

expression1 ? expression2 : expression3

Expression1 is evaluated first. If its value is true, then expression2 is evaluated and expression3 is ignored.

If expression1 is evaluated as false, then expression3 evaluates and expression2 is ignored. The result will be a value of either expression2 or expression3 depending upon which of them evaluates as True.

Conditional operator associates from right to left.

Rules of Conditional Operator

Expression1 must be a scalar expression; expression2 and expression3 must obey one of the following rules:

  • Both expressions have to be of arithmetic type.
  • expression2 and expression3 are subjected to usual arithmetic conversions, which
    determines the resulting type.

Both expressions have to be of void type. The resulting type is void.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: