Branching Control Statement

In the previous section we have discussed about types of control statements. In this section I am going to discuss about   branching control statement.

Branching control statement are of two type :

1)conditional branching statement

2)Unconditional branching statement

Conditional Branching statement:

Conditional branching statement are of following type:

1)if…..else

2)if…..else if

3)switch

if….else

This control statement is used to take single way decision.

Syntax:

If the expression under if condition is satisfied i.e. it returns non zero value then the statement(s) written inside if block will be executed otherwise statements written inside else block will be executed.

example:

Program compiled in Dev C++ compiler.

================================================================================

OUTPUT:

================================================================================

In the above example number I entered is 12 which is even number, hence the  value of expression returns non zero value and expression inside if block is executed. In this type of program else part is optional. We can write the same program without else part. While using if….else conditional statements it must be kept in mind that how many statement to use inside blocks.If you want to use single statement in such conditions braces are optional.You can remove them which makes program faster as comparison to using braces in programs.So above code will produce same output if it is written as below:

if…..else if

This control statement is used to take multiway decision in single program.

Syntax:

In this the code will be executed line by line until the condition is satisfied. After satisfying the condition or expression rest part of code will not be executed. The last else part statement(s) will be executed if all the above expression becomes false.

 

Program compiled in Dev C++ compiler ================================================================================

OUTPUT:

================================================================================

switch() case:

switch case is also multiway control statement.It works when the expression value is matched with case constant value.

syntax:

Points must remember while using switch case:

  • The expression and constant of switch case must be integer.
  • Wherever the value of expression and case constant match the statement within that particular case constant will executed until getting break statement.
  • break statement must be used inside case constant, if you fails to do so after matching the expression value and case constant the all statements will execute until the end of switch block. So better to use break inside case constant.
  • constant of switch case can be written anywhere.
  • constant must not be duplicate.
  • default statement will be executed in case of wrong case constant input.

Example:

Program compiled in Dev C++ compiler ================================================================================

OUTPUT:

================================================================================

Unconditional Branching statement:

break and continue :

break and continue statements are used to terminate the execution of program from loops.

break:

break is used in looping (for,while,do..while)  along with switch case. while compiling the code whenever compiler encounters break statement it terminate the execution of particular loop and comes out of that block of loop.

Example:

In the above example when the value of i will be 5 the compiler will encounter the break statement. The compiler will come out of that loop and rest value will not be print.

It must be kept in mind that break statement can be used with if condition only when that particular if is present inside any loop otherwise it will lead to error.

Continue:

continue statement also used in looping but continue statement is not used in switch  case. when the compiler encounter continue statement inside loops, in that  situation after continue statement all the expression or statements written there will not compile. The compiler simply go for next iteration.

For example:

In the above program when the value of i will be 2 the continue statement will be encountered by the compiler and print statement will not print the value. It will go for next iteration.Hence value from 0 to 10 will be print except 2.

goto and label:

goto is also an unconditional statement which transfers the flow of control to any other part of program.break statement is only used to come out of innermost loop but in case you have to come out from inside of multiple loops in such case goto statement is used. This statement is generally not used. Programmers prefers to do the programs without using goto statement and its also better.

Syntax:

In the above syntax label can be any valid identifiers.

For example:

There are many site which provides information related to the Programming please click here for more details.

 

Leave a Reply