Jump to content

For loop until a specific value (C#)

Shammikit
Go to solution Solved by RhinoMcD,

I haven't really lived in the C# world all that much, but i'm gonna take a guess that your first two only print '0'

EDIT:  @Sauron clarified:

Quote

The first two will actually never even run because it sets i to 10, then checks if i is equal to 0, notices it is not (we just set it to 10), and does not enter the loop.


You're probably looking for this:

 for (int i = 10; i >= 0; i--)

which will take all values of i that are greater than or equal to 0

this should be something simple but i havent managed to find anything about it on the internet. maybe i searched it wrong. idk, anyway this is what i have tried.

  

//i want to show value of i from 10 to 0. i have tried both methods below but nothing happens
 for (int i = 10; i == 0; i--)
 {
MessageBox.Show(i.ToString());
 }

 for (int i = 10; i.Equals(0); i--)
 {
MessageBox.Show(i.ToString());
 }


//normal loops like this work
 for (int i = 0; i < 10; i++)
 {
MessageBox.Show(i.ToString());
 }

 

Link to comment
Share on other sites

Link to post
Share on other sites

for (int i = 10; i >= 0; i--) {
    MessageBox.Show(i.ToString());
}

The second item in a for loop represents the "conditional". The loop will execute as long as the conditional evaluates to true.

Link to comment
Share on other sites

Link to post
Share on other sites

the second value in the for loop is the condition for which it will continue to run. If you set i to 10 and then set the condition for the loop to be i == 0 it will never run. You need to use i >= 0, so it will go on until i is 0.

 

I'm not sure why you wrote the last loop correctly but tried to use equality checks in the other two.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't really lived in the C# world all that much, but i'm gonna take a guess that your first two only print '0'

EDIT:  @Sauron clarified:

Quote

The first two will actually never even run because it sets i to 10, then checks if i is equal to 0, notices it is not (we just set it to 10), and does not enter the loop.


You're probably looking for this:

 for (int i = 10; i >= 0; i--)

which will take all values of i that are greater than or equal to 0

Link to comment
Share on other sites

Link to post
Share on other sites

You seem to not understand how to make a loop work. In the case of a for-loop, the three fields are:

  • Initial conditions
  • A conditional that keeps the loop running as long as it's true
  • Post loop action

The first two loops fail because the condition is not true at the start given the initial conditions. i = 10 will make i == 0 or some other expression to equate i to 0 false.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, RhinoMcD said:

I haven't really lived in the C# world all that much, but i'm gonna take a guess that your first two only print '0'

You're probably looking for this:


 for (int i = 10; i >= 0; i--)

which will take all values of i that are greater than or equal to 0

The first two will actually never even run because it sets i to 10, then checks if i is equal to 0, notices it is not (we just set it to 10), and does not enter the loop.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks everyone.

3 minutes ago, Sauron said:

I'm not sure why you wrote the last loop correctly but tried to use equality checks in the other two.

silly mistake by me.thanks for pointing it out

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×