In C Language doesn't follow the sequence follow means top to bottom. C Program flow can be change by the programmer by using Control Structures. In C Language we have three types of Control Structures
- Decision Making Statements
- Looping Control Structures / Iterative Control Statements
- Unconditional Control Structures / Jumping Statements
Decision Making Statements
- If Statements
- simple if
- if else
- if else if (or) if ladder
- Nested if
- Switch Statements
Simple if: it is used to check the conditions. if the condition is true it executes the true set of statements otherwise if the condition is failed and rest of the program will be continuous.
Syntax: if (cond){
statements;
}
If Else: it is used to check the condition. if the condition is true it executes the true set of statements otherwise it executes the false set if statements and rest of the program will be continuous.
Syntax: if (cond)
{
statements;
}
else
{
statements;
}
If Else if or If Ladder: it is used to check multiple conditions.
Syntax: if (cond)
{
statements;
}
else if( cond )
{
statements;
}
else if( cond )
{
statements;
}
else if( cond )
{
statements;
}
else if( cond )
{
statements;
}
||
||
else
{
statements;
}
Note: if any one condition is true that block will be executed and rest of the statements will not executed. If all conditions are false than else block will be executed.
Nested if: In one if we are writing another if is called as Nested if.
Switch : It is a multiway decision statements used to perform = =( equal to operator) operations where as in if ladder we can perform all operators.
Syntax: switch(variablename/exression)
{
case val1: statements; break;
case val2: statements; break;
case val3: statements; break;
case val4: statements; break;
case val5: statements; break;
||
||
default: statement;
}
Note: if we forget to write the break keyword for the case, next case statement will be executed if the condition is false.
No comments:
Post a Comment