C
C#5mo ago
Jexs

Am I reading this right?

in this code : for (int i =0; i < 5: i++) { Console.WriteLine(i); } So am i understanding this right?: First you have to declare statement 1 witch is int i = 0; once this is declared the complier execute the code block that contains Console.WriteLine(i); statement 2 defines the condition for executing the code block. statement 3 is executed everytime after the code block has been executed?
30 Replies
Jexs
JexsOP5mo ago
Am I understanding this right *
Angius
Angius5mo ago
Basically, yeah
for (initial state; run condition; state change)
{
// ...
}
for (initial state; run condition; state change)
{
// ...
}
is how you could describe a for loop
Jexs
JexsOP5mo ago
Thanks for your reply I will now go over it ! But is this also true
Jexs
JexsOP5mo ago
No description
Jexs
JexsOP5mo ago
Like is statement one ran and then the code block runs after that statement 2 and 3 are ran by the compiler ? @ZZZZZZZZZZZZZZZZZZZZZZZZZ Like statement 1 first ran and then the code block is ran behind the scene and then statement 2 and 3 ran? Can someone answer my question c: ?
CybeR
CybeR5mo ago
u made a typo its supposed to be ; not :
No description
Jexs
JexsOP5mo ago
Oh ops But am I right ?
blueberriesiftheywerecats
First runs statement 1, than statement 2, than code, than statement 3
Jexs
JexsOP5mo ago
Are you sure @blueberriesiftheywerecats ? Can someone help me D:
Angius
Angius5mo ago
Chill, the world's not gonna end if you don't get an answer for 10 minutes
MODiX
MODiX5mo ago
Angius
REPL Result: Success
int One()
{
Console.WriteLine("One");
return 0;
}

bool Two(int a, int b)
{
Console.WriteLine($"Two (a: {a}, b: {b})");
return a < b;
}

int Three(int a, int b)
{
Console.WriteLine($"Three (a: {a}, b: {b})");
return a + b;
}

for (var i = One(); Two(i, 5); i = Three(i, 1))
{
Console.WriteLine($"- Iteration {i}");
}
int One()
{
Console.WriteLine("One");
return 0;
}

bool Two(int a, int b)
{
Console.WriteLine($"Two (a: {a}, b: {b})");
return a < b;
}

int Three(int a, int b)
{
Console.WriteLine($"Three (a: {a}, b: {b})");
return a + b;
}

for (var i = One(); Two(i, 5); i = Three(i, 1))
{
Console.WriteLine($"- Iteration {i}");
}
Console Output
One
Two (a: 0, b: 5)
- Iteration 0
Three (a: 0, b: 1)
Two (a: 1, b: 5)
- Iteration 1
Three (a: 1, b: 1)
Two (a: 2, b: 5)
- Iteration 2
Three (a: 2, b: 1)
Two (a: 3, b: 5)
- Iteration 3
Three (a: 3, b: 1)
Two (a: 4, b: 5)
- Iteration 4
Three (a: 4, b: 1)
Two (a: 5, b: 5)
One
Two (a: 0, b: 5)
- Iteration 0
Three (a: 0, b: 1)
Two (a: 1, b: 5)
- Iteration 1
Three (a: 1, b: 1)
Two (a: 2, b: 5)
- Iteration 2
Three (a: 2, b: 1)
Two (a: 3, b: 5)
- Iteration 3
Three (a: 3, b: 1)
Two (a: 4, b: 5)
- Iteration 4
Three (a: 4, b: 1)
Two (a: 5, b: 5)
Compile: 500.659ms | Execution: 50.925ms | React with ❌ to remove this embed.
Angius
Angius5mo ago
See for yourself
Jexs
JexsOP5mo ago
Sorry buddy didn’t mean to offend you
Angius
Angius5mo ago
You did not It's just a word of advice that if you expect people to always answer the minute you post a question, you're not always gonna get that
Jexs
JexsOP5mo ago
Okay 🙂 is a string literal , litteraly just something along the lines of "Console.WriteLine("Hello how are you today")
Angius
Angius5mo ago
A string literal is literally just a string So "hello world"
Jexs
JexsOP5mo ago
okay ty 🙂 what does null mean? also are + ,- ,/ , * considered unicode characters
Jexs
JexsOP5mo ago
ty for the link can break be used to exit from a for loop, while loop, a do while loop, a for each loop ? like all of theses?
Angius
Angius5mo ago
From any loop, yes Most characters fit the Unicode range
Angius
Angius5mo ago
And when it comes to null...
No description
Jexs
JexsOP5mo ago
thank you for your reply you've been really help ful! is there a website where you can paste your code and it lines up all the brackets for you automaticallyh i messed up my brackets and it feels like a pain trying to figure out where i should put oned one*
blueberriesiftheywerecats
Yes, imagine if int i = 10 and statement 2 is i < 5, why would it even run? So yes im sure Check format keybindings in your ide
Jexs
JexsOP5mo ago
@blueberriesiftheywerecats "For the loop, starting with int i equal to 0, while i is less than 10, increment i by 1 after each iteration. Inside the loop, print the value of i to the console." @blueberriesiftheywerecats "For the loop, starting with int i equal to 0, while i is less than 10, increment i by 1 after each iteration. Inside the loop, print the value of i to the console." is this the correct way to read this for (int i = 0; i < 10; i++) { Console.WriteLine(i); }
Jexs
JexsOP5mo ago
ty mate 🙂
Angius
Angius5mo ago
No need for a website $prettycode
MODiX
MODiX5mo ago
To format your code in Visual Studio, Visual Studio Code, Rider, use the following shortcut:
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
Visual Studio CTRL + K + D
Rider / Resharper CTRL + ALT + L
Visual Studio Code SHIFT + ALT + F
NOTE: the first key must be held while doing it. https://cdn.discordapp.com/attachments/569261465463160900/899513918567890944/2021-10-18_01-26-35.gif
Anton
Anton5mo ago
for (a; b; c){d}
for (a; b; c){d}
=
a;
while (b)
{
d;
c;
}
a;
while (b)
{
d;
c;
}
Gokul
Gokul4mo ago
so the order is statement1(int i=0) , then statement2( i<5), if statement 2 satisfies code block is executed and then statement 3, if statement 2 result in false , it will directly skip the code block and statement 3.
Want results from more Discord servers?
Add your server