Home > Mobile >  How to use loops instead of goto statement
How to use loops instead of goto statement

Time:07-13

I'm new to C# and want to implement loops , right now I'm using goto statement and labels but I have read that it is not suggested to use goto statement

so i was thinking to implement loops instead of goto and labels , byt i dont know how can i replace goto with loops

and if possible please also give a small explanation of the answer

here is my code with goto and labels

using System;



/* A simple coffee ordering program
 * used switch,if else and lable*/

class Program
{
    public static void Main()
    {


        int TotalCoffeeCost = 0;

    // Start is a lable to point to this location so i can use it in goto 
    Start:
        Console.WriteLine("");
        Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
        Console.Write(" "); 
        int CoffeeChoice = int.Parse(Console.ReadLine());

        switch (CoffeeChoice)
        {
            case 1:
                TotalCoffeeCost  = 2;
                break;
            case 2:
                TotalCoffeeCost  = 5;
                break;
            case 3:
                TotalCoffeeCost  = 7;
                break;
            default:
                Console.WriteLine("");
                Console.WriteLine(" Please enter a vaild choice");
                goto Start;
        }


    // YesOrNo is a lable to point to this location so i can use it in goto 
    YesOrNo:
        Console.WriteLine("");
        Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
        Console.Write(" ");
        string UserChoice = Console.ReadLine();
        string upperCaseChoice = UserChoice.ToUpper();

        if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
        {
            goto Start;
        }else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
        {
            goto LastConfirmation;
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter a vailed choice");
            goto YesOrNo;
        }


    // LastConfirmation is a lable to point to this location so i can use it in goto
    LastConfirmation:
        Console.WriteLine(" ");
        Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
        Console.Write(" ");
        string Anything = Console.ReadLine(); 
        string UpperCaseAnything = Anything.ToUpper();
        if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
        {
            goto Amount;
        }
        else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
        {
            goto Start;
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter a vaild choice");
            goto LastConfirmation;
        }



    // Amount is a lable to point to this location so i can use it in goto 
    Amount:
        Console.WriteLine("");
        Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
        Console.WriteLine("");
        Console.WriteLine(" Please pay the amount by entering the amount bellow");
        Console.Write(" ");
        int EnterdAmt = int.Parse(Console.ReadLine());
        if(EnterdAmt != TotalCoffeeCost)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please pay the correct amount");
            goto Amount;
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");

        }

    }
}

CodePudding user response:

We can use methods and a simple bool "lock" that either keeps you inside the while loop, or breaks out of it when conditions are met:

using System;

/* A simple coffee ordering program
 * used switch,if else and lable*/

class Program
{
    public static void Main()
    {
        var exiting = false;
        while (!exiting)
        {
            programLoop();
        }
    }

    private int askForCoffee()
    {
        bool invalidChoice = true;
        while(invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
            Console.Write(" "); 
            int CoffeeChoice = int.Parse(Console.ReadLine());

            switch (CoffeeChoice)
            {
                case 1:
                    TotalCoffeeCost  = 2;
                    invalidChoice = false;
                    break;
                case 2:
                    TotalCoffeeCost  = 5;
                    invalidChoice = false;
                    break;
                case 3:
                    TotalCoffeeCost  = 7;
                    invalidChoice = false;
                    break;
                default:
                    Console.WriteLine("");
                    Console.WriteLine(" Please enter a valid choice");
            }
        }
    }

    private void programLoop()
    {
        int TotalCoffeeCost = 0;

        TotalCoffeeCost  = askForCoffee();

        var invalidChoice = true;
        while(invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
            Console.Write(" ");
            string UserChoice = Console.ReadLine();
            string upperCaseChoice = UserChoice.ToUpper();

            if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
            {
                TotalCoffeeCost  = askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
            }
            else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
            {
                invalidChoice = false;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while(invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string Anything = Console.ReadLine(); 
            string UpperCaseAnything = Anything.ToUpper();
            if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
            {
                invalidChoice = false;
            }
            else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
            {
                return;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while(invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
            Console.WriteLine("");
            Console.WriteLine(" Please pay the amount by entering the amount bellow");
            Console.Write(" ");
            int EnterdAmt = int.Parse(Console.ReadLine());
            if(EnterdAmt != TotalCoffeeCost)
            {
                Console.WriteLine("");
                Console.WriteLine(" Please pay the correct amount");
            }
            else
            {
                invalidChoice = false;
            }
        }

        Console.WriteLine("");
        Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
    }
}

CodePudding user response:

Just use while loops and methods to make this work. Your Start method could look like this:

public void Start(int TotalCoffeCost)
{
   Console.WriteLine("");
   Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, - large");
   Console.Write(" ");
 
   int CoffeChoice = 0;
   do
   {
        CoffeChoice = int.Parse(Console.ReadLine());//Asking for Choice until you put in 1,2 or 3
   }while(CoffeChoice != 1 | CoffeChoice != 2 | CoffeChoice != 3)

   switch (CoffeeChoice)
   {
          case 1:
              TotalCoffeeCost  = 2;
              break;
          case 2:
              TotalCoffeeCost  = 5;
              break;
          case 3:
              TotalCoffeeCost  = 7;
              break;
   }
}

And you can call the method like this:

public static void Main()
{
   int TotalCoffeeCost = 0;
   Start(TotalCoffeCost);
}
  • Related