Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

While Loops C#

Go to solution Solved by minibois,
8 hours ago, Sakuriru said:

This code has two bugs.

  1. The greater than or equal to symbol is ">=" and not "=>".
  2. When input is less than zero it will include it in the total calculation.

If I told聽you I included those errors on purpose, so OP still had to debug some code, would that be believable?
No? Okay.. 馃様

/s

Thanks for the corrections, I just kinda typed that code on the spot and it's clear I rely a bit on Visual Studio showing me a red line when doing stuff like "=>", lol.

How do I break a While loop in C# when I enter a negative integer?

MacBook Air 2020

Link to post
Share on other sites
1 minute ago, RockyRZ said:

How do I break a While loop in C# when I enter a negative integer?

if(number < 0)
{
	break;
}

Of course without having any more information, there could be a million other ways of handling this, but this is the simplest way to directly answer your question,

"We're all in this together, might as well be friends" Tom, Toonami.

Sorry if my post seemed rude, that is never my intention.

"Why do we suffer a lifetime for a moment of happiness?" - Anonymous

Link to post
Share on other sites
9 minutes ago, minibois said:

if(number < 0)
{
	break;
}

Of course without having any more information, there could be a million other ways of handling this, but this is the simplest way to directly answer your question,

So I鈥檓 supposed to break the loop when a negative number is inputted then give out the result of all inputted numbers.

<int value = 0;
聽 聽 聽 聽 int total = 0;
聽 聽 聽 聽聽
聽 聽 聽 聽 int x = 1;
聽 聽 聽 聽 while ( x <= 1){
聽 聽 聽 聽 Console.Write("Input a number: ");
聽 聽 聽 聽 value = Int32.Parse(Console.ReadLine());
聽 聽 聽 聽 total = value + total;
聽 聽 聽 聽 }
聽 聽 聽 聽聽
聽 聽 聽 聽 Console.WriteLine($"{total}");>

MacBook Air 2020

Link to post
Share on other sites

So at every iteration a while loop evaluates some condition. So in the the parenthesis of your while loop you have to put the logic for that condition. So if you did

// Here is where you set your conditions for the loop to be executed. 
while(number >= 0)
{
	// Run some code
    foo();
}

// you can also check if multiple conditions using boolean logic.
// so if you wanted to run the loop while say 2 (or more you can do as many conditions as you like) variables are between 2 values you could do this

// always initialize something to a variable but this is just pseudo code so...
int number;
bool keepGoing = true;

while(number >= 0 && keepGoing)
{
	// run some code
    foo();
    if(bar)
    {
    	keepGoing false;
    }
}

// you can also check if either condition is true and run the while loop as well with || and many others

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95,聽Storage:聽Intel 730 Series 480GB SSD,聽WD 1.5TB Black

Display:聽BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites
1 minute ago, RockyRZ said:

So I鈥檓 supposed to break the loop when a negative number is inputted then give out the result of all inputted numbers.

<int value = 0;
聽 聽 聽 聽 int total = 0;
聽 聽 聽 聽聽
聽 聽 聽 聽 int x = 1;
聽 聽 聽 聽 while ( x <= 1){
聽 聽 聽 聽 Console.Write("Input a number: ");
聽 聽 聽 聽 value = Int32.Parse(Console.ReadLine());
聽 聽 聽 聽 total = value + total;
聽 聽 聽 聽 }
聽 聽 聽 聽聽
聽 聽 聽 聽 Console.WriteLine($"{total}");>

You can make the condition in the while trigger what you want to do.

int input = 0;        
int total = 0;

while(input => 0)
{
        Console.Write("Input a number: ");
        input = Int32.Parse(Console.ReadLine());
        total += input;
}

Console.WriteLine("Total is: " + total);

The while 'tests' if the 'input' variable is 0 or higher. If it's lower than 0 (a negative number), the while will stop and the "total is" line will be written.

If the inputted number is 0 or above (a positive number) it will count up.

This code is missing some sanitization on the input, as if you input anything that is not a number (i.e. a letter, or symbol), the code will not break under the Int32.Parse.

For a proper program, you want to test the input using Int32.TryParse (which tells you聽if聽the input is a number or not) and only then once confirmed a number, you want to actually convert the input to an int.

But if you want/need to implement that depends on if that is needed in the homework assignment.

"We're all in this together, might as well be friends" Tom, Toonami.

Sorry if my post seemed rude, that is never my intention.

"Why do we suffer a lifetime for a moment of happiness?" - Anonymous

Link to post
Share on other sites
1 hour ago, minibois said:

You can make the condition in the while trigger what you want to do.


int input = 0;        
int total = 0;

while(input => 0)
{
        Console.Write("Input a number: ");
        input = Int32.Parse(Console.ReadLine());
        total += input;
}

Console.WriteLine("Total is: " + total);

The while 'tests' if the 'input' variable is 0 or higher. If it's lower than 0 (a negative number), the while will stop and the "total is" line will be written.

If the inputted number is 0 or above (a positive number) it will count up.

This code is missing some sanitization on the input, as if you input anything that is not a number (i.e. a letter, or symbol), the code will not break under the Int32.Parse.

