Else if statement

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Jeroen
Posts: 2
Joined: July 13th, 2018, 11:08 pm

Else if statement

Post by Jeroen » July 13th, 2018, 11:15 pm

Hi this is my first time posting on here so I hope it al works.

I was watching the beginner tutorial series for c++ and the "if" statement was being explained. The "else if" statement was also being used. Now I know how it works and what is does but I don't see the point in using a else if statement. Can't you just use a seperate if statement.

example:

instead of doing
if()
{}
else if()
{}

just doing
if()
{}
if()
{}

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Else if statement

Post by MrGodin » July 14th, 2018, 5:32 am

Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Else if statement

Post by albinopapa » July 14th, 2018, 5:33 am

Code: Select all

#include <iostream>

int main( int ArgC, char ArgV[] )
{
    int a = 1;
    if( a == 1 )
    {
        a = 3
    }
    else if( a == 3 )
    {
        a = 5;
    }
    else 
    {
        a = 1;
    }

    std::cout << a << "\n";

    if( a == 1 ) 
    {
        a = 3;
    }
    if( a == 3 )
    {
        a = 5;
    }

    std::cout << a << "\n"
    std::cin.get();
}
Run this and see if you can figure out what the difference is.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Else if statement

Post by albinopapa » July 14th, 2018, 5:35 am

@MrGodin, great find.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Else if statement

Post by MrGodin » July 14th, 2018, 9:02 pm

@albinopapa, explains it better than i can lol :)
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Else if statement

Post by albinopapa » July 15th, 2018, 5:23 am

Nah, your response was a good find and will allow them to read and have it explained. All mine does is allow them to see in action what happens if they choose to use multiple ifs instead of if/else if.

Sometimes it doesn't matter, and I've seen it a lot in projects from people on the forum asking for help.

Code: Select all

if( wnd.kbd.KeyIsPressed( VK_UP ) )
{
}
if( wnd.kbd.KeyIsPressed( VK_DOWN ) )
{
}
if( wnd.kbd.KeyIsPressed( VK_LEFT ) )
{
}
if( wnd.kbd.KeyIsPressed( VK_RIGHT ) )
{
}
This is pretty common in the projects that get posted here. So if the user presses the UP arrow, the first if gets executed. Now what happens when the DOWN arrow is also pressed? Usually, pressing up subtracts a value from Y and pressing down adds the same value to Y. Which means the value of Y will basically remain the same ( 5 + 3 - 3 = 5 ). If these were to be if/else if blocks, only the UP arrow would be considered because it is listed first and the else if down pressed would be skipped.

Code: Select all

if( wnd.kbd.KeyIsPressed( VK_UP ) )
{
}
else if( wnd.kbd.KeyIsPressed( VK_DOWN ) )
{
}
if( wnd.kbd.KeyIsPressed( VK_LEFT ) )
{
}
else if( wnd.kbd.KeyIsPressed( VK_RIGHT ) )
{
}
Here, we have the case where only UP or DOWN is checked. First UP is checked, and only if UP is not pressed, DOWN is checked. Then LEFT and if LEFT is not pressed, then RIGHT is checked.

Code: Select all

if( wnd.kbd.KeyIsPressed( VK_UP ) )
{
}
else if( wnd.kbd.KeyIsPressed( VK_DOWN ) )
{
}
else if( wnd.kbd.KeyIsPressed( VK_LEFT ) )
{
}
else if( wnd.kbd.KeyIsPressed( VK_RIGHT ) )
{
}
Here, you are restricting the user to only one of the four direction keys. This means the user can't move diagonal. They must choose, and whichever direction is higher on the list is chosen if multiple keys are pressed ( UP and LEFT are pressed, only UP is accepted ).
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Jeroen
Posts: 2
Joined: July 13th, 2018, 11:08 pm

Re: Else if statement

Post by Jeroen » July 15th, 2018, 12:01 pm

I think I get it now so if you use else if statements it can only ever execute one of the else if statements at once but if you use only if statements it can execute multiple at the same time. Did I get that right?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Else if statement

Post by albinopapa » July 15th, 2018, 7:15 pm

Well, not at the same time, but each one gets evaluated using all ifs. The only time all if/else if statements get evaluated is IF all previous cases were false.

Code: Select all

if( true ) { do this }           // If false, move to next one, if true skip all other cases
else if( true ) { do this }    // If false, move to next one, if true skip all other cases
else { do this }                 // Skip if any case above was true

if( true ) { do this }          // If false, move to next statement, if true execute and move to next statement
if( true ) { do this }          // If false, move to next statement, if true execute and move to next statement
if( true ) { do this }          // If false, move to next statement, if true execute and move to next statement
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply