Sunday, July 12, 2009

C Programming?

I am trying to use a loop and I need to set two conditions that it will stop. So I need to know how to use and or comparitor and how to use not (like a is not equal to 2). Thanks.

C Programming?
I think you better go for a while loop with two conditions





something like





while( (a==2)%26amp;%26amp;(b==3))


{


// Condition code block


}
Reply:Just use the %26amp;%26amp; (AND) operator. This allows you to string as many conditions as you like together; all of them must be met for the expression to be evaluated as true.





For example:





for ( n=0; a!=b %26amp;%26amp; b!=c; n++)


printf("%d\n", n);





Or,





if (a!=b %26amp;%26amp; c!=d)


n = 1;





Btw, the || (OR) operator works the same way, except only one of the given condition needs to be met for the expression to be true.





Don't confuse these with the bitwise %26amp; and | versions; these do something else!
Reply:while( 1) {


if( a != 2 ) break; // Condition 1


if( a != 3 ) break; // Condition 2


}
Reply:in the loop use if statement,


it would be like that:





For (i=0;i%26lt;n;i++) {


if (a!=i) // a is not equal to 2


{


// some commands here if it is equal


}


else


{


// some commands here if it is not equal


}


}











hope it helped a bit


No comments:

Post a Comment