[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]


#include <iostream>
#include <cmath>
using namespace std;
 
 
int main(){
 
  //   ==  Double equals sign  -- boolean comparison operator  
  // boolean = type of variable that can only hold true or false
  // || or, && and, > Gt, <lt , >=, < =, ! not, !=,  
  // bool -- Type
 
  //bool isTaken = false;
  //char c = ' ';
 
  //if ( isTaken ){
  //  cout << "This seat is already purchased" << endl;
  //}
  //else{
  //  cout << "This seat costs $12 would you like to purchase this seat? Y/N" << endl; 
  //  cin >> c;
  //  if (c == 'Y'){
  //    isTaken = true; 
  //    cout < < "Thank you for your purchase";
  //  }
  //  else{
  //    cout << "Thanks for your interest";
  //  }
 
  //}
 
  int i = 0;
  if ( 1 < 7 || 9 < 6  ){ 
    cout << "Is True" << endl;
    i += 9;
  }
  else{
    cout << "Is false" << endl;
  }
 
  cout << i;
 
 
  system("PAUSE");
  return 0;
}


[/spoiler]

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:

if (myNumber == 2){
    myNumber +=5;
}

or they can be much more complex, like this

int multiplier=3, manaCost=17, healthCost=14, intellect=27, playerCurrentHealth=5, playerCurrentMana=15, damage =0;
bool isStunned=false;
if(intellect >=20 && playerCurrentMana >= manaCost && playerCurrentHealth >= healthCost && !isStunned){
     damage = multiplier*intellect;
}
else if(isStunned){
    cout < < "You are stunned. " << endl;
}
else{
    cout << "Insufficient health and / or mana to cast." << endl;
}

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

if(myNumber ==5){
   myNumber +=2;
}

cout < < "The value of myNumber is: " << myNumber;

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.

...

if( startingCash >= 5000 ){
     interestRate=5;
}
else{
    //placeholder
}
...

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:

if ( true ){
    int myNumber = 1;    //Declared in scope
}     //Gets destroyed here
cout < < myNumber;     //This command will fail since myNumber was destroyed at the end of the scope established by the if statement

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.

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]


#include <iostream>
using namespace std;
 
 
int main(){
 
  //9>8 == true == 1, 1>7 ==false
 
  //nested if's
  //if inside of an if
  //Homework:  If the salesperson made less than 4000 in a month, they do not get any bonus
  //Fix all the if statements so that they work properly
  //Bonus: If the person made over 15000, they get a 100 bonus added to their check after commision has been factored in.
        // ==, < , > < !=, >=, < =, !, &&, ||
 
 
 
  double pay = 0.0, comission = 0.0, sales = 0.0;
  cout << "Enter the salesperson's sales for the month: ";
  cin >> sales;
 
  if (sales > 4000 && sales &lt;5000){
    comission = 0.01;
  }
  else if (sales >5000 && sales < 6000){
    comission = 0.02;
  }
  else if (sales >6000 && sales < 7000){
    comission = 0.03;
  }
  else if (sales >7000 && sales < 8000){
    comission = 0.04;
  }
  else if (sales >8000 && sales < 9000){
    comission = 0.05;
  }
  else{
    comission = 0.08;
  }
 
  pay = 400;
 
 
  //cout << "Pay = " << pay << " -- comission = " << comission << "\n\n";
  cout << "\nThe salesperson's pay for the month is: " << (pay*(1 + comission)) << endl;
 
  system("PAUSE");
  return 0;
}


[/spoiler]

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

if(firstConditional){
     if(secondConditional){
          //stuff Happens here
     }
     else{
          //Stuff can also happen here
     }
}
else{
     //If the first if was false
}

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.

if( condition ){//First if statement
     if( condition2){//inside second if statement
            ...
      }//end inner if
}//end first if
else{//outer else statement
     ...
}//end else

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:


//The following if statements will determine .... and if not they will go to the else and ...
if( condition ){
     if( condition2){
            ...
      }
}
else{
     ...
}

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]


#include <iostream>
using namespace std;
 
 
int main(){
 
  //9>8 == true == 1, 1>7 ==false
 
  //nested if's
  //if inside of an if
  //Homework:  If the salesperson made less than 4000 in a month, they do not get any bonus
  //Fix all the if statements so that they work properly
  //Bonus: If the person made over 15000, they get a 100 bonus added to their check after commision has been factored in.
        // ==, <, > < !=, >=, <=, !, &&, ||
 
 
 
  double pay = 0.0, comission = 0.0, sales = 0.0;
  cout << "Enter the salesperson's sales for the month: ";
  cin >> sales;
 
  if (sales > 4000 && sales <5000){
    comission = 0.01;
  }
  else if (sales >5000 && sales < 6000){
    comission = 0.02;
  }
  else if (sales >6000 && sales < 7000){
    comission = 0.03;
  }
  else if (sales >7000 && sales < 8000){
    comission = 0.04;
  }
  else if (sales >8000 && sales < 9000){
    comission = 0.05;
  }
  else{
    comission = 0.08;
  }
 
  pay = 400;
 
 
  //cout << "Pay = " << pay << " -- comission = " << comission << "\n\n";
  cout << "\nThe salesperson's pay for the month is: " << (pay*(1 + comission)) << endl;
 
  system("PAUSE");
  return 0;
}


[/spoiler]

