how to end a while loop with a certain variable

后端 未结 4 566
谎友^
谎友^ 2021-01-17 02:28

I am making an odd or even program with a while loop. I am trying to figure out how to end the while loop with a certain number. Right now I have 1 to continue the loop, and

4条回答
  •  孤街浪徒
    2021-01-17 03:19

    You should try to use "a real termination condition" in order to terminate a while loop (or any loop for that matter); it's cleaner and should be easier to understand by everyone else.

    In your case, I think it's better to have a do-while loop with some condition around this logic: num % 2 == 0, and an inner while loop for handling user input/validation.

    If you still want to break loops abruptly, have a look here.

    If you still need some help with the code, hit me up and I'll sketch up something.

提交回复
热议问题