Programming language novice help with C#-For loops

Updated on August 19, 2019 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on August 15, 2019

In https://www.youtube.com/watch?v=eIsGXwisKPE&list=PLPV2KyIb3jR6ZkG8gZwJYSjnXxmfPAl51&index=6

there’s a part I need help with such as printing out numbers each on a separate line- (1-10). https://pastebin.com/Xvk4PANZ. I tried my own code, but it didn’t work so I tried the latter, and it didn’t work either. https://pastebin.com/cg9YdMDm. Please help. 

  • Liked by
Reply
0 on August 15, 2019

Your latter attempt is close, and could technically work, but it’s not following a standard for loop pattern.

You have:

for (int i = 0; i <= 9)
{
     console.WriteLine (I);
     I += 1;
}

 

Which while this could work, there are a few issues.

  1. You have 2 different  variable (i, and I) both of which are completely different variables.
  2. While you can change the index variable inside the loop, typically you just let the for loop handle that.

Try this:

for(int i = 0; i <= 9; i++)
{
     console.WrightLine(i);
}

 

The proper format for a for loop is:

for(index; while something; do this)

“Index” are you variables, yes you can have multiple in 1 for loop, that your going to use in your loop. (or not, you don’t have to use them, I guess)

“While something” is some condition that will cause the loop to keep going until it is false.

“Do this” is what the loop is gonna do every time it starts over. In most cases, you just increase the index.

 

Fun tip, you can do your line print thing with nothing inside the for loop. It might look something like this:

for(int i = 0; i <=9; console.WrightLine(i++)) {}

The only thing to note here, is where the ++ are relative to the i. If it’s ‘i++’ then i is printed to the screen before the ++ happens, while ‘++i’ will increase i before reading it.

  • Liked by
Reply
Cancel
0 on August 16, 2019

Thanks.

  • Liked by
Reply
Cancel
1 on August 18, 2019

https://imgur.com/a/SCI12uJ. Since it isn’t possible to edit my previous post I will have to double post, I just have a question about why it says error when if it works it always pops up a black screen, ex: pick a number to divide like the one with command prompt. 

Wise
on August 19, 2019

There are a few errors there.

  1. You just have a random ‘1’ before the for loop
  2. You have extra {} after the for loop
  3. You don’t have a closing ‘}’ for main

Edit: also, you don’t need the console.WrightLine(i) inside the for loop if you do console.WrightLine(i++) as the increment.

Show more replies
  • Liked by
Reply
Cancel
0 on August 18, 2019
  • Liked by
Reply
Cancel
0 on August 19, 2019

, so far everything worked out, I even went from having a bug error message to just a blank black screen, I understand line 11 (most likely) from the previous tutorial about WrightLine, and definitions, but line 9 I do not understand. Thanks

Edit-Image link did not work, replaced it. 

  • Liked by
Reply
Cancel