Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:02:12+00:00 2026-05-28T16:02:12+00:00

I am looking for an easy to way to combine these 2 if statements

  • 0

I am looking for an easy to way to combine these 2 if statements in my Cocos2D application. However my attempts so far have not worked at all.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:02:14+00:00Added an answer on May 28, 2026 at 4:02 pm

    I believe your attempt to combine the if statements into one stems from your problem actually understanding the statement. As a programmer, your first and foremost duty is to write readable code even if you’re the only person ever to read it. The other reason I can think of why you want only one if statement is that somehow you think this will be faster.

    So first I would start by moving all calculations out of the if clauses. Not only is this repetitive, it is also error prone and very hard to read. I’ve also taken the liberty to replace division by two with multiplication by 0.5f because the ARM processors do not have a division instructions, therefore the divisions are executed by software and rewritten as multiplication anyway. It’s possible the compiler already optimizes this but nevertheless good practice to avoid division operations if you can.

    So here’s how I would write the statement. The additional local variables cause no performance penalty. Usually the compiler will do something like that anyway. Similarly, if spriteA.tag is not 1, the condition doesn’t evaluate the rest of the first if condition and jumps right to testing the else if part. This is called “early out”: on the first test which will cause the entire condition to fail the program will stop evaluating the remaining tests because there would be no point in doing so. Therefore it is good practice to begin an if statement (or nested ifs) with the condition that is most often going to fail.

    float spriteAHalfWidth = spriteA.boundingBox.size.height * 0.5f;
    float spriteBHalfWidth = spriteB.boundingBox.size.height * 0.5f;
    
    float spriteALeftSide = spriteA.position.x - spriteAHalfWidth;
    float spriteARightSide = spriteA.position.x + spriteAHalfWidth;
    float spriteBLeftSide = spriteB.position.x - spriteBHalfWidth;
    float spriteBRightSide = spriteB.position.x + spriteBHalfWidth;
    
    if (died == NO)
    {
        if (spriteA.tag == 1 && spriteB.tag == 4 && 
            spriteALeftSide <= spriteBRightSide)
        {
            float y = spriteB.position.y + spriteBHalfWidth +
                      sprite.boundingBox.size.height * 0.5f;
            sprite.position = ccp(pos.x, y);
        } 
        else if (spriteA.tag == 4 && spriteB.tag == 1 && 
                 spriteBLeftSide <= spriteARightSide) 
        {
            float y = spriteA.position.y + spriteAHalfWidth +
                      sprite.boundingBox.size.height * 0.5f;
            sprite.position = ccp(pos.x, y);
        }
    }
    

    Needless to say, you can’t combine this into a single if statement. At the end you perform two different functions (update position based on either spriteA’s or spriteB’s y position). Therefore you require to have two branches.

    If you wanted to, you could add died == NO as the first test in both nested if statements if you really want to avoid the outermost if clause. However doing so will have your program always test for died == NO twice even though the died variable will not change. On the other hand it’s possible that the compiler also recognizes and optimizes this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an easy way to replace all punctuated letters with normal
I have been looking for a easy way to add borders to custom made
I'm looking for an easy way to convert all variables and function names from
I am looking for an easy way to check if two 2D arrays have
I'm looking for easy way to count all rows within one SQL Server 2005/2008
I'm looking for an easy way to combine two arrays. $lang = array('de' =>
I am looking for a easy way to output milliseconds into hours-minutes-seconds. I have
I'm looking for a easy way to include application installation folder to a %PATH%
I'm looking for a easy way to have multiple attachments on one model using
Im looking for an easy way to move around a character and rotate it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.