Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Combining if statements

Combining if statements

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyclone_S
    wrote on last edited by
    #1

    Hi, I need some help combining these two blocks of if statements. Thanks.

    if(segments_comp[head_comp]->panel->Left <= Food->Left)
    {
    if(segments_comp[head_comp]->panel->Top >= Food->Top){direction_comp =2;}
    else{direction_comp=-1;}
    }
    else if(segments_comp[head_comp]->panel->Left >= Food->Left)
    {
    if(segments_comp[head_comp]->panel->Top >= Food->Top){direction_comp =2;}
    else{direction_comp=1;}

    }
    
    
    if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
    {
     if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;}
     else{direction\_comp=-2;}
     
    }
    else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top)
    {
     if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;}
     else{direction\_comp=2;}
    }
    
    A 1 Reply Last reply
    0
    • C Cyclone_S

      Hi, I need some help combining these two blocks of if statements. Thanks.

      if(segments_comp[head_comp]->panel->Left <= Food->Left)
      {
      if(segments_comp[head_comp]->panel->Top >= Food->Top){direction_comp =2;}
      else{direction_comp=-1;}
      }
      else if(segments_comp[head_comp]->panel->Left >= Food->Left)
      {
      if(segments_comp[head_comp]->panel->Top >= Food->Top){direction_comp =2;}
      else{direction_comp=1;}

      }
      
      
      if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
      {
       if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;}
       else{direction\_comp=-2;}
       
      }
      else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top)
      {
       if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;}
       else{direction\_comp=2;}
      }
      
      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      that code is pretty bad, you essentially overwrite direction_comp in the second set of if/else statements, therefore it doesn't matter what the first one does, so combining them is as easy as just keeping the second block

      if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
      {
      if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left)
      	direction\_comp =-1;
      else
      	direction\_comp=-2;
      }
      else
      {
      if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left)
      	direction\_comp =1;
      else
      	direction\_comp=2;
      }
      
      C 1 Reply Last reply
      0
      • A Albert Holguin

        that code is pretty bad, you essentially overwrite direction_comp in the second set of if/else statements, therefore it doesn't matter what the first one does, so combining them is as easy as just keeping the second block

        if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
        {
        if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left)
        	direction\_comp =-1;
        else
        	direction\_comp=-2;
        }
        else
        {
        if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left)
        	direction\_comp =1;
        else
        	direction\_comp=2;
        }
        
        C Offline
        C Offline
        Cyclone_S
        wrote on last edited by
        #3

        Thanks. That code works better.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups