Using a While Loop to Control Program Termination in C

In summary, a while loop is used to ask the user if they want to execute the program again or terminate it. In this example, the user_choice variable is set to 1 if they want to execute the program again, and to 2 if they want to terminate it.
  • #1
Nothing000
403
0
How would one use a while loop in the C programmin language to ask the user if they want to terminate the program, or execute it again. You could say press 1 to terminate and press 2 to execute again. How would someone set that up with a while loop?
 
Computer science news on Phys.org
  • #2
That's not what a while loop does; a while loop is merely one of the components you will use in your program.

If you write down, step by step, exactly how you want your program to behave, it will hopefully be clear how to write it!
 
  • #3
I want to say:
while variable = 2
execute program

and I will ask the user to press 1 to use the program again, or press to to terminate the program.
 
  • #4
are you home hurkyl?
 
  • #5
user_choice = 1
while (user_choice == 1)
{
program code;
ask "what do you want to do (1 for repeat 2 for exit)";
scanf into user_choice;
}
 
  • #6
That is exactly what I am doing and I am getting caugt in an infinite loop.
 
  • #7
when I run the program it goes through it the first time fine, then it asks enter 1 to repeat or 2 to exit. When I enter 1 it goes into an infinite loop.
 
  • #8
make sure you used &user_choice with the scanf argument, if you just put user_choice scanf will put 2 at the memory location 1...

you can printf user_choice and see if it got the right value just before the loop ends, so you'd be sure its 2...
or put your code here so we can see what's wrong...by the way... it's just style, but i suggest using boolean for that kind of thing:
Code:
run=1;
while(run) {
     ...
     scanf("%d",&user_choice);
     if (user_choice==2) run=0;
}
it's just nicer.

i once was an efficiancy freak, id use one integer to store 16 boolenas...
 
Last edited:
  • #9
Nothing000 said:
I want to say:
while variable = 2
execute program

It should be:

while variable == 2.

You want == not = here, else it loops forever.
 
  • #10
I have the == sign there, I just wrote it wrong in this thread. Sorry. I will post the souce code soon so you guys can look it over to see if you can help me.
 
  • #11
help!

How will i make an output of all even numbers from 2-18 using a while loop structure?:confused:
 
  • #12
note that you must differ between 1 and '1'. one is a int the other is a char. This is important in the type of data you read from your scanf.

You could also look into the "break;" keyword. and figure out how to do an infinite loop with just while(?) {}. for instance while(true) {} is a C++ option.
 

FAQ: Using a While Loop to Control Program Termination in C

What is a while loop in C?

A while loop is a control structure in the C programming language that allows a block of code to be executed repeatedly as long as a certain condition is met. The code will continue to loop until the condition is no longer true.

How do you use a while loop to control program termination in C?

To use a while loop to control program termination in C, you need to specify a condition that will terminate the loop. This condition should eventually become false, otherwise the loop will continue to run indefinitely. Once the condition becomes false, the loop will terminate and the program will continue to execute the code outside of the loop.

What happens if the condition in a while loop is never met?

If the condition in a while loop is never met, the loop will continue to run indefinitely. This is known as an infinite loop and can cause the program to become unresponsive or crash. It is important to ensure that the condition in a while loop will eventually become false to prevent an infinite loop.

Can you use a while loop to control program termination in any programming language?

While loops are a commonly used control structure in many programming languages, including C. However, the syntax and specific usage may differ slightly in each language. It is important to familiarize yourself with the specific syntax and rules for using a while loop in the programming language you are working with.

Are there any best practices for using a while loop to control program termination?

When using a while loop to control program termination, it is important to ensure that the condition is clear and easily understandable. Additionally, it is recommended to include a counter variable to prevent an infinite loop in case the condition is never met. It is also good practice to test and debug your code to ensure the loop is functioning as intended.

Similar threads

Replies
2
Views
1K
Replies
9
Views
2K
Replies
2
Views
1K
Replies
10
Views
2K
Replies
10
Views
2K
Replies
16
Views
3K
Back
Top