How to create a loop that run the code from beginning

Viewed 36

How to loop back the code to the first line after it done the calculation?

Console.WriteLine("Enter Temp in Celcius");
int x = Convert.ToInt16(Console.ReadLine());
if (x <= -271.15)
    {
        Console.WriteLine("Temperature below absolute zero!");
    }
else
    {
        Console.WriteLine("Your answer is " + ((x * 1.8) + 32) + "F");
    }
1 Answers

If its a continous loop thats required, use a while or do while loop and exit based on user input. The do while version executes the code atleast once and then executes further till the condition turns false and while version executes only when condition is met and conntinues to execute till the condition turns false

bool continueProcessing = true;
while (continueProcessing) --condition
{
Console.WriteLine("Enter Temp in Celcius");
int x = Convert.ToInt16(Console.ReadLine());
if (x <= -271.15)
    {
        Console.WriteLine("Temperature below absolute zero!");
    }
else
    {
        Console.WriteLine("Your answer is " + ((x * 1.8) + 32) + "F");
    }
}

Now inside the loop, get the user input and check if he wants to continue and set the value of continueProcessing to false. Try to implement that part yourself and see :)

Related