Thursday, July 2, 2009

if ... then with C/C++

Absolutely the most central part of the core of every program is the if ... then statements of every program. The purpose of an if ... then statement is to check to see if something is true, and then perform an action if it is true.

Let me give you a few examples:
  • If Mario is hit by a barrel, then get rid of one of the players lives
  • If a user is banned, then do not let them post comments
  • If dogs hate cats, then dogs fight with cats
  • If a new video game is a AAA title, then it will sell a lot of copies
In C++ we perform if checks based on a variables' value. There are seven basic checks we can perform:
  1. If a value is equal to another value ( == )
  2. If a value is not equal to another value ( != )
  3. If a value is greater than another value ( > )
  4. If a value is less then another value ( < )
  5. If a value is greater than or equal to another value ( >= )
  6. If a value is less than or equal to another value ( <= )
  7. If a value is greater than 0 ()
  8. If a value is less than or equal to zero (!)
The basic syntax of an if ... then statement is below:  
if(value_one check value_two){ 
code to execute if the if statement is true  
}  

Here is a long, although perfectly practical example of a use of all of the basic if statements in a simple C/C++ program:
#include (iostream)


int main(){ 

int janes_age = 0; 
int bills_age = 0; 

std::cout << "How old is Jane?";
std::cin >> janes_age;  

std::cout << "How old is Bill?"; 
std::cin >> bills_age;  

//Now lets see how their ages compare  

if(janes_age == bills_age){  
std::cout << "Jane is the same age as Bill";
}  

if(janes_age != bills_age){
std::cput << "Jane is not the same age as Bill";
}


if(janes_age > bills_age){  
std::cout << "Jane is older than Bill";
}

if(janes_age <= bills_age){  
std::cout << "Bill is older than or the same age as Jane.";  
}

if(bills_age <= janes_age){  
std::cout << "Bill is younger than or the same age as Jane.";


if(!janes_age){ std::cout << "Jane is not even more than 0 years old yet!";
} 
 
if(bills_age){ 
std::cout << "Bill is more than 0 years old."; 
} 
 
int pause; 
cin >> pause; 
 
return 0;  
} 
 
The program listed above creates two variables representing the age of two people; Bill and Jane. The program than asks the user to assign a values to be held by each variable. After wards, the program uses all of the seven basic if statements to compare the variables and output text if each if statement is true. But would happen if your programming duties asked you, the programmer, to compare the truth behind two if ... then statements and then execute code if they were both true? Here's a few examples to show you what I mean:
  • If Mario is hit by a barrel and the player's health is equal to 1, then the game is over
  • If dogs hate cats and cats hate dogs, then dogs and cats fight
  • If a new game is a AAA title and it was developed by Davide Jaffe, then it will sell lots of copies
In C/C++ we use the character's && to represent and. The and clause is a clause that returns true only when both of it's depending clauses are true. The following is an if ... then statement that could be used in the program you read about Bill and Jane previously:
if(janes_age < bills_age && janes_age < 5){
std::cout << "Jane is younger than Bill because she is only a toddler.";
}  

In this example, the if statement is only tue if Janes age is less than Bills age and if Jane is less than 5. If either one or both of these two statements is false, then the if ... then statement is not true and the computer will not output, "Jane is younger than Bill because she is only a toddler." The following would also work the same way as the above example:
if((janes_age < bills_age) && (janes_age < 5)){ 
std::cout << "Jane is younger than Bill because she is only a toddler."; 
}  

The extra set of parenthesis in this second example does nothing to change the statement but many programmers, myself included, feel that is makes the if statement look more clear. This is especially true when you're writing complex if ... then statements such as collison routines ;-) The other, last, type of clause used in if statements is the or clause. The or clause only returns true if at least one of it's depending statements are true. Heres a few examples of using an or clause:
  • If Mario is hit by a barrel or a fireball, then Mario is dead.
  • If dogs hate cats or cats hate dogs, then dogs and cats fight
  • If a user is banned permanently or temporarily, then he can not post comments
In C/C++ the or clause is represented by the characters ||. Here is an example of using an or that could be applied to our program about Jane and Bill: 
if((janes_age > bills_age) || (bills_age < janes_age)){
std::cout << "Jane is older than Bill";
}

In this example the computer will output, "Jane is older than Bill" if either janes_age is greater than bills_age or bills_age less than janes_age. 
 
if ... then statements are used literally everwhere in programming. Every laguage harnesses them, and every program has too many of them to count. While the examples provided here were simple, the core idea I hope I got across to you, the reader, is that without if ... then statements computer programs as we know them would not exist. (And I also hoped you learned how to properly use them in your C/C++ programs as well ;-) )

No comments:

Post a Comment