C
C#14mo ago
🃏

✅ return to start of the code after a while loop break

hi eggy pls help me here
151 Replies
EntireTeamIsBabies
here we are. Thank you for helping out. So I don't know why but the Executionflow confuses me a wee bit, so I thought I would replicate your scenario to see where I am fekking up.
🃏
🃏OP14mo ago
so lemme explain it quickly in short
EntireTeamIsBabies
namespace Beräknare_V1._0
{
class Program
{

static void main(string[] args )
{

Console.Writeline("Välj följande alternativ mellan 1-12 för att beräkna:");

string MainInput = Console.ReadLine();

while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UsermainInput <= 13)
{
Console.Clear();
if (UserMainInput >= 13)
{
Console.Writeline("Ogiltig val, du kan bara välja mellan 1-12");
Console.Readline();
}
else
{
Console.Writeline("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");

break;

}

}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12.");
Console.ReadLine();
break
}
}

}
namespace Beräknare_V1._0
{
class Program
{

static void main(string[] args )
{

Console.Writeline("Välj följande alternativ mellan 1-12 för att beräkna:");

string MainInput = Console.ReadLine();

while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UsermainInput <= 13)
{
Console.Clear();
if (UserMainInput >= 13)
{
Console.Writeline("Ogiltig val, du kan bara välja mellan 1-12");
Console.Readline();
}
else
{
Console.Writeline("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");

break;

}

}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12.");
Console.ReadLine();
break
}
}

}
` This is what I copied
🃏
🃏OP14mo ago
so i have a school assignment where i need to make 12 different calculators that i know how to make and now is the issue where i forgot how to return back to the start of the code when it breaks out of that specific loop when the user types in a char instead of a int
EntireTeamIsBabies
It's true that this Gives hickups. The ReadLine-Method takes String by default, and your TryParse Operation Is correct.
🃏
🃏OP14mo ago
and the first if statement says that if it cannot parse it from a string to a int that is bigger than 1 and less the 13 it will spit out a line where it says "wrong information given please pick a calculation between the numbers 1-12 point of the tryparse is that i ensure the user dont input a char instead of a int by accident
EntireTeamIsBabies
This is also good. It's just doing the same verification from a different angle. I'm going to run your code line-by-Line with the debugger, to see how this Execution context does it's work. good?
🃏
🃏OP14mo ago
mhm just pointing it out again that everything works perfectly fine but on the last part i want it to return to the start of the code again but i dont know how to tell it im new to programming hehe this is the first year of programming in my high school at sweden
EntireTeamIsBabies
I am myself a Self employed Graphic designer Rolling into C# from Python, So much of what we're doing now I am supposed to be capable of handling, but I too am learning this. I'm currently making a mess of brackets, sorry.
🃏
🃏OP14mo ago
haha np man take youre time
Pobiega
Pobiega14mo ago
Have you learned about methods yet? A great way to solve this would be to break up your really big Main method in smaller methods
🃏
🃏OP14mo ago
nope dont even know what that is
Pobiega
Pobiega14mo ago
ok... what about switch?
🃏
🃏OP14mo ago
ah yeah know about dat
Pobiega
Pobiega14mo ago
Can you see a good usecase for it here?
🃏
🃏OP14mo ago
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🃏
🃏OP14mo ago
this is what we are following rn from our teacher we on bool rn
EntireTeamIsBabies
W3 is a good source for rookie-material. But in all fairness, they are a bit hesitant on updating on new changes.
Pobiega
Pobiega14mo ago
huh? but bool is above both switch, if/else, while... what the helvete is your lärare doing
🃏
🃏OP14mo ago
bro its a spanish teacher that is teaching us he can barely teach other people in a proper way like i have to go out of my way and go teach my self cuz how bad this only programming teacher we have at our high school
Pobiega
Pobiega14mo ago
🙂 Unfortunately this is not rare "Don't let school get in the way of your education" is a saying for a reason
🃏
🃏OP14mo ago
im at school for a reason bro i want to be tought/teached 🤣
EntireTeamIsBabies
lol, I get no errors from inside the code, but somehow the Main method is not being recognised as a valid entry point. Current code looks like this:
namespace Beräknare_V1_0
{
class Program
{

static void main(string[] args )
{

Console.Writeline("Välj följande alternativ mellan 1-12 för att beräkna:");

string MainInput = Console.ReadLine();

while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UsermainInput <= 13)
{
Console.Clear();
if (UserMainInput >= 13)
{
Console.Writeline("Ogiltig val, du kan bara välja mellan 1-12");
Console.Readline();
}
else
{
Console.Writeline("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");

break;

}

}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12.");
Console.ReadLine();
break
}
}

}


}
}
namespace Beräknare_V1_0
{
class Program
{

static void main(string[] args )
{

Console.Writeline("Välj följande alternativ mellan 1-12 för att beräkna:");

string MainInput = Console.ReadLine();

while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UsermainInput <= 13)
{
Console.Clear();
if (UserMainInput >= 13)
{
Console.Writeline("Ogiltig val, du kan bara välja mellan 1-12");
Console.Readline();
}
else
{
Console.Writeline("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");

break;

}

}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12.");
Console.ReadLine();
break
}
}

}


}
}
did I forget somehting?
Pobiega
Pobiega14mo ago
I'm confused. Who is helping who here? 😄
🃏
🃏OP14mo ago
me
EntireTeamIsBabies
I am ( trying to help him out) but I am not as speedy as I want to be.
🃏
🃏OP14mo ago
um how do i do the grey thingis like your text
Pobiega
Pobiega14mo ago
$codegif
🃏
🃏OP14mo ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beräknare_V1._0
{
class Program
{
static void Main(string[] args)
{

int UserMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
String MainInput = Console.ReadLine();
while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UserMainInput <= 12)
{
Console.Clear();
if (UserMainInput >= 12)
{
Console.WriteLine("Ogiltig val, du kan bara välja mellan 1-11.");
Console.ReadLine();
}
else
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 11 bara");

break;
}


}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
Console.ReadLine();

}
}








}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beräknare_V1._0
{
class Program
{
static void Main(string[] args)
{

int UserMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
String MainInput = Console.ReadLine();
while (true)
{
if (int.TryParse(MainInput, out UserMainInput))
{

while (UserMainInput >= 1 && UserMainInput <= 12)
{
Console.Clear();
if (UserMainInput >= 12)
{
Console.WriteLine("Ogiltig val, du kan bara välja mellan 1-11.");
Console.ReadLine();
}
else
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 11 bara");

break;
}


}
Console.ReadLine();
break;

}
else
{
Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
Console.ReadLine();

}
}








}
}
}
Pobiega
Pobiega14mo ago
you skipped a step
🃏
🃏OP14mo ago
that is ?
EntireTeamIsBabies
cs after the first backticks
Pobiega
Pobiega14mo ago
its tripple backtick, cs, enter paste enter tripple backtick enter
EntireTeamIsBabies
disco!
🃏
🃏OP14mo ago
mhm but now how do i make it return to the top of the code after a string has been inputed
Pobiega
Pobiega14mo ago
let me clean that up for you
public class Program
{
public static void Main()
{
int userMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
string mainInput = Console.ReadLine();
while (true)
{
if (int.TryParse(mainInput, out userMainInput))
{
while (userMainInput >= 1 && userMainInput <= 12)
{
Console.Clear();
if (userMainInput >= 12)
{
Console.WriteLine("Ogiltig val, du kan bara välja mellan 1-11.");
Console.ReadLine();
}
else
{
Console.WriteLine(
"om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
break;
}
}

Console.ReadLine();
break;
}
else
{
Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
Console.ReadLine();
}
}
}
}
public class Program
{
public static void Main()
{
int userMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
string mainInput = Console.ReadLine();
while (true)
{
if (int.TryParse(mainInput, out userMainInput))
{
while (userMainInput >= 1 && userMainInput <= 12)
{
Console.Clear();
if (userMainInput >= 12)
{
Console.WriteLine("Ogiltig val, du kan bara välja mellan 1-11.");
Console.ReadLine();
}
else
{
Console.WriteLine(
"om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
break;
}
}

Console.ReadLine();
break;
}
else
{
Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
Console.ReadLine();
}
}
}
}
tada
🃏
🃏OP14mo ago
thx now HOW cuz i need to hand this is today
Pobiega
Pobiega14mo ago
There is quite a bit wrong with this code as is to be honest
🃏
🃏OP14mo ago
that is pls tell me
EntireTeamIsBabies
to manipulate controlflow of your code you could employ jumpstatements. But they are generally frowned upon.
Pobiega
Pobiega14mo ago
Well, look at where you read the input and where you parse it those should reasonably be next to eachother, and definately not on either side of a while(true)
🃏
🃏OP14mo ago
point of that is so the user does not enter a char instead of a int
Pobiega
Pobiega14mo ago
I understand that But how many times does this code ask the user to write a value?
🃏
🃏OP14mo ago
only once
Pobiega
Pobiega14mo ago
only once. so if you gave it... say "cheese"
🃏
🃏OP14mo ago
oooooooooooooooooooooooooooooooooooooooooooh now i get it
Pobiega
Pobiega14mo ago
it will actually end in a forever loop that you cant escape
🃏
🃏OP14mo ago
i put the input part in a while loop to
Pobiega
Pobiega14mo ago
yes
🃏
🃏OP14mo ago
HAHHAHA thanks bro youre the best I LOVE YOU
EntireTeamIsBabies
he is cheers mate.
🃏
🃏OP14mo ago
i was literally thinking for an hour today in the class and i couldnt figure out what the next step to do
Pobiega
Pobiega14mo ago
you tell us I can immediately identify one thing thats not quite right thou
🃏
🃏OP14mo ago
that is ?
Pobiega
Pobiega14mo ago
In what scenario can the execution reach the red marked line
No description
🃏
🃏OP14mo ago
after it tried the parse
Pobiega
Pobiega14mo ago
all the needed information is in the screenshot.
🃏
🃏OP14mo ago
so i have to have the input check first then it tells me that the info is wrong or not right ?
Pobiega
Pobiega14mo ago
Im not sure what you are asking tbh but its not relevant to my question
🃏
🃏OP14mo ago
nvm yeah it is
Pobiega
Pobiega14mo ago
Look at the piece of code I screenshotted and where the red line is
🃏
🃏OP14mo ago
im just mind fucking u hehe
Pobiega
Pobiega14mo ago
assume we reach the while loop when can we enter the if? during what scenario/condition?
🃏
🃏OP14mo ago
when the number is smaller then 12 no no bigger
Pobiega
Pobiega14mo ago
right. But when can we enter the while loop?
🃏
🃏OP14mo ago
when the input is smaller then 12 so 11
Pobiega
Pobiega14mo ago
well, between 1 and 12 so a value that is guaranteed to be between 1 and 12... can't really be above 12, can it?
🃏
🃏OP14mo ago
bro bro i forgot to tell you the point of that is to tell the user that you can only pick thrue these numbers
Pobiega
Pobiega14mo ago
I understand
🃏
🃏OP14mo ago
then it enters the if and tells the user
EntireTeamIsBabies
That is what kept throwing me off. I thought i was losing the ability to read lol
🃏
🃏OP14mo ago
its the swedish part haha
Pobiega
Pobiega14mo ago
Uncle. Look at my bio.
Pobiega
Pobiega14mo ago
No description
🃏
🃏OP14mo ago
vänta vah hahaha
Pobiega
Pobiega14mo ago
See anything that stands out? 😛
🃏
🃏OP14mo ago
HAHHAHAHAHAHHAHA so i take that back what i said
EntireTeamIsBabies
Glad to have you on board tho.
Pobiega
Pobiega14mo ago
Now, back to the numbers
🃏
🃏OP14mo ago
yes
Pobiega
Pobiega14mo ago
Can you name a number that is between 1 and 12, but also greater than 12
🃏
🃏OP14mo ago
5 and 13 ?
Pobiega
Pobiega14mo ago
no a single number
🃏
🃏OP14mo ago
hm dafuq
Pobiega
Pobiega14mo ago
indeed its a trick question, its obviously impossible
🃏
🃏OP14mo ago
so i have not told it what number i want it to be on the else part right ?
Pobiega
Pobiega14mo ago
What im trying to get to is...
🃏
🃏OP14mo ago
?
Pobiega
Pobiega14mo ago
bro chill I need to draw
🃏
🃏OP14mo ago
ok mb
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
the red block is only ever accessed by a number that is 1 to 12 the green block is only ever accessed by a number that is both between 1-12, but also equal too or greater than 12 thats a really convoluted way to say 12 you could easily remove the green block and just reduce the top limit by 1 in the while however that while is itself weird its a while with a constant condition, as you never modify userMainInput inside the loop so its another forever-loop 🙂
🃏
🃏OP14mo ago
well i have before the loop i think nvm
Pobiega
Pobiega14mo ago
sure before the loop but a loop with a constant condition is the same as while(true) (assuming your condition evaluates to true)
🃏
🃏OP14mo ago
what the hell thats the wrong code kompis det är fel
Pobiega
Pobiega14mo ago
its your code. or whatever was pasted here I did not write this 😛
🃏
🃏OP14mo ago
wait heres the original
Pobiega
Pobiega14mo ago
here is the code I took
🃏
🃏OP14mo ago
so basically youre trying to tell me the 2 while loops is useless only need 1 right ? remove the true on and just add it to the if statement
Pobiega
Pobiega14mo ago
no
🃏
🃏OP14mo ago
fan
Pobiega
Pobiega14mo ago
if only 1 to 11 are the valid numbers, why does your condition allow 12?
EntireTeamIsBabies
It's the peculiar conditioning for those 2 loops that make for unnessecery nesting, - if those conditions never will be met then the code is not following its function
🃏
🃏OP14mo ago
i fixed that to 11
Pobiega
Pobiega14mo ago
why is the message about invalid numbers INSIDE the body that has been checked so the number is valid? ok, updated now the value is 1-11, so it can never be 12 or greater so we can remove the green block
🃏
🃏OP14mo ago
exactly yes
Pobiega
Pobiega14mo ago
it now looks like this
while (userMainInput >= 1 && userMainInput <= 11)
{
Console.WriteLine(
"om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
break;
}
while (userMainInput >= 1 && userMainInput <= 11)
{
Console.WriteLine(
"om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
break;
}
🃏
🃏OP14mo ago
well bro the point of that green block i just remembered that i wanted to tell the user that
Pobiega
Pobiega14mo ago
that loop will get broken immediately, meaning this is just a weird way to write if 😄 I know you dont need to explain Im saying you dont need that block, because you already have that block
🃏
🃏OP14mo ago
so if its not 1-11 it just countinues on the next part of the code ?
Pobiega
Pobiega14mo ago
yes
🃏
🃏OP14mo ago
oh
Pobiega
Pobiega14mo ago
so you see how thats literally just an if right?
🃏
🃏OP14mo ago
yeah yeah
Pobiega
Pobiega14mo ago
cool
🃏
🃏OP14mo ago
ill just add other part where to check if its a string or a int
Pobiega
Pobiega14mo ago
btw, I want to change something fairly big.. do you know what !true is?
🃏
🃏OP14mo ago
not true ?
Pobiega
Pobiega14mo ago
exactly
🃏
🃏OP14mo ago
teach me
Pobiega
Pobiega14mo ago
The way you are structuring your code right now with increasing levels of nesting is... not great we can inverse most of those nestings
🃏
🃏OP14mo ago
realised that
Pobiega
Pobiega14mo ago
for example, the loop that makes sure you entered a valid value? well if we inverse that...
🃏
🃏OP14mo ago
how will it look like
Pobiega
Pobiega14mo ago
int userMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
while (true)
{
string mainInput = Console.ReadLine();
if (int.TryParse(mainInput, out userMainInput))
{
break;
}

Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
}

// at this point, we know the value is a number.

if (userMainInput >= 1 && userMainInput <= 11)
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
}

Console.ReadLine();
int userMainInput;

Console.WriteLine("Välj följande alternativ mellan 1-11 för att beräkna: ");
while (true)
{
string mainInput = Console.ReadLine();
if (int.TryParse(mainInput, out userMainInput))
{
break;
}

Console.WriteLine("Ogiltig tal, välj en beräkning mellan 1-11. ");
}

// at this point, we know the value is a number.

if (userMainInput >= 1 && userMainInput <= 11)
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 11 bara");
}

Console.ReadLine();
its now a lot shorter we should fix a few more things thou we should keep asking them for a number until its not just a number, but a number between 1 and 11 so why not do that in the same loop? Can you figure out a way to do that?
🃏
🃏OP14mo ago
so my code works fine just the issue is that i have nested the shit that makes the loop for the number checking fuck up if the value is between 1-11 right ? alltså kåden startar ej om och säger att du har valt fel nummer istället så stänger den ba av ellerhur ?
Pobiega
Pobiega14mo ago
? English only on the server please. I honestly kinda dont know what "works" looks like, Im just fixing issues as I see them bad design is an issue in my eyes 🙂
🃏
🃏OP14mo ago
mhm you did thx for the help man lemme fix it and ill show u how it looks
EntireTeamIsBabies
Also Thanks for letting me tag along.
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
any time 😄 list of help threads I'm active in
🃏
🃏OP14mo ago
class Program
{
static void Main(string[] args)
{

int UserMainInput;

while (true)
{
Console.Clear();
Console.WriteLine("Välj följande alternativ mellan 1-12 för att beräkna: ");
String MainInput = Console.ReadLine();

if (int.TryParse(MainInput, out UserMainInput))
{

break;
}

Console.Clear();
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12. ");
Console.ReadLine();
}

if(UserMainInput >= 1 && UserMainInput <= 12)
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");
Console.ReadLine();
}






}
}
class Program
{
static void Main(string[] args)
{

int UserMainInput;

while (true)
{
Console.Clear();
Console.WriteLine("Välj följande alternativ mellan 1-12 för att beräkna: ");
String MainInput = Console.ReadLine();

if (int.TryParse(MainInput, out UserMainInput))
{

break;
}

Console.Clear();
Console.WriteLine("Ogiltig information, välj en beräkning mellan 1-12. ");
Console.ReadLine();
}

if(UserMainInput >= 1 && UserMainInput <= 12)
{
Console.WriteLine("om du ser det här så har du gjort while loop rätt som 1 till 12 bara");
Console.ReadLine();
}






}
}
fixed it yay works perfectly as i wanted
Pobiega
Pobiega14mo ago
.. I somehow doubt that
🃏
🃏OP14mo ago
whaaat why you dont have faith in the newcomer it actually works try it
Pobiega
Pobiega14mo ago
I dont need to. I can read it
🃏
🃏OP14mo ago
ah ok
Pobiega
Pobiega14mo ago
wait, oooh. You only wanted to validate numbers= that was the entire programs purpose?
🃏
🃏OP14mo ago
what now HAHHAA YESSSS
Pobiega
Pobiega14mo ago
wtf
🃏
🃏OP14mo ago
then i can continue with the rest
Pobiega
Pobiega14mo ago
also, the if() { break; } makes me feel very uneasy 😄 I know I wrote it, but it feels so bad
EntireTeamIsBabies
lol, I thought it had to represent a calculation. 🙂
🃏
🃏OP14mo ago
it does
Pobiega
Pobiega14mo ago
the namespace is literally "calculator"
🃏
🃏OP14mo ago
now i can just paste everything in
EntireTeamIsBabies
Cheers tho. I'm off for homework.
🃏
🃏OP14mo ago
the calcutlation of second degree question, quadratic calculation this is the main is i had now everything is gonna run perfectly fine yo @Pobiega you still there ?
Pobiega
Pobiega14mo ago
Every now and then
🃏
🃏OP14mo ago
you still there ? ?
Pobiega
Pobiega14mo ago
just ask your question
Want results from more Discord servers?
Add your server