Thread: If statement (easy, but I forgot)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    If statement (easy, but I forgot)

    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
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    var evaluates to true for every nonzero value, zero is false
    Kurt

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by jimblumberg View Post
    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
    Exploratory programming has its place, but it's rather a dangerous technique with C. A lot of C constructs are "undefined behaviour", which often means "it seems to work as you would hope, until such time as it doesn't".
    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


  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Exploratory programming has its place, but it's rather a dangerous technique with C.
    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.

    Jim

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    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...
    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.
    Anyone feel free to correct me.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    24

    Code:
    if(dog == cats)
    {
       //code here
    }
    A few more comparison operators exist. GL.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jwroblewski44 View Post
    Anyone feel free to correct me.
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy if statement
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2003, 01:54 AM
  2. Forgot the attachment!
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2002, 03:50 PM
  3. forgot how to rewind in C++
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 07:55 PM
  4. The pc that time forgot....
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2001, 06:37 PM