Jump statements

Jump statements are used to transfer control from one point to another point in the program using some specified code while executing the program. In simple terms, while using jump statement control will jump from one point to another point. There are five keywords in the Jump Statements. we'll see all those with its definition below.

CODE

EG 1 . Login application using Goto statement

static void Main(string[] args) {
    Start:
    //Getting Username
    Console.Write("Enter Username : ");
    string username = Console.ReadLine();
    //Getting Password
    Console.Write("Enter Password : ");
    string password = Console.ReadLine();

    //Here we used Logical AND(&&), so both LHS and RHS condition need to True in order to make entire condition True
    switch (username == "info@coderscapsule.com" && password =="123456789") {
        case true:
        Console.WriteLine("You have unlocked your Phone!!!");
        break;
        case false:
        Console.WriteLine("Invalid Username/Password");
        break;
    }
    goto Start;
}



EG 2 . Login application using break statement

static void Main(string[] args) {
    //Getting Username
    Console.Write("Enter Username : ");
    string username = Console.ReadLine();
    //Getting Password
    Console.Write("Enter Password : ");
    string password = Console.ReadLine();

    //Here we used Logical AND(&&), so both LHS and RHS condition need to True in order to make entire condition True
    switch (username == "info@coderscapsule.com" && password =="123456789") {
        case true:
        Console.WriteLine("You have unlocked your Phone!!!");
        break;
        case false:
        Console.WriteLine("Invalid Username/Password");
        break;
    }
}



EG 3 . Creating For loop using continue keyword

static void Main(string[] args) {
    for (int i = 1; i <= 5; i++) {
    // if the value of i becomes 3, system will skip 3 and send the transfer to the for loop and continue with 4
    if (i == 3) {
        continue;
    }
    Console.WriteLine(i);
}



EG 4 . Creating For loop and throwing exception in the middle

static void Main(string[] args) {
    for (int i = 1; i <= 5; i++) {
    // if the value of i becomes 3, system will throw a Exception message stating "This is my Exception".
    if (i == 3) {
        throw new NullReferenceException("This is my Exception"); ;
    }
    Console.WriteLine(i);
}




(Click Here, For more exercises)