✅ For loops, while loops, and TryParses
I still don't understand them, nor do I know how to apply em.
133 Replies
That's a very vague question, which makes it hard to answer. Loops are completely different to
TryParse
What specifically are you having trouble with?Angius
REPL Result: Success
Console Output
Compile: 516.128ms | Execution: 55.582ms | React with ❌ to remove this embed.
Sorry, so here's what I don't really get; I know the format it's in, but not the specifics for example, "for (int i = 1; i <= num; i++)", or "while(true)", I really don't get it thus I can't really apply it...
Since I also mentioned TryParses I just don't get em at all, I only mentioned it since I learned with both loops in a course I'm going through
Wth
Have a run through https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/branches-and-loops?tutorial-step=1, then if you don't understand something specific, ask here
Branches and loops - Introductory interactive tutorial
In this tutorial about branches and loops, you'll use your browser to learn C# interactively. You'll write C# code and see the results of compiling and running your code directly in the browser.
thats what u mean by dont understand @Kenji!!!
Then what is I, like why do we have it
In other words,
i is just a number to keep track of iterrations
a for loop could as well just be
for(;;)
Think of it this way:
can be written as
What if I wanted to make something like the following, "
*
**
"?
what?
u mean like a slope of
*
characters?Yeah
Then you want 2 loops. The first one prints each line every time it loops, and then there's a second one inside which prints the right number of '*'s
Or

They're obviously trying to learn basic loops. That's unhelpful
wtf, didnt know u could do that in c#
Fair
Also your terminal is broken ><

is is basically:
wait what's /n
Ligatures
\n
is newlineAngius
REPL Result: Success
Console Output
Compile: 342.738ms | Execution: 18.458ms | React with ❌ to remove this embed.
.WriteLine()
does (roughly) the same as .Write("\n")
Oh
wait so new string would be the same?
Wait until you see
\t
:when:\n is a way to say
newline
what did it do?
it that a tab?Since in your code you've written newstring
.WriteLine("foo")
is the same as .Write("foo" + "\n")
Yeah, \t
is a tabI get that, but what's new string then

