Homework: http://beginnerscpp.com/forums/index.php/topic,60.0.html



If Statements
If statements are used to determine the course that our program will run in, based off whether a condition evaluates to being true, or false. The statement of if has 2 parts. if and the condition that it’s checking (which can have many parts). A simple if statement might look something like this:

or they can be much more complex, like this

In the first example we are simply checking if a number is equal to the number 2. Once we determine that is true, we add 5 to the number. The second example is something you might see in a game development scenario. A user is goes to cast a spell, there needs to be checks made against current values, and those are passed over to our if statements. We need for ALL of those conditions to be true for the spell to be cast (the user needs enough intellect to understand the spell, they need both mana and health, and they cannot be stunned) if any of those are not true, it will be passed to the else if (and then evaluated for truth there) then passed to else if that is not true either.

If Statement Flowchart
Classically if statements were represented (in flowcharting) as diamond layed on its side, with lines coming from the edges diagonally to illustrate that there were two paths that can be taken from an if statement (symbolizing an if and an else). While this is technically correct, it does lead to some questions among beginners for instance the following is VALID code

In the example above you will notice that there is no “else” attached to this if. So the earlier example showing two distinct “branches” might not always be clearly represented in code. A way that I’ve seen some beginners cope with this confusion is by putting in Null else statements (else statements that are just placeholders) and I am against this for 2 reasons:

  1. It adds length to your code
  2. It adds no functionality to your code

Here’s an example of something a student sent me that was along those lines.

As you can see there is no functionality gained from this type of code.


Scope
Scope is essentially the “lifespan” of a variable. When a variable is declared within a specific scope it “dies” (is destroyed) with that scope. This is useful and dangerous at the same time. Scopes will become more important when we get into loops. Below is a short example showing a variable declared in a scope and being unavailable outside of the scope:

Boolean Comparison Operators

  • == (Equal to)
  • != (Not Equal To)
  • ! (Not (typically used with bool variables))
  • > (Greater Than)
  • < (Less than)
  • >= (Greater than / Equal to)
  • <= (Less Than / Equal to)
  • || (Or)
  • && (And)

Please note, the listing above is not a complete list of operators, but it will encompass the scope of what we will be learning in this course. There are other operators called “Bitwise operators” that you can read about if you choose, though you will likely not need to use them for quite some time.

Homework: http://beginnerscpp.com/forums/index.php/topic,61.0.html



Nested If Statements
Despite the title of this lesson, I did very little in the way of showing you nested if statements, and the reason why I opted to teach it this way was because I wanted to show that intelligent program design can get away from nesting. Sometimes nesting is unavoidable, and for those cases the following syntax remains true

The only thing that I can advise you on when it comes to nested if statements is that you should be EXTREMELY strict when it comes to your indenting. Furthermore, some people like to document their code to remind them which if statement they are in, however I find this type of commenting somewhat excessive. A comment here and there to remind us what something does, or what we’re testing is fine, but when you see something like this, it’s a bit excessive.

While I understand the intention of the person that made the comments, it actually adds to confusion over stating the implied usage on a single line like this:



Topics discussed: If statements, simple if syntax, variable output, if else structure.

Source Code from lesson:
#include

using namespace std;

int main(){

double accountBalance=5000.0, withdrawAmount=0;

cout << "How much money would you like to withdraw? "; cin >> withdrawAmount;

if(withdrawAmount > accountBalance){
cout << "Error: The amount you tried to take out is greater than your current balance."; } else if(withdrawAmount > 1000){
cout << "It looks like you're trying to take out a lot of money, call your bank to confirm"; } else{ cout << "You withdrew: " << withdrawAmount << " -- Your remaining account balance is: " << accountBalance - withdrawAmount; } // if(conditional) --Structure // IF - (if this is true) -- do some stuff // Else if - if the first condition wasn't true, check to see if this is true // else -- If none of the things we checked are true // Boolean operators // == (equal to) -- ||(or) -- != (Not Equal To) -- ! (not) -- < (less than) -- > (greater than),
// <=, >=(less than / equal to and greater than / equal to)
// true -- false -- bool (variable type that contains true or false)

return 0;
}


If statements
If statements are the backbone of simple decision making in object oriented programming languages. The if statement checks one or more statements for a boolean(true or false) value. Their basic structure is as follows


if (condition){
//if the above is true
}

//optional

else{ //or elseif(condition){
//if the "if" statement above was not true
}

Below are some examples of these statements:


If statement examples:

// Examples
// Example 1

int i=9;
if (i == 9){
cout<< "This condition is true"; } //this statement should be true

in the above example the if statement compares (using the == operator) the integer value of i, to the number 9. In that case the statement if (i==9) is true, thus the code inside the brackets is executed.


// Example 2

int i=9;
if ( i != 8){
cout << "This condition is also true"; }

In the example above we see that the contents of i (which is 9) is being compared to 8 with != which means "does not equal" or "is not equal to". Since 9 does not equal 8, the contents of the if statement will execute.


//Example 3
int i=9;
if (i == 8){
cout << "The if statement here won't be displayed"; } else{ cout << "This will be displayed"; }

In the final above example we are checking to see if the value of i (9) is equal to 8, in this case it is no. So everything from the bracket after the conditional until the subsequent closing bracket is skipped over. Once the word else is seen, it will execute anything in the brackets after the else, unless there are further if statements to be evaluated inside the if.


Boolean operators
Below is a brief listing of the boolean operators used in testing conditionals and what each of them mean, there will be specific usage examples of each later on in the series.

  • == : Is equal to
  • != : Is not equal to
  • > : Is greater than
  • >= : Is greater than or equal to
  • < : Is less than
  • <= : Is less than or equal to
  • ! : Is not
  • && : And
  • || : Or


Topics Discussed: More data types, integrating said data types with if, and the start of a review seen in the next section.

Source Code Available Here.


Strings
Strings are an extremely important data type in C++ because they can hold literally anything. This is extremely useful for data sanitation. The best way to think of a C++ string is to have a strong understanding of how they’re implemented. If we’re going to explicitly set a string equal to a value, we can do so like this:
string stringName = "This is the value of the string";


bool
Bools are variables that hold a value of being either true (1) or false (0). These values cannot be seen as an actual number, but will evaluate to one in boolean mathematics (covered later). Bool variables default to “false” if you don’t specify what they are explicitly:

bool variablename; // initial value is set to false
bool variableName=true; //set to true