For a proper program, you want to test the input using Int32.TryParse (which tells you聽if聽the input is a number or not) and only then once confirmed a number, you want to actually convert the input to an int.

But if you want/need to implement that depends on if that is needed in the homework assignment.


鈥淎 local variable named `input' cannot be declared in this scope because it would give a different meaning to `input', which is already used in a `parent or current' scope to denote something else
Compilation failed: 1 error(s), 0 warnings鈥

It seems this method didn鈥檛 work.

MacBook Air 2020

Link to post
Share on other sites
3 hours ago, minibois said:

You can make the condition in the while trigger what you want to do.


int input = 0;        
int total = 0;

while(input => 0)
{
        Console.Write("Input a number: ");
        input = Int32.Parse(Console.ReadLine());
        total += input;
}

Console.WriteLine("Total is: " + total);

The while 'tests' if the 'input' variable is 0 or higher. If it's lower than 0 (a negative number), the while will stop and the "total is" line will be written.

If the inputted number is 0 or above (a positive number) it will count up.

This code is missing some sanitization on the input, as if you input anything that is not a number (i.e. a letter, or symbol), the code will not break under the Int32.Parse.

For a proper program, you want to test the input using Int32.TryParse (which tells you聽if聽the input is a number or not) and only then once confirmed a number, you want to actually convert the input to an int.

But if you want/need to implement that depends on if that is needed in the homework assignment.

This code has two bugs.

  1. The greater than or equal to symbol is ">=" and not "=>".
  2. When input is less than zero it will include it in the total calculation.
Link to post
Share on other sites
56 minutes ago, Sakuriru said:

This code has two bugs.

  1. The greater than or equal to symbol is ">=" and not "=>".
  2. When input is less than zero it will include it in the total calculation.

Yeah I was able to find a way around that with:

聽 聽 聽聽

        int input = 0;
聽 聽 聽 聽 int total = 0;
聽 聽 聽 聽聽
聽 聽 聽 聽 int x = 1;
聽 聽 聽 聽 while (x > 0) {
聽 聽 聽 聽 Console.Write("Input a number:聽");
聽 聽 聽 聽 input = Int32.Parse(Console.ReadLine());
聽 聽 聽 聽 if (input < -1)
聽 聽 聽 聽 break;
聽 聽 聽 聽 total += input;
聽 聽 聽 聽 }
聽 聽 聽 聽聽
聽 聽 聽 聽 Console.WriteLine("\nSum total is" + total);聽

MacBook Air 2020

Link to post
Share on other sites
31 minutes ago, RockyRZ said:

Yeah I was able to find a way around that with:

聽 聽 聽聽




        int input = 0;
聽 聽 聽 聽 int total = 0;
聽 聽 聽 聽聽
聽 聽 聽 聽 int x = 1;
聽 聽 聽 聽 while (x > 0) {
聽 聽 聽 聽 Console.Write("Input a number:聽");
聽 聽 聽 聽 input = Int32.Parse(Console.ReadLine());
聽 聽 聽 聽 if (input < -1)
聽 聽 聽 聽 break;
聽 聽 聽 聽 total += input;
聽 聽 聽 聽 }
聽 聽 聽 聽聽
聽 聽 聽 聽 Console.WriteLine("\nSum total is" + total);聽

Are you using Visual Studio? I highly recommend developing C# in Visual Studio. If you're using MacOS then use Visual Studio for Mac.

Improvements that can be made to your code:

  1. All integers are originally initialized to when created in C#, it's unnecessary to set them equal to zero.
  2. "int x = 1" and "while (x > 0)" can be reduced to simply "while (true)", since x will never be set to another value.
  3. "if (input < -1)" doesn't actually include -1, so -1 won't break out of the loop. This line should read "input < 0".
  4. When you print the total, there's no space after "is" in your string, causing the output to be formatted like "Sum total is12345". I recommend using string interpolation to format strings to avoid errors like these. As an interpolated string the line would read 'Console.WriteLine($"\nSum total is {total}");'.
Link to post
Share on other sites
8 hours ago, Sakuriru said:

This code has two bugs.

  1. The greater than or equal to symbol is ">=" and not "=>".
  2. When input is less than zero it will include it in the total calculation.

If I told聽you I included those errors on purpose, so OP still had to debug some code, would that be believable?
No? Okay.. 馃様

/s

Thanks for the corrections, I just kinda typed that code on the spot and it's clear I rely a bit on Visual Studio showing me a red line when doing stuff like "=>", lol.

"We're all in this together, might as well be friends" Tom, Toonami.

Sorry if my post seemed rude, that is never my intention.

"Why do we suffer a lifetime for a moment of happiness?" - Anonymous

Link to post
Share on other sites
On 11/12/2020 at 7:05 PM, Sakuriru said:

"int x = 1" and "while (x > 0)" can be reduced to simply "while (true)", since x will never be set to another value.

This is the only advise I would change, while it's true it overlooks the bigger improvement of using a do while loop.聽 (and just testing for input < 0)

3735928559 - Beware of the dead beef

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