0

i wrote the following code in orde to get familiar with the while loop However , instead of executing the loop when i give a number lower than 1 it does the exact opposite !it executes the loops and prints "you gave a negative number " when i input a number higher than 1. Can someone explain me why is this happening . Below is the code

    int main()
{
 int x =0;
        printf("Enter a postive number : ");
        scanf("%d" ,&x);
        printf("%d", &x);
         while (x < 1);
         {
             printf("you gave a negative number please try  again: " );
         }
}
2
  • try deleting the semicolon for the while () part
    – BooFluff
    May 25 '15 at 19:25
  • It looks like you are confused between loops and conditions, you use loop when you want execute a set of code multiple times while a condition should be used when you of course want to check something , here you should try using if else condition I.e if x is less than zero execute the following code else don't do anything or execute some other code.
    – cantyousee
    May 25 '15 at 19:38
1

You have a couple of issues in this code that are mostly about structure.

First, the semicolon after the while condition serves as a statement terminator and basically means "While this condition is true, do nothing until it is false and then continue to the next line of code." In fact, if you entered a negative number, it should just sit there forever, looking like the computer is locked up.

Because of the semicolon, the curly brace pair serve only to define or delineate the next block of code. That block of code is going to get executed every time, no matter what value x has. (Well, sort of. In this code, if x < 1, it will never get past the infinite loop. If x >= 1, the print statement will execute once and the program continues to the end.)

If you remove the semicolon after the where condition, then the curly brace pair defines the code to be executed only when the test condition is true.

Second, (once you remove the semicolon) the way it is structured, your code will merely execute the print statement when x < 1, or not print anything when x >= 1. It won't go back and prompt for input again. It will just continue to the next statement in the program. You need to restructure the code to include the input request code inside the while loop. You also need to make sure that the loop executes at least once by using a do-while loop instead of just a while loop.

Here's a pseudocode example that might help you understand.

int main()
{
    int x =0;
    do 
    {   
           // prompt for and get input
           // test for valid input
           // if the input isn't right, print a comment to try again
           // the while test will cause the code to repeat if input isn't right.

    } while ( test for valid input );  // end of do-while loop.
}

If this answers your question, please accept the answer and mark the question as answered. Let's keep up on forum maintenance. ;-)

1
  • Thank you all for your answers and especially you Cliff. Your answer was very decriptive and i think i understand now what i did wrong.
    – VAPORAS
    May 26 '15 at 8:06

You must log in to answer this question.

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