-   Programming (
-   -   A Small doubt about IF loop ??? (

indian 02-26-2007 08:36 AM

A Small doubt about IF loop ???
I am running this code and the output is coming Hello. I checked with all possible combinations and it prints Hello unless all the variables becomes 0. Boat int and char. Now I am not able to understand what is going on in IF loop ? What is the standard C syntax of it ?


      int a= 0;int b = 20;char x =1;char y =10;

tuxdev 02-26-2007 08:55 AM

If you don't have K&R, go get it. It's got a nice explanition of the ',' operator.

Essentially, the ',' operator just evaluates both sides and returns the right side. It's rarely used.

matthewg42 02-26-2007 10:28 AM

I think you want the logical OR operator, ||

sundialsvcs 02-26-2007 07:24 PM

Wow. Talk about deja vu. :)

My very first program was written in BASIC, ##CLASSIFIED## years ago, and it was eight lines long and took six weeks to write and it had two bugs in it. One of them was this one...

The computer has "accepted" the program, because it "can be compiled," but the program as-written probably does not mean to the computer what you intended it to mean.

Let's look at your program as written and see what it says to the computer.


int a= 0;int b = 20;char x =1;char y =10;
You are declaring four variables here. Two of them are (probably) four bytes long; the other two are one byte long. All of the values will fit in the space provided. So the statement is "legal." But, what does it mean (to you)?


if(a,b,x,y) printf("hello");
This is the nonsensical statement, although no doubt you didn't intend it to be so. An if statement needs to act upon an expression that is either True or False. "True" is nonzero; "False" is zero.

You have used an operator that is legal, but not appropriate to the present situation. It will evaluate, and throw away, the values of 'a', 'b', and 'x', then return the value of 'y', which as we see is '10'. This value is non-zero, so the expression is "True." The "printf()" statement will execute.

But still... what you've asked for, while legal, is nonsensical.

All times are GMT -5. The time now is 06:14 PM.