Skip to main content

Selection statement

 

If statement 

If tests a particular condition, if the condition evaluates to true then set of statements under the body of 'if' statement will be executed otherwise it will be ignored by the compiler.

Syntax : if(<condition>)
{
     statements;
}

<> : programmer defined

If else statement

If we use 'else' statement with 'if' statement then if the condition of 'if' statement evaluates to false, in that case body of else statement will be executed, otherwise it will be ignored.

Syntax : 
if(<condition>)
{
     statements;
}
else
{
    statements;
}

If-else-if ladder

If we use 'else if' statement with 'if' statement then if any condition evaluates to true, in that case its associated statement will be executed and all other 'else if' and 'else' statement will be ignored by the compiler.

Syntax : 
if(<condition>)
{
     statements;
}
else if(<condition>)
{
   statements;
}
else if(<condition>)
{
   statements;
}
.
.
.
else
{
    statements;
}

Switch statement 

C++ provides a multiple branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constant. If a match is found then its associated statement will be executed.

According to ANSI (American National Standard Institute) a switch statement can have upto 257 case statements.

switch(<expression>)
{
     case<constant_value1> : statements;
                                               break;
     case<constant_value2> : statements;
                                               break;
     .
     .
     .
     [default : statements; ]
}


[] : optional

Note: In 'if-else' statement 'else' always comes at last but in switch statement 'default' has no specified position and default statement is optional.



Comments

Popular posts from this blog

Data Representation

 Digital Number System In digital representation, various number systems are used. The most common ones are : Binary : base 2 (0, 1) Octal : base 8 (0, 1, 2, 3, 4, 5, 6, 7) Decimal : base 10 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) Hexadecimal : base 16 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F) Number Conversions : From Decimal to any system (base) (Decimal) 10 = (Any) base

Data Type Modifiers

 As the name suggests, data type modifiers are used with fundamental data types to modify the length of data that a particular data type can hold. Following 4 datatype modifiers are available in C++: Signed Unsigned Short Long