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

Variables / Identifiers / Literals (Constants)

Variables These are the memory location which can store a value. As the name suggests, the value of a variable can vary during the whole program. In other words, variables are named storage locations whose value can be manipulated during program run. Example:  a = 5;  ✔ 5 = a;  ✖ a = a + 2; a = a + 10; Identifiers Name of variable, function, array, class etc created by the programmer are called as identifiers. Constants Data items that never change their value during a program run. integer constant character constant floating constant string-literal

Operators

  The operations are represented by operators and the object of the operation are referred to as operands. 5 types of operators: 1. Arithmetic Operator  Addition : 2 + 4 = 6 Subtraction : 2 - 4 = -2 Multiplication : 2 * 4 = 8 Division : 2 / 4 = 0 Modulus (remainder operator, used only with integer values) : 7 % 2 = 1 2. Increment (++) / Decrement (--) Operator Increment Operator increases its operand value by 1 while decrement operator decreases its operand value by 1.  They both have two types : Prefix : First it will increase (or decrease) the operand value by one and then participate in the operation. Postfix : First it will participate in the operation and then increase (or decrease) the operand value by 1. 3. Relational Operator They determine the relation among different operands. But they do not work with strings. 6 types of relational operator are: > greater than < less than >= greater than or equals to <= less than or equals to == equal to != not equ...