C# Jump Statements

Introduction

 
In this blog post, we are going to focus on the jump statements of the C# language. You’ve probably been working with it for a while, but needed more clarity, especially with the notorious goto statement. Thus, that’s the main goal of this post. However; If you are a total beginner, I encourage you to continue reading until the end without skipping. 
 
C# provides three (3) different types of jump statements that enable you to move immediately to another line in the program. These jump statements are break, continue and goto. Thus, these jump statements are very handy in one of these scenarios: in case that you need to terminate a loop before it has reached the end or might need to skip an iteration of a loop.
 
Break statement
 
Let me guess, you’ve met the break statement for the first time when you used it to exit inside a switch statement.
 
See the example below:
  1. [Flags]  
  2. public enum Status : byte  
  3. {  
  4.     NotStarted = 0,  
  5.     InProgress = 1,  
  6.     Done = 2  
  7. }  
  8.   
  9. Status myStatus = Status.Done;  
  10.   
  11. switch (myStatus)  
  12. {  
  13.     case Status.NotStarted:  
  14.         //do something  
  15.         break;  
  16.     case Status.InProgress:  
  17.         //do something  
  18.         break;  
  19.     case Status.Done:  
  20.         //do something  
  21.         break;  
  22.     default:  
  23.         break;  
  24. }  
As you learn more about the C# language, you’ve possibly faced with the different looping statements such as for, for-each, while, and do-while loops. In fact, break can be also used to exit from these looping statements.
 
See the example below:
  1. [Fact]  
  2. public void Jump_Statement_Break_Test()  
  3. {  
  4.     int counter = 0;  
  5.     int length = 10;  
  6.     int breakWhenEqualsToFive = 5;  
  7.     string message = string.Empty;  
  8.   
  9.     for (int i = 0; i < length; i++)  
  10.     {  
  11.         counter = i;  
  12.         message += $"Inside the loop counter = {counter} to {length}.{Environment.NewLine}";  
  13.         if (i == breakWhenEqualsToFive)  
  14.         {  
  15.             message += $"Breaking out counter at counter = {counter}";  
  16.             break;  
  17.         }  
  18.     }  
  19.   
  20.     Assert.Equal(breakWhenEqualsToFive, counter);  
  21.     this._output.WriteLine(message);  
  22. }  
The example above shows that when the counter variable i equals the variable breakWhenEqualsToFive, (i == breakWhenEqualsToFive) will eventually get out of the loop.
 
See the sample output below:
 
 
Continue statement
 
You might be asking: “So, is the continue statement similar to the break statement?”. You are correct, but there is a slight difference. The continue statement causes the program control to jump back to skip and start the loop rather than restarting outside the loop altogether.
 
See the example below:
  1. [Fact]  
  2. public void Jump_Statement_Continue_Test()  
  3. {  
  4.     List<int> evenNumbers = new List<int> { };  
  5.     Func<intbool> isEven = x => x % 2 == 0;  
  6.     string message = string.Empty;  
  7.   
  8.     for (int i = 0; i < 10; i++)  
  9.     {  
  10.         if (isEven(i))  
  11.         {  
  12.             evenNumbers.Add(i);  
  13.             message += $"{i} is Even. {Environment.NewLine}";  
  14.         }  
  15.         else  
  16.         {  
  17.             continue;  
  18.         }  
  19.     }  
  20.   
  21.     Assert.True(evenNumbers.SequenceEqual(new List<int> { 0, 2, 4, 6, 8 }));  
  22.     this._output.WriteLine(message);  
  23. }  
The example above shows that everytime a number isn't an even number, it will automatically jump back to skip and start the new iteration of the loop. 
 
See the sample output below:
 
 
 
Difference between the two statements
 
The break statements allow you to terminate or exit a loop before reaching the end, while the continue statement allows us to skip the iteration of the loop.
 
Goto statement
 
The goto statement enables you to jump directly to another specified line in the program, indicated by a label. This is an identifier followed by a colon.
 
See the example below:
  1. [Fact]  
  2. public void Jump_Statement_Goto_Inside_Switch_Statement()  
  3. {  
  4.     var random = new System.Random();  
  5.   
  6.     var values = Enum.GetValues(typeof(Status));  
  7.   
  8.     var myStatus = (Status)values.GetValue(random.Next(values.Length));  
  9.   
  10.     switch (myStatus)  
  11.     {  
  12.         case Status.NotStarted:  
  13.             goto LabelNotStarted;  
  14.         case Status.InProgress:  
  15.             goto LabelInProgress;  
  16.         case Status.Done:  
  17.             goto LabelDone;  
  18.         default:  
  19.             break;  
  20.     }  
  21.   
  22.     LabelNotStarted:  
  23.         Assert.True(myStatus == Status.NotStarted);  
  24.         this._output.WriteLine("jumped into LabelNotStarted & status not yet started");  
  25.         return;  
  26.     LabelInProgress:  
  27.         Assert.True(myStatus == Status.InProgress);  
  28.         this._output.WriteLine("jumped into LablInProgress & status in progress");  
  29.         return;  
  30.     LabelDone:  
  31.         Assert.True(myStatus == Status.Done);  
  32.         this._output.WriteLine("jumped into LabelDone & status done");  
  33.         return;  
  34.   
  35. }  
See the sample output below:
 
 
 
Goto statement warning
 
Knowing the goto statement is a good tool under your belt. However, developers have a bad impression of it. Therefore, we need to be cautious about it. See the reasons why we need to be cautious about it below: 
  • It doesn't have a good reputation in most developers because it leads to many unwanted bugs and inelegant programs. 
  • Well known cause of 'spaghetti code' 
  • When you have implemented this into your code, expect massive criticism from other developers. 

Summary

 
In this blog post, we have discussed the following,
  • Break statement
  • Continue statement
  • The difference between break and continue
  • Goto statement
  • Things you need to be cautious about the goto statement
Back in the day, I still remember when I was a total noob and asked myself: “Why do you need to learn these statements?”. Hopefully, this blog was able to answer that.
 
I hope you have enjoyed this article, as I have enjoyed it while writing. You can also find the sample code here at GitHub. Until next time, happy programming!