C#/C# Tutorial

C# Jump Statement | break, continue, goto, return | break vs continue | jump out of | C# Tutorial for Beginners

DragonTory 2023. 2. 24. 11:41
반응형

C# Jump Statement 

break, continue, goto, return | break vs continue | jump out of

 

In C#, jump statements are used to transfer control to another part of the program. There are four types of jump statements in C#:

1. break statement: It is used to exit from the current loop or switch statement.

2. continue statement: It is used to skip the current iteration of a loop and continue with the next iteration.

3. goto statement: It is used to transfer control to a labeled statement elsewhere in the program. However, it is generally considered a bad programming practice and should be used sparingly.

4. return statement: It is used to exit from a method and return a value to the calling method.

Here's an example that illustrates the use of break and continue statements in a for loop:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
        break; // exit the loop when i equals 5
    if (i % 2 == 0)
        continue; // skip even numbers
    Console.WriteLine(i);
}

In this example, the break statement is used to exit the loop when i equals 5, and the continue statement is used to skip even numbers and continue with the next iteration of the loop.

 

break statement:

the break keyword is used to immediately exit from a loop or switch statement. It is typically used when a certain condition is met and there is no need to continue iterating or processing any further.

Here's an example that illustrates the use of the break statement in a while loop:

int i = 0;
while (i < 10)
{
    Console.WriteLine(i);
    if (i == 5)
        break; // exit the loop when i equals 5
    i++;
}

In this example, the break statement is used to exit the loop when i equals 5. Without the break statement, the loop would continue to iterate until i reaches 10.

The break statement can also be used in a switch statement to exit the switch block early. 

 

Here's an example:

int num = 2;
switch (num)
{
    case 1:
        Console.WriteLine("One");
        break;
    case 2:
        Console.WriteLine("Two");
        break; // exit the switch block after printing "Two"
    case 3:
        Console.WriteLine("Three");
        break;
    default:
        Console.WriteLine("Invalid number");
        break;
}

In this example, the break statement is used after the case for num equals 2, which causes the switch block to exit early after printing "Two".


continue statement:

 

 the continue keyword is used to immediately skip the current iteration of a loop and continue with the next iteration. It is typically used when a certain condition is met and there is no need to execute the remaining code for the current iteration.

Here's an example that illustrates the use of the continue statement in a for loop:

for (int i = 0; i < 10; i++)
{
    if (i % 2 == 0)
        continue; // skip even numbers
    Console.WriteLine(i);
}

In this example, the continue statement is used to skip even numbers and continue with the next iteration of the loop. Without the continue statement, the code within the loop would be executed for every value of i, even if it is an even number.

The continue statement can also be used in a while or do-while loop in a similar way. 

Here's an example:

int i = 0;
while (i < 10)
{
    i++;
    if (i % 2 == 0)
        continue; // skip even numbers
    Console.WriteLine(i);
}

In this example, the continue statement is used to skip even numbers and continue with the next iteration of the loop. Without the continue statement, the code within the loop would be executed for every value of i, even if it is an even number.

 

goto statement:

 

the goto statement is used to transfer control to a labeled statement within the same method. The goto statement is often used to implement a jump from one part of the code to another part that cannot be reached by normal control flow statements like loops, conditional statements, and method calls.

Here is an example of how the goto statement can be used:

start:
Console.WriteLine("Enter a number:");
int num = int.Parse(Console.ReadLine());

if (num <= 0)
    goto start; // jump back to the start label

Console.WriteLine("The number you entered is: " + num);

In this example, the goto statement is used to jump back to the start label (start:) if the user enters a non-positive number. Without the goto statement, the program would exit after printing an error message.

It is important to use the goto statement with caution because it can make the code hard to read and understand. In general, it is a good practice to use structured control flow statements like loops, conditional statements, and method calls instead of the goto statement.

 

return statement:

 

the return statement is used to exit from a method and return a value to the calling method. The return statement can be used with or without a value depending on whether the method returns a value or not.

Here is an example of a method that uses the return statement to return a value:

public int Add(int a, int b)
{
    int sum = a + b;
    return sum;
}

In this example, the Add method takes two integers as input parameters, adds them together, and returns the result as an integer value using the return statement.

The return statement can also be used without a value to exit from a method without returning a value. 

Here is an example:

public void DisplayMessage(string message)
{
    if (string.IsNullOrEmpty(message))
        return; // exit the method if the message is null or empty

    Console.WriteLine(message);
}

In this example, the DisplayMessage method takes a string as an input parameter, checks if the string is null or empty, and exits the method using the return statement if the string is null or empty. If the string is not null or empty, the method continues to execute and displays the message using the Console.WriteLine statement.

The return statement can also be used to exit from a loop or a switch statement early by placing it inside the loop or switch block. When the return statement is executed, control is immediately transferred back to the calling method.

 

the return statement can be used in a switch statement to exit the switch block early and return a value to the calling method. When the return statement is executed, control is immediately transferred back to the calling method.

Here is an example of a switch statement that uses the return statement:

public string GetDayOfWeek(int dayNumber)
{
    switch (dayNumber)
    {
        case 0:
            return "Sunday";
        case 1:
            return "Monday";
        case 2:
            return "Tuesday";
        case 3:
            return "Wednesday";
        case 4:
            return "Thursday";
        case 5:
            return "Friday";
        case 6:
            return "Saturday";
        default:
            return "Invalid day number";
    }
}

 

 

반응형