C
C#2y ago
inexQ

❔ The name number does not exist in the current context

I'm new at C#, and i had a problem with this code:
for(int number = 1000; number < 0; number - 7)
Console.WriteLine("{0} - 7 = ", number);
for(int number = 1000; number < 0; number - 7)
Console.WriteLine("{0} - 7 = ", number);
Error - The name "number" does not exist in the current context
16 Replies
Alexicon
Alexicon2y ago
you have a semi colon at the end of your loop statement, you need to remove that. Also
number - 7
number - 7
doesnt work you need to do something like
number = number - 7
number = number - 7
inexQ
inexQOP2y ago
thank you, but now is other error: Only assignment, call, increment, decrement, and new object creation expressions can be used as an operator
Alexicon
Alexicon2y ago
what does you code currently look like now?
inexQ
inexQOP2y ago
I edited my old code on top wait
Alexicon
Alexicon2y ago
Right, like I mentioned in my previous message
number - 7
number - 7
doesn't work you need to do
number = number - 7
number = number - 7
or
number -= 7
number -= 7
inside the loop like this
for(int number = 1000; number < 0; number = number - 7)
{
//...
}
for(int number = 1000; number < 0; number = number - 7)
{
//...
}
inexQ
inexQOP2y ago
first variant doesn't working without any errors second variant is same maybe that:
for(int number = 1000; number < 0; number - 7) {
Console.WriteLine("{0} - 7 = ", number);
{
for(int number = 1000; number < 0; number - 7) {
Console.WriteLine("{0} - 7 = ", number);
{
? no, doesn't working without errors full code looks that:
using System;

namespace Program {

class Program {

static void Main() {
for(int number = 1000; number < 0; number -= 7) {
Console.WriteLine("{0} - 7 = ", number);
}
}
}
}
using System;

namespace Program {

class Program {

static void Main() {
for(int number = 1000; number < 0; number -= 7) {
Console.WriteLine("{0} - 7 = ", number);
}
}
}
}
Alexicon
Alexicon2y ago
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:
for (int number = 1000; number > 0; number = number - 7)
{
//...
}
for (int number = 1000; number > 0; number = number - 7)
{
//...
}
inexQ
inexQOP2y ago
what i need to do now? int number = 1000; number < 0? I don't understand this
Alexicon
Alexicon2y ago
Which part do you not understand? I do not quite follow what your question is now.
inexQ
inexQOP2y ago
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
Alexicon
Alexicon2y ago
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
int number = 1000
int number = 1000
the condition to continue looping is
number < 0
number < 0
and the index modifier is
number = number - 7 //(number -= 7)
number = number - 7 //(number -= 7)
if your familiar with the way a while loop works you can think of this for loop in your mind like the following:
int number = 1000;
while (number < 0)
{
//...

number = number - 7;
}
int number = 1000;
while (number < 0)
{
//...

number = number - 7;
}
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.
inexQ
inexQOP2y ago
Thank you so much
inexQ
inexQOP2y ago
it working, thanks
inexQ
inexQOP2y ago
@Alexicon number = number - 7 doesn't working, without errors my code:
int counter = 1000;
while (counter < 6)
{
Console.WriteLine($"The counter is {counter}");
counter = counter - 7;
}
int counter = 1000;
while (counter < 6)
{
Console.WriteLine($"The counter is {counter}");
counter = counter - 7;
}
on microsoft website writen only about ++ ahh it's --
Alexicon
Alexicon2y ago
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.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server