Topic : Loop Control Statements in C#: Goto | Break | Continue

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C# supports the following control statements.





Break Statement:

The C# language allow to bypass the Natural ending of statements by using the break. It terminates the current For Loop , While-Loop , Do-While-Loop or Switch statement and jump roughly to the next part of the code.

The Break Statement interrupts the flow of control. we have seen in switch statement that when a true case is found, the flow of control goes through every statement downward. We want That only the statements of true case should be executed and the remaining should be skipped. For this purpose, we use the "break Statement".

When the Break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.

If we are using Nested loops (i.e. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the Next line of code after the block.


 Break Statement

Syntax:

The Syntax of a break statement in C# is:


break;


Example:

Let's have an Example to know how exactly "Break statement" work in C# Program: in this example we will try to print first 10 integers (0 to 10) using For Loop but when loop will reach at integer 5, it will terminate the program using "break" as shown in example:



/*Example: Break Statement : InfoBrother:*/

using System;

namespace breakStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i =0; i<10; i++)  //Print digits from 0 to 9:
            {
                Console.WriteLine("The value of i is: " + i);

                if (i == 5)  //When loop reach at 5:
                {
                    break;  //terminate the Loop:
                }
            }

            Console.Read();
        }
    }
}


In General, we should minimize the use of break statement in loops. The switch statement is an exception in this regard where it is necessary to use the "break statement" after every case. Otherwise there may be logical error. while writing loops, we should try to execute the loops with the condition test and should try to avoid the "break statement".


Continue Statements:

The continue statement is very similar to the break statement, except that instead of terminating the loop, it starts executing the body of the loop over from the top. The continue statement skips the rest of the body of the loop and immediately checks the loop's condition. if the loop's condition remains true, the loop's execution resumes at the top of the loop.

For the For Loop , the continue causes the conditional test and increment portions of the loop to execute. For the While-Loop and Do-While-Loop , program control passes to the conditional tests.



 Continue Statement

Syntax:

The Syntax of a continue statement in C# is:

continue;


Example:

Let's have an simple example to know how continue statement works in C# program. In this example we will try to Print first 10 integers (0 to 10) using do-while loop. but here we will try to skip integer "4" using continue statement as shown in example:



/*Example: Continue Statement : InfoBrother:*/

using System;

namespace continueStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;   //local variable:
            do
            {
                if (a == 5)   //when loop will reach at 5:
                {
                    a = a + 1;  //Skip the digit 5:
                    Console.WriteLine("Skip...");
                    continue;   //and continue forward:
                }

                Console.WriteLine("The value of a is: " + a);
                a = a + 1;
            }
            while (a <= 10);  //print digits from 0 to 10;

            Console.Read();
        }
    }
}


Same like break statement we should avoid to use "Continue Statement" where it's possible. the continue statement executes some statements of the loop and then exits the loop without executing some statements after it. we can use if statement for this purpose instead of continue.



goto Statement:


A goto Statement provides an unconditional jump from the goto to a labeled statement in the same function.
goto statement is an unconditional branch of execution which used to jump the control anywhere (back and forth) in a program. Use of goto statement is highly discouraged because it is very difficult for a programmer to keep the track of execution as the control jumps from one place to the other and from there to anywhere else. we call this kind of traditional code as "Spaghetti code" .



 goto Statement

Use of goto statement is highly discouraged because it is very difficult for a programmer to keep the track of execution.


Syntax:

The Syntax of a goto statement in C# is:

 goto label;
  /*Statement
    or group
    of 
    Statements*/
 label: statements(s);


Example:

Let's have a simple example to know how goto statement work in C#. in this statement, we have three statements. and we will try to skip 2nd statement and print 3rd statement using goto statement:

/*Example: goto Statement : InfoBrother:*/

using System;

namespace gotoStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1st Statement: ");

            goto skip;   //goto label skip:

            Console.WriteLine("2nd Statement: ");

            skip:    //label:
            Console.WriteLine("3rd Statement: ");

            Console.Read();
        }
    }
}


Same like break and continue statement we should avoid to use "goto Statement" where it's possible. because it's an "Spaghetti code", it's mean that's very difficult to trace out the way of execution and figure out what the program is doing. and debugging and modifying such program is very difficult. And all of our Programs Should consist of sequences, decisions and loops.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement