Thread: Combining if statements

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Combining if statements

    Hi everyone,

    Just a quick question, i have a section in my code where there are quite a few (and still growing) if statements inside each other and was wondering how to combine them all?

    Not sure how to go about it. I guess its not really necessary but as i'm teaching myself it would be nice to know if there was a better way.

    Some sample code for you to laugh at

    Code:
    if(seterrorcheck[x][y] ! = seterrorcheck[z][t])
    {
    	if(possarray[x][y] != 0)
    	{
    		if(smallsquarefinal[x][y][candidate_check] != 0)
    		{
    			if(smallsquarefinal[x][y][candidate_check] == smallsquarefinal[x][y][t])
    			{
    
    			Some Arbitrary Code
    
    			}
    		}
    	}
    }
    Thanks for your time and suggestions.
    My program is coming along nicely.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use logical and, i.e., the && operator. You could read the tutorial on If statements as it talks a little on that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "combining" if-statements like that can be done with "and". Something like this:

    Code:
    if(seterrorcheck[x][y] ! = seterrorcheck[z][t] && possarray[x][y] != 0)
    {
    	if(smallsquarefinal[x][y][candidate_check] != 0 && 
               smallsquarefinal[x][y][candidate_check] == smallsquarefinal[x][y][t])
    
    		Some Arbitrary Code
    
    	}
    }
    You can of course do all in one if-statement:
    Code:
    if(seterrorcheck[x][y] ! = seterrorcheck[z][t] && 
       possarray[x][y] != 0 &&
       smallsquarefinal[x][y][candidate_check] != 0 && 
       smallsquarefinal[x][y][candidate_check] == smallsquarefinal[x][y][t])
    {
    	Some Arbitrary Code
    
    }
    However, it makes little difference to the final code generated. An "and" conditional will terminate if the partial condition turns false, and (consequently) continue as long as the partial condition is true. The compiler will still generate a sequence of conditional code that first tests seterrorcheck[x][y] ! = seterrorcheck[z][t], if that's false, jump past the if-block. Then a test of possarray[x][y] is zero. If it is, jump pas the if-block.
    Then test if smallsquare ... is zero, if so, jump.
    Finally, if smallsquare... is not equal to the other smallsquare..., then jump.

    There are situations where you may want to combine if-statements to avoid complications elsewhere, for example if there's an "else" on each of all of your if-statements, and that else is doing exactly the same thing for all of the conditions, then you'd obviously not want to repeat that code 4 times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah, cool,
    So in the above code:

    Code:
    if(
    	(seterrorcheck[x][y] ! = seterrorcheck[z][t])
    	&&
    	(possarray[x][y] != 0)
    	&&
    	(smallsquarefinal[x][y][candidate_check] != 0)
    	&&
    	(smallsquarefinal[x][y][candidate_check] == smallsquarefinal[x][y][t])
    )
    {
    
    }
    Or can i simplify it a bit more like this?:
    Code:
    if(
    	(seterrorcheck[x][y] ! = seterrorcheck[z][t])
    	&&
    	(possarray[x][y] && smallsquarefinal[x][y][candidate_check] != 0) //combined the 2 '!= 0' statements
    	&&
    	(smallsquarefinal[x][y][candidate_check] == smallsquarefinal[x][y][t])
    )
    {
    
    }
    Thanks for the reply laserlight

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    possarray[x][y] && smallsquarefinal[x][y][candidate_check] != 0
    tests the condition:
    Code:
    (possarray[x][y]) && (smallsquarefinal[x][y][candidate_check] != 0)
    which isn't the same thing...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    So Matsp, its basically the same thing as far as the compiler is concerned?

    Good point about the else statements. Didn't think of that

    Also just a little thing, as i'm teaching myself (with a couple of books)
    i don't know anyone who can program. Would it be rude to post my finished program, for people to maybe have a look at for suggestions on how to improve it? The program is pretty big. But i don't have a lecturer or teacher who can look at it and say... " you wrote the entire thing in baby language... here's how to do it better!!!"

    Thanks, Simon.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    man i'm slow at typing! everyone is beating me with replys!! haha, ok noted.
    Thanks!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, reviewing hundreds or thousands lines of code is a pretty large undertaking.

    [And no matter how fast you type, there's someone else typing/reading/browsing faster...]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Yeah i guess so! Its already at 600 lines!
    I don't think i would read 600 lines of code if someone sent it to me!!

    Its probably gonna get much bigger too! I'm really enjoying learning though. Thanks for your help and encouragement.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a small piece of code that you think "I wonder if this can be done better", you may want to post that.

    Ideally, it should be a "complet" piece of code, so that it can be run and verified for any changes [e.g. if I'd suggest you do something a different way, I'd like to be able to test that it actually works first].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Yeah, i totally agree that it would be too much to ask to say heres my code.. mark it!

    i think my problem is that the task that i set myself has a lot of conceptual things that were difficult to get my head round before i even started writing the code. Its only in places where i think that something is very cumbersome that i wonder if it can be done better quicker etc!

    A good example is that i have lots of code where there are loads of loops nested inside each other and i'm not sure if its commonplace?
    Is it normal in code to have 11 or more levels of loops nested inside each other?

    My problem is that i'm regularly thinking in euclidean spacetime notation (4 dimensions) and then i have several of these coordinate systems running inside each other and against limits. So i end up with 8 or nine non decimal dimensions.

    I'm used to this in my job (Electronic Engineer) but translating it to code is difficult for a noob. So it would definately be too much to ask for help with the conceptual stuff and its difficult to seperate the code from the concept!

    Sorry about the long post and lack of sense.
    Regards, Simon.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Having many loops inside each other is not something I particularly like to do, but sometimes that's the only way to do it.

    If you do that a lot, consider if there is a way to create a function that does the inner sets of some of the loops, or some such - particularly if the inner part is common across multiple places.

    Or if there's a way to have one set of "iterate over these arrays" and then call some function - in which case you could use a function pointer to perform the "inner part" and just have one (or a few) set(s) of many nested loops.

    But it's hard to say without understanding the whole code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. having problems with my if statements
    By bajanstar in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 01:39 PM