Statements break, continue, goto. Examples of use
Contents
- 1. Operator break. Assignment
- 2. Operator continue. Assignment
- 3. Operator goto. Assignment
- 4. Examples of programs with a break statement
- 5. Examples of programs using the continue statement
- 6. Samples of programs that use the goto statement
- Related topics
1. Operator break. Assignment
In C#, the break statement has two main uses:
- in loop statements, the break statement is used to end the cyclic process (interrupting the loop). Such an action is necessary when the code needs to interrupt the execution of the cycle depending on some condition;
- in the switch statement, the use of the break statement is necessary to implement the exit from this statement.
⇑
2. Operator continue. Assignment
The continue statement is applied inside the loop body. The operator stops the current iteration and proceeds to the next iteration (the next step of the loop). Using the continue statement in a loop is effective if you need to skip some iterations depending on the condition.
⇑
3. Operator goto. Assignment
The goto operator is designed to perform an unconditional jump in a program. The general form of using the goto operator is as follows
goto label;
where label – label in the program to implement the transition.
In C# programming, the goto operator can be used in two ways:
- in the body of the program (some function) to organize the transition between operators;
- in the switch statement to go to one of the case branches.
If the program needs to go to the label with the name m1, then the code fragment may be as follows
m1: // operators // ... if (condition) goto m1;
here the condition is a conditional expression according to the C# syntax.
Using the goto operator in a program is considered bad form in programming. This is due to the fact that the very frequent use of the goto operator in a program confuses the program code and complicates its perception. Therefore, whenever possible, it is recommended that you replace goto with loop statements. However, this is only a recommendation.
⇑
4. Examples of programs with a break statement
Example 1. Given a sequence of 10 numbers. Determine if this sequence is ordered in ascending order. In the case of a negative answer, determine the sequence number of the first number that violates this sequence.
One of the possible solutions of the problem for an application such as Console Application is as follows
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class Program { static void Main(string[] args) { // Determining if there is an increasing sequence int num, pnum, pos = 0; int i; Console.WriteLine("Enter numbers:"); // numbers input cycle (maximum 10 numbers) i = 0; num = Convert.ToInt32(Console.ReadLine()); pnum = num; while (i < 9) { // if not the first time the loop body is executed if (pnum > num) { // the sequence is not increasing pos = i; // remember position break; // exit from the cycle } pnum = num; i++; num = Convert.ToInt32(Console.ReadLine()); } if (i == 9) Console.WriteLine("The sequence is ascending."); else { Console.WriteLine("The sequence is not ascending."); Console.WriteLine("break position = {0}", pos + 1); } Console.ReadKey(); } } }
As you can see from the above code, if the sequence is not increasing, then the while loop exits using the break statement.
The result of the program
Enter numbers: 5 6 4 The sequence is not ascending. break position = 3
Example 2. Given a sequence of integers, which ends with the number 15. Implement the calculation of the sum of the elements of a sequence. The number 15 is not included in the sum.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class Program { static void Main(string[] args) { // Calculation of the sum of the elements of a sequence int num; int sum; Console.WriteLine("Enter numbers:"); // the loop of numbers input, do-while operator used sum = 0; // the required sum do { num = Convert.ToInt32(Console.ReadLine()); if (num == 15) break; // выход из цикла, если введено число 15 sum = sum + num; } while (true); // endless cycle Console.WriteLine("sum = {0}", sum); Console.ReadKey(); } } }
Program result
Enter numbers: 5 5 6 8 2 15 sum = 26
⇑
5. Examples of programs using the continue statement
Example 1. Given a sequence of integers, which ends with the number 0. Calculate the sum of sequence items whose position numbers are multiples of 3 (elements at positions 3, 6, 9 …). Assume that the numbering of positions of elements begins with 1.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class Program { static void Main(string[] args) { // Calculation of the sum of the items of a sequence, // which are placed at positions multiple of 3 int num; int sum; int i; Console.WriteLine("Enter numbers:"); // the loop of numbers input, do-while operator used sum = 0; // the required sum i = 0; do { num = Convert.ToInt32(Console.ReadLine()); i++; // if position is 3, then skip summing if (i % 3 != 0) continue; sum = sum + num; } while (num != 0); Console.WriteLine("sum = {0}", sum); Console.ReadKey(); } } }
Program result
Enter numbers: 1 1 100 2 2 215 3 3 150 5 6 0 sum = 465
Example 2. Given a sequence of real numbers, which ends with the number -1. Calculate the sum of the items of a sequence whose values are in the range [1.7 … 2.7].
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class Program { static void Main(string[] args) { // Calculation of the sum of the items of a sequence, // the values of which are within [1.7 .. 2.7] const double MIN = 1.7; const double MAX = 2.7; double sum = 0; double number = 0; Console.WriteLine("Enter numbers:"); while (number != -1) { number = Double.Parse(Console.ReadLine()); if ((number < MIN) || (number > MAX)) continue; sum += number; } Console.WriteLine("sum = {0}", sum); Console.ReadKey(); } } }
The result of the program
Enter numbers: 1.1 2 3.1 -4.5 1.9 -1 sum = 3.9
⇑
6. Samples of programs that use the goto statement
Example 1. Organize a cyclic process of calculating the sum of items of a sequence using the goto operator. The end of the sequence is the number 0.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class Program { static void Main(string[] args) { // Calculation of the sum of the elements of a sequence // goto statement double sum = 0; double number; Console.WriteLine("Enter numbers:"); number = 1; // label, symbolizes the beginning of a repeating block begLoop: number = Double.Parse(Console.ReadLine()); if (number != 0) { sum += number; goto begLoop; // переход на метку begLoop } Console.WriteLine("sum = {0}", sum); Console.ReadKey(); } } }
The result of the program
Enter numbers: 5 6 1 0 sum = 12
⇑
Related topics
⇑