Loop Questions In C

30.08.2019
Loop Questions In C 4,2/5 8720 votes

Which of the following statements about the while loop is not true? A.The while loop is a posttest loop. B.Testing condition is made before each iteration. C.The while loop statement must terminate with a semi-colon.

  1. Multiple Choice Questions On Loops In C

I'm trying to make a program that asks the user whether they would like to continue to the next calculation. For some reasons whenever I enter y or Y, the program ends. However, if I use only one condition inside the if statement(without ' 'sign), the code works fine. I just want to make sure that the user can enter both upper and lower case.

What's wrong with the code? Is there a better way to do this?

Horsey Potato
Horsey PotatoHorsey Potato

closed as unclear what you're asking by Bill the Lizard, AndyG, EdChum, user5735775, JALOct 22 '16 at 14:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

The return statement ends a function, in this case, this is the main, so it ends your program, whatever value you're returning.
If you only want to get out of your loop you have two solutions:
Use a boolean:

Use break to exit your loop:

But both these solution will consider any character entered exept N/n as 'continue', if you want to avoid this:

TreycosTreycosLoop
4,6873 gold badges14 silver badges33 bronze badges

For some reasons whenever I enter y or Y, the program ends

The return statement is used to return control(and sometimes an optional value), to a functions caller. When return is used inside the called function, the function is terminated. From cppreference.com:

[return] terminates [the] current function and returns [a] specified value to the caller function.

(emphasis mine)

You may be under the impression that the statement return true inside your while-loop is returning the Boolean value of true to your while condition. It is not.

If your end goal is to create a yes/no style program that ends when the user enters 'No/no', then you can to make use of the continue and break statements, or use a do/while loop.

Multiple Choice Questions On Loops In C

Using continue and break

The continue statement is used to immediately skip to the next iteration of a loop, for or while, terminating the current iteration. From cppreference.com:

Causes the remaining portion of the enclosing for, range-for, while or do-while loop body to be skipped. Used when it is otherwise awkward to ignore the remaining portion of the loop using conditional statements.

(emphasis mine)

The break statement is somewhat similar to continue, but instead of breaking the current iteration and skipping to the next one, it immediately breaks the entire program out of the while loop returning control to the outer-scope. From cppreference.com:

Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate. Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

(emphasis mine)

After examining the information above, you can modify your program to make use of continue and break, instead of return:

Using a do/while loop

Loop

While the method above would work, I recommend using my second option-- a do/while loop. The do/while has the advantage o being shorter, and not having to make use of any kind of major control flow. From cppreference:

Executes a statement repeatedly, until the value of expression becomes false. The test takes place after each iteration.

Peavey classic 50 410 circuit. If needed you could even add error checking to validate a users input:

Output

References and Resources

Christian DeanChristian DeanFor loop c programming examples
16.2k6 gold badges29 silver badges61 bronze badges

I just wrote some code and it works like a charm using 'goto'.The code also included data validation as well. Many thanks to @Treycos.

Output:

For uppercase n:

Horsey PotatoHorsey Potato

Not the answer you're looking for? Browse other questions tagged c++ or ask your own question.

Comments are closed.