Friday, September 29, 2017

Lesson 08: Break and Continue statement


Break and Continue in C#
Break
Often times programmers need to break out of the middle of any loop – for, while, do … while, or foreach; and to do such a task, C# provides the break statement. The break statement is also used to break out of the switch statement as we have seen in the previous tutorial.
The general form of the break statement is:
         break;
Observe the following code segment:
            int i = 0;
            while (i < 10)
            {
                Console.Write(i + "\t");
                i++;
                if(i==5)
                {
                    break;
                }

            }
If the value of ‘i’ is evaluated to 5, then the value of the ‘if statement’ will be evaluated to true and the computer will execute the ‘break’ statement forcing the control to jump out of the while loop.
The above code segment can be represented diagrammatically using the following flow chart.
The output of the above piece of code is:
0       1       2       3       4

Vital Notes:
1. When a computer executes a break statement in a loop, it jumps out of the loop.
2. A break statement jumps out of a loop that immediately encloses the break statement.

Continue
The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears. It informs the computer to skip the current iteration of the loop. When it is executed it doesn’t jump out of the loop; instead, it jumps back to the condition of the loop and continues with the next iteration.

Observe the following code segment:
            int i = 0;
            while (i<10)
            {
                i++;
                if (i > 2)
                {
                    continue;
                }
               Console.WriteLine(i);
              
            }
The above piece of code can be represented using a flow chart as follows:

The following program calculates the average salary of employees using a while loop, break and continue statements.

/*
* Author: Temesghen Tekeste
* The for loop construct in C#.
* mail: mail.temesghen.tekeste@gmail.com
* */
using System;

namespace BreakAndContinueDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Average Salary Calculator");
            Console.WriteLine();
            decimal employeeSalary = 0.0M;
            int numberOfEmployees = 0;
          
            while (true)
            {
                Console.Write("Enter salary of employee number " + (++numberOfEmployees) + ": ");
                employeeSalary += Decimal.Parse(Console.ReadLine());

                Console.Write("Press \"0\" to enter another employee salary OR \"1\" to show average salary: ");
                int continueOrQuit = Int32.Parse(Console.ReadLine());
                Console.WriteLine();
                if (continueOrQuit == 0)
                {
                    continue;
                }
                else
                {
                    Console.WriteLine("Output");
                    Console.WriteLine("Number of employees: " + numberOfEmployees);
                    Console.WriteLine("Averge salary: " + (employeeSalary / numberOfEmployees));
                    break;
                }
            }

            Console.Write("Press any key to quit");
            Console.ReadKey();
        }
    }
}

Sample Output:

References:
break (msdn C# Reference)
continue (msdn C# Reference)