Homework: No homework this lesson



[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]

#include <iostream>
using namespace std;
 
 
int main(){
 
  //THERE IS HOMEWORK, I JUST RAN OUT OF TIME AND FORGOT TO MENTION IT
  //Using 1 for and 1 while loop, cout 1-10 and 10-1 counted by 1's, and by 2's respectively.
  //Bonus:  Allow the user to enter a starting and ending value, as well as a number to count by, implement this using both a for and a while loop
 
  //int i = 0;
 
  //while ( i < 10 ){
  //  cout << i++ <<endl;
  //}
  bool shouldRun = true;
  int i = 1, j=2;
  while (shouldRun){
    if (i > 10){
      shouldRun = false;
    }
    i += j;
    cout << i << endl;
 
  }
 
 
  /*for (int i = 0; i < 10; i++){
    cout << i << endl;
  }*/
 
  system("PAUSE");
  return 0;
}
  

[/spoiler]

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



Loops
The over-arching theory of loops is that sometimes you’re going to have a set of data, which will need to be iterated through and have actions performed on each member. You can use if statements to qualify what happens to each item, but the loop itself ensures that every item in a set is able to be iterated through.


While
While loops are the simplest of the loops that we’re going to be discussing. The syntax is exactly the same as an if statement.

while ( condition ) { //code }

//Compared to

if( condition ) { //code }

One of the catches of using an while statement is that there needs to be some kind of a control in place or the loop will run endlessly. The most common way to see a while loop implemented for beginners is by incrementing a number. Here’s an example of how someone would do that:

int counter = 0;
while(counter <10){
     cout << counter << endl;
     counter++;
}
//  ***Please note that this code could be simplified to just cout << counter++ << endl; And have the same effect  I believe I did this in the video.


Do_While loops
These loops weren’t actually covered in the videos, as I don’t feel that they are as important in theory as other loops. The purpose of a do_while loop is to ensure that something happens at least once. Typically, in programs you might see a do_while used to make a user enter a number within a certain range. For example:

int counter = 0, choice = 0;

do{
    if(counter > 0 ){
         cout << "The number you entered is invalid, please enter a number between 1 and 10: ";
         cin >> choice;
         counter++;
    }
    else{
         cout << "Please enter a number between 1 and 10: ";
         cin >> choice;
         counter++;
    }
}while(choice <1 || choice >10);

cout << "It took you: " << counter << " tries to enter a number in the 1-10 range"
</[pre><br />

This code is just an example, but typically the reason you use a do_while is because the while may not be triggered and you need it to run at least one time.  We will get more into this as we progress in the series and out programs get more structurally complex.<br /><hr />

<strong>For loop</strong><br />
For loops are the most complex type of loop that we will be covering, and they combine a lot of the things that are "lacking" from while loops and reduces their complexity.  For loops are often complex for beginners, so they avoid them.  This often leads to more complexity in their program due to having more variables, and more lines of code.   Below is an example of a simple for loop that emulates the while loop above.<br />

<pre lang="c++">

for ( int counter = 0; counter <10; counter++){
    cout << counter;
}

As you can see, the above code took 3 lines, instead of 5. Let’s break down what exactly happened in this for loop. For loops are often broken into 4 basic parts:

  1. for statement –the literal use of the word “for” at the start of the line
  2. Initialization –This part allows you to create a variable or assign an existing variable a new value. An example of this is int counter = 0; as above.
  3. Conditional –This is the statement that must remain true (like a while) for the loop to continue running.
  4. Increment –This statement typically (although it doesn’t have to) brings your initialized variable closer to satisfying the conditional.

Here is one more example of for loops

int myVariable = 5, goal = 25;
for ( int counter = myVariable; counter < goal; counter++){
     cout << "We started this loop at: " << myVariable << " -- we are now at:  " << counter << " -- and we're counting up to " << goal << endl;
}

It is strongly recommended that you review this lesson and familiarize yourself with loops in some depth, as the next lesson will be VERY challenging as it deals with nested loops.

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]

#include <iostream>
using namespace std;
 
 
int main(){
 
  /*
  *
  **
  ***
  ****
  ***
  **
  *
  */
  //Allow the user to determine the max size for the output
  //Counter starts and ends at 1
  //Each time the counter is increasing by 1
  //Each time, there is a second counter, counting to the value of the first counter
  //Once it hits it's maximum value, it counts down.
 
  int value = 0;
  cout << "Please enter a value: ";
  cin >> value;
 
  //Count up
  for (int i = 1; i <= value; i++){
    for (int j = 0; j < i; j++){
      cout << "*";
    }
    cout << endl;
  }
 
  //Count down
  for (int i = value-1; i >= 1; i--){
    for (int j = 0; j < i; j++){
      cout << "*";
    }
    cout << endl;
  }
 
 
  system("PAUSE");
  return 0;
}
  

[/spoiler]

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



Nested Loops
In this lesson I threw you guys straight into the deep end. The basic concept of nested loops is the same as a nested if statement, you have an inner loop that will complete all of it’s instructions every time through the outer loop (unless there is a break statement somewhere). Here’s an example:

for (int i=0; i<4; i++){
    for(int j=0; j<3; j++){
        cout << j;
    }
    cout << endl;
}

The code above will look like this when output:

012
012
012
012

As you can see in the example, the inner loop is running fully every time the outer loop iterates.