So, I'm having a really dumb moment and I cant seem to remember how an if statement works, with only the variable name as the condition..
Code:if(var){ //then do something }
So, I'm having a really dumb moment and I cant seem to remember how an if statement works, with only the variable name as the condition..
Code:if(var){ //then do something }
var evaluates to true for every nonzero value, zero is false
Kurt
So I suggest you experiment. Create a program and implement your variable and the if statement, compile and run the program. Determine if the program is doing what you expected. If that doesn't help then I suggest you find a good book or tutorial, if you can't figure out this you probably won't be able to understand most of the rest of the language.
Jim
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean
Maybe with advanced topics, but I totally disagree when talking about such a basic issue. But the best option is to obtain a good book, read it, and enter and run all the sample programs.Exploratory programming has its place, but it's rather a dangerous technique with C.
Jim
It is as this: Every expression, ( a = b + c ) , results or returns the lvalue of the expression. In this case, it would result to a.
So...
Anyone feel free to correct me.Code:if( a ) // a is not an expression, so it is evaluated as is. If a is a non-zero value, it results in a true condition if( ( a = b + c ) ) // here we have an expression. The expression is evaluated; b is added to c and assigned to a. Then the lvalue, a, is examined // to determine if it's a non-zero value. If it is a zero, it would be a false condition and the statement(s) inside the if statement won't be executed.
"Simplicity is the ultimate sophistication." - Leonardo da Vinci
A few more comparison operators exist. GL.Code:if(dog == cats) { //code here }
Consider yourself corrected.
First I'll quote the definition of an expression from the 1999 ISO C standard, Section 6.5, para 1. "An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object of a function, or that generates side effects, or that performs a combination thereof."
I haven't checked, but doubt the definition in C11 would differ substantially.
From this, in "if (a)", a is an expression, in the sense of being a sequence (of one operand and no operators) that retrieves the value of a. Retrieving the value of a variable is an example of a computation of a value.