1

I made a program that asks for input and returns a value. After, I want to ask if the user wants to continue. But I don't know what to use.

7 Answers 7

3

You want to use a do / while loop, or an infinite while loop with a conditional break.

Sign up to request clarification or add additional context in comments.

Comments

2

A do-while loop is often used:

bool @continue = false;
do
{
    //get value
    @continue = //ask user if they want to continue
}while(@continue);

The loop will be executed once before the loop condition is evaluated.

Comments

2

This only allows 2 keys (Y and N):

ConsoleKeyInfo keyInfo;
do {
    // do you work here
    Console.WriteLine("Press Y to continue, N to abort");

    do {
        keyInfo = Console.ReadKey();
    } while (keyInfo.Key != ConsoleKey.N || keyInfo.Key != ConsoleKey.Y);
} while (keyInfo.Key != ConsoleKey.N);

Comments

1

I would use a do..while loop:

bool shouldContinue;
do {
    // get input
    // do operation
    // ask user to continue
    if ( Console.ReadLine() == "y" ) {
        shouldContinue = true;
    }
} while (shouldContinue);

1 Comment

This one was the easiest to understand. Thanks.
0

You probably want a while loop here, something like:

bool doMore= true;

while(doMore) {
  //Do work
  //Prompt user, if they refuse, doMore=false;
}

Comments

0

use Do While Loop. something similar will work

int input=0;
do
{  
  System.Console.WriteLine(Calculate(input)); 
  input =  GetUserInput();
} while (input != null)

Comments

0

Technically speaking, any loop will do it, a for loop for example (which is another way to write a while(true){;} )

    for (; true; )
    {
        //Do stuff
        if (Console.ReadLine() == "quit")
            {
            break;
        }
        Console.WriteLine("I am doing stuff");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.