Ignore that code
They went too advanced too quickly
It repeats the given character a number of times
I understand that one way more
it creates a new string of length
i
all filled with *
Angius
REPL Result: Success
Result: string
Compile: 190.682ms | Execution: 14.982ms | React with ❌ to remove this embed.
The point of this exercise is to understand loops
I get that, but when I see advanced applications of concepts I get to understand stuff easier dk why
This is unrelated to loops, though
btw tryparse tries to parse something and if it parses successfully it returns
true
otherwise it returns false
Yeah, but it's part of that code and since it's shorter and clearer I just end up asking one thing
Ik that, I just don't get the entire format
Which part of this, in particular, is unclear?
int.TryParse("45", out num)
|
|
int.TryParse(string to parse, where to put the number)
ohhhhhhhhh
Thank you!
out
is a mechanism for returning multiple values from a method.
When we declare a method parameter as out
, it means we use it to pass a value out of the method, rather than passing a value into the methodto explain this code:
TryParse
uses that to output 2 values: it returns a bool
saying whether parsing was successful, and also uses an out
parameter to return the parsed valueto explain this code:
its basically a way to mutate passed values
Wdym by mutate?
change
so instead of doing:
you can just do:
Argument 1 must be passed with the 'out' keyword
right
So I made this
Console.WriteLine("enter number");
Console.ReadLine();
int num;
while (!int.TryParse(Console.ReadLine(), out num))
{
Console.WriteLine("Invalid input! Please enter a positive integer greater than 0.");
}
for (int i = 1; i <= num; i++)
{
Console.WriteLine("*");
}
Console.Write("\n");
It has 2 issues most important one is that it doesn't make a slope it just repeats the charcther
two forloops
because:
one forloop is for rows
second is for columns
ah
I'll try to apply it
and change WriteLine to Write
in this code you could enter a -1, there's no check for that
What would that do
What's the difference
it just wouldnt run the loops
WriteLine
applies an enter
at the end of the line basically
Write
does not
Or in other words, it adds a line endConsole.Write("enter number");
Console.ReadLine();
int num;
while (!int.TryParse(Console.ReadLine(), out num))
{
Console.WriteLine("Invalid input! Please enter a positive integer greater than 0.");
}
for (int i = 0; i < num; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("*");
}
Console.Write("\n");
}
alright thanks
But it still worked without a-1
or whatever that is
if you want to check for negative just inside while add &&num>0
wha
i hate drawing
*
inside console so much, the teacher made me do christmass trees inside the console for like 3 months straightStill dk what that does
so mutate, and make sure it's higher than 0?
yeah
Isn't your logic inverted there? If the parse is not successful, and
num > 0
...not sure if u have to do a null check tho
its while
people please don't try to put multiple steps in a single line
oh nvm
use an infinite loop
Code makes no sense
it should be num<0
and break out manually
No it shouldn't
thats also a way
You have to remove the
!
If
int.TryParse
returns false, num
is always 0oh
The reason it's confusing is because you should not make a conditional like this
Just put it on separate lines
but he wants the user to repeat until passed a valid number
Because I don't think it helps the case
Yes use a while loop that just has
true
in it and break;
manuallyYou probably want || rather than && then. But splitting it out a bit rather than cramming everything onto one line will help
huh
It's going to be confusing for them, definitely, but in the end it willbe a lot easier to manage
while (true)
{
string input = Console.ReadLine();
if (int.TryParse(input, out num) && num > 0) { Console.WriteLine(new string('*', num)); break; } else { Console.WriteLine("Invalid input. Please enter a positive number."); } ?
if (int.TryParse(input, out num) && num > 0) { Console.WriteLine(new string('*', num)); break; } else { Console.WriteLine("Invalid input. Please enter a positive number."); } ?
Yes, this is exactly what I mean
🔥
Infinite while loop until they pass the correct number, in which case it ends by breaking the loop with
break;
Nicealso
Now I can't comment on if it ends up working as a whole, but this correcty gets a number
Console.Write("enter number");
Console.ReadLine();
int num;
while (!int.TryParse(Console.ReadLine(), out num))
{
Console.Write("Invalid input! Please enter a positive integer greater than 0.");
}
for (int i = 0; i < num; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("");
}
Console.Write("\n");
}
for (int i = 0; i < - num; i++)
{
for(int j = 0; j < - i; j++) {
Console.Write("");
}
}
Console.Write("*");
I'll do a while version later
So i'm trying to reverse it now
*
*
*
**
*
**
*
Like this
Wait
uh
a reversed slope?
yeah
inside j start from i and substract one
for(int j = i; j < - i; j--)?
oh j greater than 0
just the
-i
wrong
btw if u want to do codeblocks:
\didn't work

just the output
```<language>(for example cs (c#))
```
hmm, idk then, i really hate doing this stuff, i get flashbacks
\like this\
damn
\damn\
use `x3 then newline and `x3 again
wdym by that
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
ah
yeah
and u can add language after the ```
for example
c#
is cs
oh shi
at the top one
yeah
it adds highlights which helps when reading code
The mix of up-counting loops, down-counting loops, loops starting at 1 and at 0, is really jarring ><
yeah
it makes my brain go into error mode
It be painful
Well it's weird to me ask here, but what're concepts I should learn for game dev
dont understand the question
Models, Views and ViewModels are a good start
mvvm for short
are those to display graphics or for scripting
bost
both
nvm, scripting
mvvm is a very clean way to code
MVVM is great
Alright after I finish my course I'll learn those
Do they give any headstarts for c++ since I want learn that too
sorry to ask this here
I mean, sure, knowing C# will give you a headstart in C++
With all that said, I don't think MVVM is used all that often in gamedev
Unreal Engine uses it for UI binding... but that's about it
Not a native thing in Unity at least, though I can see the benefit of implementing a system for it
Oh, look, it does actually exist in a way https://docs.unity3d.com/Packages/[email protected]/manual/mvvm-intro.html
Yeah, that's certainly nowhere near where I'd start for game dev!
$close
If you have no further questions, please use /close to mark the forum thread as answered