Jump Statements Simplified With Flow Chart

Introduction

 
Hey folks, I trust you and your family are in good health. Our objective today is to understand the behavior of jump statements in C#.
 
Jump statements are used to transfer the control of execution from one point to another point. It's like jumping it from one location to another in real life. Basically, there are 5 types of jump statements in C#.
  • Break
  • Continue
  • Return
  • Throw
  • Goto
Let's take a deep dive to understand each of them.
 

Break statement

 
It is used to break the loop or current executing statements: such as switch case
  • In loop, after occurance of a break statement, execution of the loop will be terminated, hence statements below the break won't execute for current index and control will jump out of the loop.
  • In switch, It is used to come out of the switch control, after executing current case condition of switch, you will encounter an occurance of break statement which will ensure that, the control comes out of the switch statement, excluding the rest of the case conditions in the switch.
Flowchart for break statement
 
 
 
Let's see it working in action:
  • Loop will iterate from 1 to 5
  • Printing value of current iteration
  • Condition will match when value of iteration will be set to 3

    • Occurance of break statement

      • break the loop, and control  will come out of the loop
  1. private static void  Break()  
  2.       {  
  3.           Console.WriteLine("--------------------- Break statement demonstration ---------------------");  
  4.           for (int i = 1; i <= 5; i++)  
  5.           {  
  6.               if(i == 3)  
  7.               {  
  8.                   Console.WriteLine("Now condition has matched, loop will break now!");  
  9.                   break;  
  10.               }  
  11.               Console.WriteLine("Current Value of i = "+ i);  
  12.           }  
  13.       }   
Output
 
 

Continue Statement

  • It is used to skip over the current iteration's execution part.
  • After the occurance of continue keyword, it skips the following statements and continues with the execution of the next iteration of the loop. It transfers the control to the next iteration of the loop.   
Flowchart for continue statement,
Let's code,
  • We are going to iterate through loop from 1 to 5
  • Printing value of current iteration
  • Condition check: value of iteration == 3
    • Occurance of continue statement
      • Skipped the printing of iteration value.
    • Moving on with iteratin  = 4
    • printing value of current iteration
  1. private static void Continue()  
  2.        {  
  3.            Console.WriteLine("For loop from 1 - 5");  
  4.            Console.WriteLine("--------------------- Continue statement demonstration ---------------------");  
  5.            for (int i = 1; i <= 5; i++)  
  6.            {  
  7.                if (i == 3)  
  8.                {  
  9.                    Console.WriteLine("Now condition has matched, " +  
  10.                                        "So value of 3 will not be printed as continue is going to skip next printing statements");  
  11.                    continue;  
  12.                }  
  13.                Console.WriteLine("Current Value of i = " + i);  
  14.            }  
  15.        }   
Let's check out the output,
 
 

Return statement

  • We are familiar with methods with return type. Methods use return statement to return their values.
  • After the occurance of return statement, it terminates method's execution and passes control to the calling method. 
Flowchart of a return statement,
 
 
Let's check out the source-code,
  • Loop iterates from 1 to 5
  • Condition matches when value of iteration is set to 3
    • return string "i equal 3"
    • if condition does not match return string "i is not equal 3"
  • Outcome: when value if iteration is 1 we will come out of method as "else condition will be matched".
  1. private static string Return()  
  2.        {  
  3.            Console.WriteLine("For loop from 1 - 5");  
  4.            Console.WriteLine("--------------------- Return statement demonstration ---------------------");  
  5.            for (int i = 1; i <= 5; i++)  
  6.            {  
  7.                if (i == 3)  
  8.                {  
  9.                    return "Now condition has matched, returning i equal 3";  
  10.                }  
  11.                else  
  12.                {  
  13.                    return "Now else condition has matched, returning i is not equal 3";  
  14.                }  
  15.            }  
  16.            return "For loop has ended, Out of the for loop";  
  17.        }   
Let's check out the output,
 
 

Throw statement

 
Used in exception handling, if you want to learn exception handling in detail you must visit here.
  • It is used to throw an exception.
  • Programmer can throw an exception manually with new keyword.
    1. throw new ArithmeticException();   
