While Loops

While Loops can be used like For Loops, however, they have some differences.

Here’s the syntax:


while (condition){

// code

}

or


do{

} while(condition);

To be truthful, I’ve never used the second example.

While Loops can be used in a similar way to For Loops; as an example, while x < 10, do some thing, then add one to x each loop.

However, the difference is that you don’t have to use it for iterating.

How about this: you have 10 cups, and there’s a marble under one of them, the computer has to find the marble. A For Loop would look under all 10 cups before finishing, whereas, a while loop would stop the loop when it found the marble. This doesn’t make much difference with such small number, but imagine the difference over a thousand cups.

This example below is a program that generates a random number between 0-100. The user than has to guess the number, as program tells the user higher or lower. It’s called a Bracketing Search.


#include <iostream>

using namespace std;

int main()
{

int guess = 0; // users guess.
int t = 0; // number of tries.

srand(time(0)); // creates a seed based on the ever changing time.
int r = 1+ rand() % 100; // generates a rand # between 1-100.

// while guess is not the random number
//and number of tries is less than 10.
while (guess != r && t < 10)
{
 cout << "Guess a number between 1 and 100." << endl;
 cin >> guess;

if (guess > 0 && guess < 100)
 {
 if (guess == r)
 {
 cout << "You win! You guessed " << r << "!!!" << endl;
 }
 else if (r > guess)
 {
 cout << "HIGHER" << endl;
 }
 else
 {
 cout << "LOWER" << endl;
 }
 t ++;
 }
 else
 {
 cout << "Invalid Input" << endl;
 }
}

cout << "Number of tries: " << t << endl << endl;

if (t == 10)
{
 cout << "Sorry no more chances!" << endl;
}

}

For Loops

Firstly I’d say I didn’t know I had to press Ctrl C to stop a program running. When I saw my first, second and third infinite loop I pooped my pants… and clicking the x in the corner closes everything, very annoying!

So here’s how a For Loop is set up, followed by an example.

I always learn more playing with examples and figuring out myself.


for (variable initialization; condition; variable update)

{

/* Do some awesome thing while the condition is true.

}

Example of a For Loop:


for ( int x=0; x<10; x++)

{

cout << x ;// code runs, adds 1 to x each time, stops when x=10.

}

Outputs:


0123456789

If, else…

I’m a teacher by profession and I feel like these two words come out of my mouth far too often! But they’re really useful and I can actually start writing programs now ^^*.

Simply, if somethings true, do this. Else if that’s not true and this other thing is true, do this. Else, do this other thing. Here’s simple code I made.


#include <iostream>

using namespace std;

int main()
{
 int score = 0;
 cout<<"What's your score out of 100? \n";
 cin>> score;

 if (score == 100)
 {
 cout<<"You got a perfect score!";
 }
 else if (score > 90 && score <100)
 {
 cout<<"You scored an A";
 }
 else if (score > 80 && score <90)
 {
 cout<<"You scored an B";
 }
 else if (score > 70 && score <80)
 {
 cout<<"You scored an C";
 }
 else if (score > 60 && score <70)
 {
 cout<<"You scored an D";
 }
 else if (score >= 0 && score <60)
 {
 cout<<"You scored an F";
 }
 else
 {
 cout<<"Did you enter a number between 0-100?";
 }

}

I got a little head of myself when I wrote this and started using Boolean operators; AND &&, OR || and NOT !. NOT confused me at first but a good example is !(TRUE) = FALSE.

Math

Quickly reviewed mathematical functions, they’re pretty straight forward, but its good to keep a list. And MDAS (multiply *, divide /, addition +, subtraction , in order) applies… and for some reason I skipped this at school and went straight to brackets; so I occasionally get confused.

Then we have a single equal =, which assigns a variable to a name. And the double equals == which means equivalent; so that’s what we’ll use for the math stuff. Also, more than >, less than <, more than or equal to >=, less than or equal to <=, and not equivalent to !=. Notice the equal sign =, always comes second. 

It’s also possible to shorten code, for example, i = i + 1 becomes i++.

 

Starting C++

This blog is just a place for me to store my notes all together, and to track my progress as I learn C++. 

First I decided to install an editor and compiler. I used Quincy at university, so I’ll continue on that. Easy to use, write a code, save, build, run… sorted.. and Ctrl C for that infinite loop!

To start writing code, open a C++ source file. And most beginner’s programs will begin with:


#include <iostream>

using namespace std;

int main()

{

//write awesome code here

}

The double forward slash // is how you make comments in your code, when the code is run, these comments are ignored. You can also use:


/* multi-lined comments

Some more comments */