0

my code sniplet

I want to prompt user for range to print something but if it user puts negative or more 8 to reprompt.

1 Answer 1

1

Think negatively!

Look at the test that the code is doing:

while(n <= 8 && n >= 1 )

This checks that n is in the range that is valid. You want the loop to repeat if it isn't in the range. So, you need a test for the opposite.

Two ways to do that. Either negate the test :

while( !(n <= 8 && n >= 1) )    

or check for the opposite:

while(n > 8 || n < 1 )

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • thank you so much it worked . what does !() exclamation next to private mean . thanks for your time Commented Oct 7, 2022 at 14:30
  • The "!" exclamation is the logical NOT operator. Google it.
    – Cliff B
    Commented Oct 7, 2022 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .