Thread: nested IFs? works lijke this i want know if i understanded correctly.

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    46

    Question nested IFs? works lijke this i want know if i understanded correctly.

    This works:
    Code:
    if (x==0)
    {
        if (Function()==ERROR)
        {
        /* this happens */ 
        }
    }
    this way:
    if x is 0 and Function() retuns value which has same numercal value what ERROR has then program executes line where there reads */ this happens */? but if only x is 0 but Function() return another value this thing dont happen.
    nested ifs fors are yet thing what i dont fully understand maybe but if this goes this way i now understand.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not sure if you're specifically asking a question, but yes, your understanding of nested "if"s seems to be correct.

    Let's expand your example a bit:

    Code:
    if (x==0)
    {
        if (Function()==ERROR)
        {
            /* this happens */
        }
        else
        {
            printf("Function has performed successfully.\n");
        }
    }
    So...

    If 'x' is zero...
    - if the function returns a code called ERROR, the "this happens" part is executed
    - else (if the function does not return an error), a "success" message is printed

    If 'x' is NOT zero, the function is not even called since the inner code is not executed.
    Last edited by Matticus; 12-20-2014 at 07:24 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you want to test your understanding a little more, explain what similarities and differences of behaviour there are between your example and this one.
    Code:
    if (x == 0 && Function() == ERROR)
    {
         /* this happens */
    }
    While this example doesn't involve nested loops, there is a specific relationship between your example and this, so it is relevant to understand/explain how the expression "x == 0 && Function() == ERROR" is evaluated.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    46
    Quote Originally Posted by grumpy View Post
    If you want to test your understanding a little more, explain what similarities and differences of behaviour there are between your example and this one.
    Code:
    if (x == 0 && Function() == ERROR)
    {
         /* this happens */
    }
    While this example doesn't involve nested loops, there is a specific relationship between your example and this, so it is relevant to understand/explain how the expression "x == 0 && Function() == ERROR" is evaluated.
    I talked "nested IFs" (maybe incorrect term) not Loops which i bet goes similar way and i know return returns only innerloop when i use in nested loop it. I dont yet get how your code work differently expect if i use return i think jumps on step back to outer if but if have three ifs same what goes only exits only pervisions if level not first. If understand correctly. i know && is LOGICAL AND which means both things are true then is this speffic part code is executed but if both or one is false code is not executed,and || is LOGICAL OR where only one thing must be true not both or they are both false. at least i understanded it this way.
    My way is better becouse real world thing going check more than single "Function()" return codes if x=0.i dont actually give function a and variable names becouset is unrelated when i know how they work. even ERROR is only this example. i used this becouse these "named return values" have much longer names.Function() reealworld name in this situation ehich i try solve is shorter but Easier type wrong and i dont want tell which OS family i use.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Vanhapolle View Post
    I talked "nested IFs" (maybe incorrect term) not Loops
    Yeah, okay. I committed a typo. I used the word "loops" instead of "ifs".

    Quote Originally Posted by Vanhapolle View Post
    i know && is LOGICAL AND which means both things are true then is this speffic part code is executed but if both or one is false code is not executed,and || is LOGICAL OR where only one thing must be true not both or they are both false. at least i understanded it this way.
    There's more to it than that. Look up "short circuit evaluation". It is a property of the binary logical operators (&& and ||) that affects whether some parts of logical expressions are evaluated.

    Quote Originally Posted by Vanhapolle View Post
    My way is better becouse real world thing going check more than single "Function()" return codes if x=0.
    Maybe, maybe not.

    There are many different ways of achieving what you seek to do. There is rarely a universal better though .... there are simply different sets of advantages and disadvantages.

    By your description, you have demonstrated you only partially understand the logical operators, and the purpose of my suggestion was to encourage you to build a more complete understanding. With a more complete understanding, you might draw a different conclusion.

    But with a partial understanding of one alternative, you can hardly claim to be qualified to judge if it is better or worse than your chosen solution. Claiming you have more information, that you are choosing not to provide, does not really change that.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    46
    this exact thing in this operating system one functions gives when it fails NULL then another function called IoErr gives number which tells what goed wrong. All other numbers in first thing are pointers for example data if all goed well.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Vanhapolle View Post
    this exact thing in this operating system one functions gives when it fails NULL then another function called IoErr gives number which tells what goed wrong. All other numbers in first thing are pointers for example data if all goed well.
    Although you presumably think otherwise, nothing that you describe here invalidates what I've written in this thread.

    I suggest, as previously, that you look up "short circuit evaluation" (in the context of C expressions).
    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. Determining correctly nested brackets
    By Nyah Check in forum C Programming
    Replies: 4
    Last Post: 02-29-2012, 03:44 PM
  2. man -k (how to use correctly)
    By cus in forum Linux Programming
    Replies: 2
    Last Post: 01-14-2009, 11:00 AM
  3. Is this used correctly?
    By MuzicMedia in forum C++ Programming
    Replies: 7
    Last Post: 03-08-2008, 10:07 PM
  4. Using cin correctly
    By kwikness in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2006, 01:39 PM
  5. Did I do this correctly?
    By phosphorousgobu in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 09:41 PM