Flowchart of throw statement,
 
 
Let's see the code,
  • We are throwing an exception when we are trying to divide a number by 0.
  • Even if we don't throw an exception, compiler will internally do that.
  • This is mostly used with user-defined exception. If you wish to learn how to use user-defined exception, please visit my article on exception handling.
  1. private static void Throw()  
  2.        {  
  3.            Console.WriteLine("For loop from 1 - 5");  
  4.            Console.WriteLine("--------------------- Throw statement demonstration ---------------------");  
  5.            for (int i = 1; i <= 5; i++)  
  6.            {  
  7.                try  
  8.                {  
  9.                    if (i == 3)  
  10.                    {  
  11.                        i /= 0;  
  12.                        throw new ArithmeticException();  
  13.                    }  
  14.                }  
  15.                catch (ArithmeticException ex)  
  16.                {  
  17.                    Console.WriteLine(ex.Message);  
  18.                    Console.WriteLine("Can not divide by 0");  
  19.                }  
  20.            }  
  21.        }   
Output for throw,
 
 
Last but not least -  go-to statement.
  • It is used to transfer control from one point to other (like every other jump statement). But this one has an advantage over others.
  • Control is transferred to the specific label statement in the program.
  • The label is the identifier for the goto statement. Text is followed by a colon (:) to tell  the compiler that it is a label.

    Syntax,
    1. //lable   
    2. ImHere:   
Flowchart for go to statement,
 
Code for goto statement,
  • We have a switch statement, where case value is integer

    Note
    Switch only has case 1 to 5: any other number will move control to default case.
  • Say we pass 4 to the method
    • Control will be passed to case 4:
    • It will execute those 2 statements
    • Occurance of goto statement: goto case 1:
      • Control will pass to case 1:
      • It will execute case 1 statements and break the loop
  • Why we should avoid usage of goto statements,
  • Say we pass 8,
    • Control will pass to default case: which has goto case ImHere:
    • Now the control will come at the begining of the method
    • Again it will go to default
    • Again at the begining of the method
    • This will result in infinite execution.
  1. private static void GoTo(int caseValue)    
  2.         {    
  3.                 //lable     
  4.                 ImHere: 
  5.             Console.WriteLine("For switch statements from 1 - 5");    
  6.             Console.WriteLine("--------------------- GoTo statement demonstration ---------------------");    
  7.                 switch (caseValue)    
  8.                 {    
  9.                 case 1:    
  10.                     Console.WriteLine("Value is 1");    
  11.                     break;    
  12.                 case 2:    
  13.                     Console.WriteLine("Value is 2");    
  14.                     break;    
  15.                 case 3:    
  16.                     Console.WriteLine("Value is 3");    
  17.                     break;    
  18.                 case 4:    
  19.                     Console.WriteLine("Value is 4");    
  20.                     Console.WriteLine("Going to case 1");    
  21.                     goto case 1;    
  22.                     //break is now not reachable.    
  23.                     break;    
  24.                 case 5:    
  25.                     Console.WriteLine("Value is 5");    
  26.                     break;    
  27.                 default:    
  28.                     goto ImHere;    
  29.                     break;    
  30.                 }    
  31.         }    
Output after passing number 4 to the method,
Wonderful!
 
Let's call all the above mentioned methods together with the main method.
  1. static void Main(string[] args)  
  2.        {  
  3.            Break();  
  4.                Console.WriteLine(Environment.NewLine);  
  5.            Continue();  
  6.                Console.WriteLine(Environment.NewLine);  
  7.            Console.WriteLine(Return());  
  8.                Console.WriteLine(Environment.NewLine);  
  9.            Throw();  
  10.                Console.WriteLine(Environment.NewLine);  
  11.            GoTo(4);  
  12.        }   
Output of all,
 
I hope you have enjoyed and gained some insights on jump statements in this article. I really appreciate the time you have taken out of your busy schedule to read this article. Also stay tuned for the next article.
 

Conclusion

 
We learned some basic and advanced stuff today. Also what things to take care of while using goto statements and we have seen what we must avoid while dealing with jump statements.
 
Now we know, what types of jump statements there are in C#, which can help  us based on the project's requirements.
 
We also learned how switch statement work with break & goto statements.
 
I wish you all the very best.
 
Keep learning & keep coding.
 
Connect with me @