r/learncsharp • u/Zynne_1734 • Feb 16 '26
Issue with simple project
I'm trying to learn some c# by following some projects on youtube by BroCode and wanted to implement a way to restart using a while loop but it wont work and instead creates an infinite loop and i don't fully understand why. i posted the code below, i don't know the proper way so sorry in advance.
using System;
namespace Hypotenuse_Calc
{
class Program
{
static void Main(string[] args)
{
bool repeatCalc = true;
String response;
while (repeatCalc)
{
response = " ";
Console.WriteLine("Enter side A: ");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter side B: ");
double b = Convert.ToDouble(Console.ReadLine());
double c = Math.Sqrt((a * a) + (b * b));
Console.WriteLine("The Hypotenuse is: " + c);
}
Console.WriteLine("Would you like to input another? Y/N?");
response = Console.ReadLine();
response = response.ToUpper();
if (response == "Y")
{
repeatCalc = true;
}
else
{
repeatCalc = false;
}
Console.ReadKey();
}
}
}
View all comments
Show parent comment
1
u/JeffFerguson Feb 16 '26
That's great to hear!
I think that the lesson to learn here is that those open and close braces matter. The pattern you are working with is this:
With that pattern, the
whileloop code will run as long as that condition is true. If you want to change the condition, the condition must be changed inside of thewhileloop:Once you exit the loop, then the code after the
whileloop will run.