hey anyone,
what is the meaning of

while(!correct) ? when correct = 0,
what is the meaning of it? i have to trace the output but i dont understand it.
please help me (:
thanks alot...

Recommended Answers

All 8 Replies

if() and while() and for() statements all have a test. The answer to that test will be either zero or non-zero.

A zero answer means the test result evaluated to false. A non-zero answer means the test result evaluated to true.

if(0 < 1) {
  printf("\n Zero is less than one");  //statement was true
else
  printf("\n Funky arithmetic going on here!"); statement was false
}

With while() statements, the test is right at the top of the loop - which means the loop may never loop at all, if the test evaluates to false.

while(1 < 0)   //a false statement
  printf("This is impossible!"); //will never get printed

The ! char is the NOT operator (aka negation). Which "flips" the evaluation around.

Take the above, and make sure you understand why they do what they do, in your compiler. Then add the ! in front of them, and see if you understand why it's doing things differently.

Keep in mind that !(true) equals false, and a negative number as the final result, is just as true as a positive number. Only zero evaluates to false.

if() and while() and for() statements all have a test. The answer to that test will be either zero or non-zero.

A zero answer means the test result evaluated to false. A non-zero answer means the test result evaluated to true.

if(0 < 1) {
  printf("\n Zero is less than one");  //statement was true
else
  printf("\n Funky arithmetic going on here!"); statement was false
}

With while() statements, the test is right at the top of the loop - which means the loop may never loop at all, if the test evaluates to false.

while(1 < 0)   //a false statement
  printf("This is impossible!"); //will never get printed

The ! char is the NOT operator (aka negation). Which "flips" the evaluation around.

Take the above, and make sure you understand why they do what they do, in your compiler. Then add the ! in front of them, and see if you understand why it's doing things differently.

Keep in mind that !(true) equals false, and a negative number as the final result, is just as true as a positive number. Only zero evaluates to false.

yeah. i understand the symbol of !
but i just dont understand while(!correct), when set correct = 0,
which mean how to !(0), this what i dont understand only.

So if corect=0 (false), then what does !(false) evaluate to?

Hint: not false, ;)

So if corect=0 (false), then what does !(false) evaluate to?

Hint: not false, ;)

oh... 0.0v
so how does it loop ?
is it like this,
while(!correct) when set correct = 0
keep looping until it get (=correct) which mean correct = 1, since the statement state
(!correct) = 1.
am i right dude? 0.0v

>so how does it loop ?
Not false is true, not true is false. That's how boolean logic works. Perhaps if you study equivalent constructs, things will become more clear. The following two snippets do exactly the same thing:

while (!correct) { ... }
while (correct == 0) { ... }

If correct is 0, the loop will continue. If correct is not 0, the loop stops. Likewise, the following two snippets also do the same thing (the opposite of above):

while (correct) { ... }
while (correct != 0) { ... }

Print out some of these examples, and see if it begins to gel for you. Work with it, a bit. We learn best by doing.

>so how does it loop ?
Not false is true, not true is false. That's how boolean logic works. Perhaps if you study equivalent constructs, things will become more clear. The following two snippets do exactly the same thing:

while (!correct) { ... }
while (correct == 0) { ... }

If correct is 0, the loop will continue. If correct is not 0, the loop stops. Likewise, the following two snippets also do the same thing (the opposite of above):

while (correct) { ... }
while (correct != 0) { ... }

do you have any more easy to understand examples ?
because i still confuese with your example dude. :-/

>do you have any more easy to understand examples ?
Sorry, it doesn't get any easier than that. Now you need to do some work. Come up with hypotheses about how things work and verify that your assumptions are correct with empirical test programs. For example:

#include <stdio.h>

void test(int condition)
{
    if (condition)
        printf("condition (%d): if (condition)\n", condition);
    if (condition != 0)
        printf("condition (%d): if (condition != 0)\n", condition);
    if (!condition)
        printf("condition (%d): if (!condition)\n", condition);
    if (condition == 0)
        printf("condition (%d): if (condition == 0)\n", condition);
}

int main(void)
{
    test(0);
    test(1);
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.