Control Structures (if else)

Control structures like if...else and switch...case are what allow the computer to make decisions based on input and calculations. According to different conditions and circumstances your program can make different decisions or pathway of actions.

The most important one is the if else structure.

#include <iostream>
using namespace std;

int main(){
  int inferno = 0;
  if(inferno > 100){
    cout << "inferno IS greater than 100\n";
  }
  return 0;
}

Of course in the above example, the text will never be displayed because inferno is always zero.

if(condition){
 // this is executed ONLY if condition is TRUE or 1 or non-zero
}

Conditional Operators

Common conditional operators include:

  • > Greater than (tests if left side is greater than right)
  • < Less than (tests if left side is less than right)
  • || Logical OR (tests if left side is true OR right is true)
  • && Logical AND (tests if left side is true AND right is true)
  • ! Logical NOT (tests if right side is false)
  • == equivalence (tests if both left and right are equivalent)
  • != Logical NOT and equal (tests if left and right are NOT equal)
  • >= Greater than OR equal to
  • <= Less than OR equal to

#include <iostream>
using namespace std;

int main(){
  int inferno = 0;
  cin >> inferno; // get input from user keyboard
  if(inferno >= 100){
    cout << "inferno IS greater than OR equal to 100\n";
  } else if(inferno <= 80){
    cout << "inferno IS less than OR equal to 80\n";
  } else {
    cout << "inferno IS between 81 and 99.\n";
  }
  return 0;
}

Yes, you can combine any number of if and else statements to create different paths of actions. The last else statement decides everything you didn't define.

Switch and Case

You can use switch case system, where you are testing for individual cases or integers.

#include <iostream>
using namespace std;

int main(){
  int inferno = 0;
  cin >> inferno; // get input from user keyboard
  switch(inferno){
    case 0:{
      cout << "Zero was entered.";
      break;
    }
    case 1:{
      cout << "One was entered.";
      break;
    }
    case 2:{
      cout << "Two was entered.";
      break;
    }
    case 3:{
      cout << "Three was entered.";
      break;
    }
    default:{ break; } // in case of everything else, do nothing
  }
  return 0;
}

The above can be translated to if...else blocks:

#include <iostream>
using namespace std;

int main(){
  int inferno = 0;
  cin >> inferno; // get input from user keyboard
  if(inferno == 0){
      cout << "Zero was entered.";
  } else if(inferno == 1){
      cout << "One was entered.";
  } else if(inferno == 2){
      cout << "Two was entered.";
  } else if(inferno == 3){
      cout << "Three was entered.";
  } else {
     // do nothing
  }
  return 0;
}

It is definitely more beneficial to use switch case whenever possible.

Short Hand If Else

Sometimes, there is too much code to type and it may be easier to use the following format for if else. However, if you use this, you should have an else clause. Only use it for small boolean or integer option testing, like "IsRed()" do this else do this. It's good for options or settings the user enters.

Do not use it for large operations.

(DidUserEnterRed)?MakeColorRed():MakeColorOrange();

If the boolean variable "diduserenterred" is true, then it will make the color red, otherwise, orange function will be called.

Here's a good example to use the short hand for:

#include <iostream>
using namespace std;

int main(){
  int OpenInNewWindow;
  cout << "Enter 1 if you want to open it in a new window.\n";
  cin >> OpenInNewWindow;
  OpenWebsite("http://www.infernodevelopment.com", "/forums", "Welcome Message", (OpenInNewWindow)?240:100);
  return 0;
}

The function above is pseudo code, but it expresses the best time to use shorthand for.

Perhaps a function needs to give a parameter of 240, every time it wants to open a new window, and 100 if it does not. The condition tests if OpenInNewWindow is 1 or 0, if 1 then 240, otherwise 100.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.