❔ The name number does not exist in the current context
I'm new at C#, and i had a problem with this code:
Error - The name "number" does not exist in the current context
16 Replies
you have a semi colon at the end of your loop statement, you need to remove that.
Also
doesnt work you need to do something like
thank you, but now is other error: Only assignment, call, increment, decrement, and new object creation expressions can be used as an operator
what does you code currently look like now?
I edited my old code on top
wait
Right, like I mentioned in my previous message
doesn't work you need to do
or
inside the loop like this
first variant doesn't working without any errors
second variant is same
maybe that:
?
no, doesn't working
without errors
full code looks that:
The problem now is just your logic. This loop wont do anything since your "number" starts a 1000 but your only looping when "number < 0" which can never be true.
I think what your trying to do is something like:
what i need to do now? int number = 1000; number < 0?
I don't understand this
Which part do you not understand? I do not quite follow what your question is now.
I don't understand how does for() cycle working, what means those arguments: "number = 1000", "number > 0"
I want to make a loop that subtracts 7 from 1000 until the result is less than zero
Well I would reccomend reading about loops here if your new: https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/branches-and-loops-local
specifically the section titled: Work with the for loop
however breifly in my own words,
A for loop has three components, the index variable, the condition to continue looping and the index modifier.
in your example the index variable is
the condition to continue looping is
and the index modifier is
if your familiar with the way a while loop works you can think of this for loop in your mind like the following:
in this while loop example maybe you can see the issue, you are saying "while number is less than zero" keep looping, but because you start the number at 1000 it will never be less than zero at the start. thats why what I think you want is the opposite, "while number is greater than zero".
Branches and loops - Introduction to C# tutorial
In this tutorial about branches and loops, you write C# code to explore the language syntax that supports conditional branches and loops to execute statements repeatedly.
Thank you so much
it working, thanks
@Alexicon number = number - 7 doesn't working, without errors
my code:
on microsoft website writen only about ++
ahh it's --
Yeah the "for iterator" can be basically any form of number modification on the variable. You don't have to only use ++ (+ 1) or -- (- 1) you can do anything like your original example (- 7). However ++ is probably the most common since often you will be working with arrays and or lists in loops where you want to access each item at an index.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.