C
C#2mo ago
Staynk

Learning please help me understand.

I am sure this is so simple for most of you finding this but I am struggling and I really just want to understand it. Help.
No description
22 Replies
Keswiik
Keswiik2mo ago
What part of this are you having trouble understanding?
Staynk
StaynkOP2mo ago
I understand what they’re asking me to do I just don’t know how to get there. I know it has to be a loop could I use a do-while loop? For example “do number -1” “while number >= 0”
Keswiik
Keswiik2mo ago
what would happen in your do-while loop if the user entered 0 as a number?
Staynk
StaynkOP2mo ago
hmm It would go negative So adjust while number > 0 ?
Keswiik
Keswiik2mo ago
try it and see
Staynk
StaynkOP2mo ago
lol fail
Staynk
StaynkOP2mo ago
No description
No description
Keswiik
Keswiik2mo ago
Arithmetic operators - C# reference
Learn about C# operators that perform multiplication, division, remainder, addition, and subtraction operations with numeric types.
Keswiik
Keswiik2mo ago
read this
Staynk
StaynkOP2mo ago
the answer was —number 😎
Salman
Salman2mo ago
you dont even need a do while loop there, a simple while loop will suffice such as :
var x = 10;
while(x >=0) Console.WriteLine(x--);
var x = 10;
while(x >=0) Console.WriteLine(x--);
Keswiik
Keswiik2mo ago
if you want it to count down in real time, you'd want to look at https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.sleep?view=net-9.0 or https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.delay?view=net-9.0 (the second would require you use an async main method, though)
Staynk
StaynkOP2mo ago
I like that too That may be too advanced me right now hahah I think the concept behind that was for me to use a ooop
Salman
Salman2mo ago
btw it's better if you use the new api like:
if(int.TryParse(Console.ReadLine(),out var res)
//do something
else
//otherwise do something else
if(int.TryParse(Console.ReadLine(),out var res)
//do something
else
//otherwise do something else
in this way even if user inputs something other than an int , you would be able to gracefully handle that . note that code might need some adjustment but you get the idea
Staynk
StaynkOP2mo ago
I’m not understanding that I’m sorry I’m just learning
Salman
Salman2mo ago
what TryParse does is that , it tries to parse a string into a number if that string is a valid number e.g "10" to 10 , however if the string isn't a valid number .eg acd it would return false but if it's true it would give you the parsed value that's in the above example res
Staynk
StaynkOP2mo ago
I see and when would you use that type of statement
Salman
Salman2mo ago
when you want to parse a string to a number and you aren't sure if the string is in correct form or not like in this program, user could type anything on their console , in your Convert.ToInt32 code, if user inputs abcd, it would crash your program
Staynk
StaynkOP2mo ago
Ohhh I see Now that makes sense.
Salman
Salman2mo ago
$close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Staynk
StaynkOP2mo ago
Thank you.

Did you find this page helpful?