Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
class Program
{
    static void Main(string[] args)
    {
        double N;
        Console.WriteLine("Введите N: ");
        N=Convert.ToInt64(Console.ReadLine());
        int i = -1;
        while (i < N)
        {
            i = i+1;
            Console.WriteLine(i);
        }
        Console.WriteLine(i); // I need the value from the while loop to be output here.
    }   
}



Help to make it so that after the While loop, the value obtained directly in it is output. Thank you in advance!

What I have tried:

I tried assigning a value before the loop.
Posted
Updated 14-Oct-21 15:28pm
v2
Comments
Patrice T 14-Oct-21 13:43pm    
What is the problem ?
What is the input ?
What is the output ?
What did you expect ?
Member 15394448 14-Oct-21 13:55pm    
I expected the value of i to take the value of i from the While loop. And in the end it takes the value N.
Member 15329613 14-Oct-21 13:59pm    
That is because when the loop is over i and N have the same value because you keep incrementing i.
Member 15394448 14-Oct-21 14:02pm    
You don't know how to fix it?
Member 15329613 14-Oct-21 14:30pm    
It isn't broken. It is doing exactly what you told it to. What is the problem?

Quote:
I expected the value of i to take the value of i from the While loop. And in the end it takes the value N.

Then your code is doing exactly what it is supposed to do.
At the end of while loop, i contain the value making it go out of loop, and this value is N.
Break the debugger to see how the code execute.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

By the way, N should be an integer.
 
Share this answer
 
v2
Comments
CPallini 15-Oct-21 2:30am    
5.
Patrice T 15-Oct-21 3:54am    
Thank you
As mentioned in the comments, the value of i is being output. You change the value of i until it equals the value in N.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900