C++ infinite loop

前端 未结 3 1413
陌清茗
陌清茗 2021-01-17 02:00

I am attempting to write a loop that will repeat until the user enters one of the correct choices (either 1 or 0). For some reason when I have the loop written as below it

相关标签:
3条回答
  • 2021-01-17 02:26

    The only way to break out

    while ((control != 0 )|| (control != 1))
    

    is

    !(control != 0) && !(control != 1)
    

    which is equivalent to

    control == 0 && control == 1
    

    which is impossible for all integers.

    0 讨论(0)
  • 2021-01-17 02:29
    (control != 0) || (control != 1)
    

    is equivalent to,

    !(control == 0 && control == 1)
    

    but,

    (control == 0 && control == 1) 
    

    is always false (there is no such number).

    Therefore, the whole expression will always get true value.

    0 讨论(0)
  • 2021-01-17 02:48

    You have to use && operator.

    while ((control != 0 ) && (control != 1))
    
    0 讨论(0)
提交回复
热议问题