[rps-include post=6557]
Conditions or Decisions making is one of the most important aspect of programming languages. Actual programming logic is implemented in the decisions. According to decisions the application will be branched and expected results or operations will be accomplished. There are 3 main decision making mechanisms. We will look in details below.
If
The syntax of if is like below in C programming language.
if(boolean_expression) { statements_to_execute }
with if(boolean_expression) if boolean_expression is true code block of if which is specified as statements_to_execute is executed. If boolean_expression is not true statement_to_execute code block is skipped without executing. Let’s make an example to make things more clear and understandable.
If Else
If else is similar to the if . Else is executed if if is not executed because of boolean_expression .
if(boolean_expression) { statements_to_execute } else { else_statement_to_Execute }
If Else if
Up to now we have only evaluated two condition with if and else . But in real world there will be more than two conditions to check. In this situation if ... else if ...
can be used.
if(boolean_expression) { statements_to_execute } else if(boolean_expression) { statement_to_execute } .... else { statement_to_Execute }
Switch Case
Switch case is alternative to if
statement . Their functionality is very similar. Expressions
is evaluated for each case
statement. Matched case statements will be processed. For a single expressions more than one case conditions can be matched.
switch(expression){ case case_expression: statement_to_execute case case_expression: statement_to_execute .... }
[rps-include post=6557]