C++ part 5 – for loop

(UPDATED 11.9.2013)
In this post we are going to go through a statement called for loop. A for loop is a function in C++ (and other programming languages) that does something the number of times that the user specifies it to do. The for loop can go through values/table of values, do counting among other things you would expect a counter to do. In the below picture I have drawn a picture of how it works. Click on the picture for zooming in.

for

– In the red section we tell the for loop to initialize the variable counter with the value 0.
– In the green section we have a statement where we code the for loop to STOP incrementing the value counter if the value counter has a value that is smaller or equal to 10.
– In the blue section we have told the for loop to increment the variable counter for every loop that it takes.

Basically the formula for the above picture is right below that Dennis Ritchie wrote himself in his own book (I take the complete credit of the above masterpiece that I made with paint).

for (expression1; expression2; expression3)
{
statement
}

and basically what the above code means is that if the condition is true, then do the same cycle again and again until the condition is false and break away from the loop and continue the program like I have tried to demonstrate in the below picture.

pic1


Below is the real code to get the program running

#include <iostream>
using namespace std;
int main ()
{
    int counter;
    
    
    for(counter = 0; counter <= 10; counter++)
    {
    cout << counter << endl;            
    }
    system(“PAUSE”);
    return 0;
}

and here is the output for the above code

forloop2

———————————————————————————————————-

but wait, there is more! Below I have written the multiplication table for the number 2.

#include <iostream>
using namespace std;
int main ()
{
    int counter;
    
    
    for(counter = 0; counter <= 10; counter++)
    {
    cout << 2 * counter << endl;            
    }
    system(“PAUSE”);
    return 0;
}

and here is the picture

formulti

———————————————————————————————————-

Now that we have gone few keypoints of the functionality, I think that the next wise thing is to go through some problems that the people have with the loops for example in the below, I have written a code. What is your opinion that will happen in it? What will be the output?

#include <iostream>
using namespace std;
int main ()
{
for(int i = 0; i <= 10; i++)
{
int theoryNumber = 1;  
cout << “The counter is “<< i <<  endl;
theoryNumber = theoryNumber + 100 + 100;
cout << theoryNumber << endl;
}
s
ystem(“PAUSE”);
return 0;
}

Because for loop always starts over again, meaning that it goes cycles, it will do whatever you write inside it everytime it starts a new loop cycle. For example in the above code, everytime when the for loop ends and starts all over again, it will initialize the theoryNumber in every new loop. This means that the theoryNumber will not accumulate any previous arithmetics or values it was given or had as an input if the creation of the variable is inside the for loop. I will go shortly over this. Below is the output of the aboe code.

forloop2.1

The example material is bad but you probably get the point.

———————————————————————————————————-

In the below example I have an input example of the for loop with an input and if clauses.

#include <iostream>
using namespace std;
int main ()
{        
int sum = 0;    
int userInput = 0;    
// Always remember to initialize!    
// because you never know when your    
// system might crash or go looney        
  for(int i = 0; i <= 10; i++)    
  {        
  cout << “Enter a whole number: ” << endl;        
  cin >> userInput;        
  sum = sum +  userInput;    
   }    
cout << “The sum of the values you enter is: “<< sum << endl;
system(“PAUSE”);
return 0;
}

and below is the output

forloop3

———————————————————————————————————-

Below is an example of for loop with if statement

#include <iostream>
using namespace std;
int main()
{
    int a = 0;
   
    for(int i = 0; i < 10; i++)
    {
            if(i != 9){
                  cout << “the variable i still has to increment” << endl;
            }else if(i == 9){
                  cout << “The program will now stop the forloop” << endl;
            }
    }
   system(“PAUSE”);
   return 0;
   
}

the picture is below

forloop

———————————————————————————————————-

I will add more content today later and with great hesitation and doubt I now release this. Please note that this post does contain a lot of problems so check back later to see the whole post in its final form.

Useful softwares – PuTTY

PuTTY i.e. a telnet- and ssh-client. We used this to connect our workstations with our servers in the university. The program basically allows you to do many things, where one of them with my example was to link with the database/servers so that I could connect to the server through my Java web-application for database queries.

The webpage where you can download it is here
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

below is a picture of the software
putty

I will later, when we will start go to the database section in Java, post the rest of the information related to this specific program.

C++ part 4 – switch statement

In post, we are going through the switch statement. Switch statement is very similar to if statement that we went through in the previous C++ post. The only difference between switch statement is that many people do not use it anymore because it is considered to be a bit outdated way to do algorithms and I will tell you why.

Switch statements only take one value to consideration although many cases will be implemented in one switch statament. The below is the example of one switch statement.

#include <iostream>
using namespace std;
int main()
{
    int choice;
    
    cout << “Please pick a number from 1-5: ” << endl;
    cin >> choice;
    
    switch(choice)
    {
      case 1:
           cout << “You have chosen: ” << choice << endl;
           break;
      case 2:
           cout << “You have chosen: ” << choice << endl;
           break;
      case 3:
           cout << “You have chosen: ” << choice << endl;
           break;
      case 4:
           cout << “You have chosen: ” << choice << endl;
           break;
      case 5:
           cout << “You have chosen: ” << choice << endl;
           break;
    }
    system(“PAUSE”);
    return 0;
}

and below is the picture of one outcome

example1

Basically what happened in the above example is that if the user entered 3, the program will jump in the part of the code that I have highlighted below


    switch(choice)
    {

where the switch statement will go through case by case, what the user has entered and when it finds for example in the code below


      case 3:
           cout << “You have chosen: ” << choice << endl;
           break;

where the “case 3:” means that if the value is 3, do something. In our example it will print on the console what you saw in the picture. The “break” means that do not execute any other case if the value is found. If there is no break, the switch statement will execute every case below the found value.

I will post this so that there will not be two week interval but I promise to update this section with more updates, technicalities, theory among other things.

C++ part 3 – If, If else, else statements

(UPDATED 26.6.2013)

I am sorry for the previous master piece of chaos namely Boolean operations. From now on I will try to be as clear as possible. In this post we will go through some basic if, if else statements.

Now you might think what is if statement? If statement is part of almost any programming language and important part that is to create algorithms to achieve something. If statements basically are statements to check if some value we input or output will be the value we want (or do not want) or if we are not sure about the value coming in our program.

For example in the below I have tried to demonstrate in pseudo code

// Example 1
if( 2 > 1 )
// Conditional expression
{
 cout << ” The value 2 is bigger than 1″ << endl;
}

Another example

// Example 1
if( 2 == 2 )
// Conditional expression
{
 cout << ” The value 2 equals 2 ” << endl;
}

As a side note, the == is not the same as = and here is why.
The == (equal) operator is a Boolean meaning that if you are creating the program logic and you want to compare the values, you will use this operator. On the other hand if you want to assign a value to another variable you use the = (assignment) operator.

If statements can basically be though as crossroads where if some value equals for example “John” or the value 1, then output “Hi John” or do some arithmetic operations with the numerical value. Below is the code example of if else statement

#include <iostream>
using namespace std;
int main ()
{
    int userInput;
    
    cout << “Enter your age: ” << endl;
    cin >> userInput;
    
    if(userInput < 0)
    {
      cout << “Are you from other dimension?” << endl;
    }else if(userInput >= 0 && userInput <= 20)
    {
      cout << “You are very young!” << endl;
    }else if(userInput > 20 && userInput <= 30)
    {
      cout << “You are not so young anymore” << endl;
    }else if(userInput > 30 && userInput <= 40)
    {
      cout << “You are in the middle age crisis!” << endl;
    }else if(userInput > 40 && userInput <= 70)
    {
      cout << “You are old!” << endl;
    }else if(userInput > 70 && userInput <= 130)
    {
      cout << “I think you are lying” << endl;
    }else{
      cout << “You have either entered invalid input or you really are a fossil” << endl;
    }
}

To be able to understand what is happening in the above example, you should look my previous post more closely. What happened in the above statement is that the user is asked for the input and the if statement will check what input the user entered and will output messages on the command line according to the user value input. For example in the below line of code

    if(userInput < 0)
    {
      cout << “Are you from other dimension?” << endl;
    }

we are checking that if the user input in the variable userInput is below the numerical value zero/0, console out/output the message on the command line “Are you from other dimension?”. If the user entered value above or equal zero/0 but under or equal 20, then output “You are very young!” in the below line of code.

    else if(userInput >= 0 && userInput <= 20)
    {
      cout << “You are very young!” << endl;
    }

Please note that there is “else if” instead of if. This basically means that if the value was not below zero, jump on the next if statement i.e. “else if” and you just continue using “else if” until you do not want any more dimensions on your program and end it with “else” where everything else will output something you have dictated for example error message etc.

Also note that I have used the && in the program meaning that if the value is over or equal 0 and value is below or equal 20 then output “You are very young!” and I end the statement with brackets where the next else if statement will come right after the bracket.

Below is example of the program in a picture

if statement

and here is some technical gibberish in the below link

http://msdn.microsoft.com/en-us/library/y34a3dk2.aspx

Below is another code example

#include <iostream>
using namespace std;
int main ()
{
    int userInput;
    
    cout << “Enter either 1 or 5: ” << endl;
    cin >> userInput;
    
    if(userInput == 1 || userInput == 5)
    {
      cout << “CORRECT!” << endl;
    }else
    {
      cout << “INVALID!” << endl;
    }
 
    system(“PAUSE”);
    return 0;
}

Where the

    if(userInput == 1 || userInput == 5)
    {

means that if the input is 1 OR the input is 5 output “CORRECT!” else output “INVALID!”.
Below is few pictures

if 2

if 3

Here is more example code related to if statement below

#include <iostream>
using namespace std;
int main ()
{
    int userInput1, userInput2, arithmetic;
    string choise;
    
    cout << “Enter the arithmetic operation(+, -, /, *): ” << endl;
    cin >> choise;
    
    cout << “Enter first whole number: ” << endl;
    cin >> userInput1;
    
    cout << “Enter second whole number: ” << endl;
    cin >> userInput2;
    
    if(choise == “+”)
    {
      arithmetic = userInput1 + userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “-“)
    {
      arithmetic = userInput1 – userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “/”)
    {
      arithmetic = userInput1 / userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else if(choise == “*”)
    {
      arithmetic = userInput1 * userInput1;
      cout << “The value is ” << arithmetic << endl;
    }else{
          cout << “INVALID INPUT!” << endl;
    }
 
    system(“PAUSE”);
    return 0;
}

and below is picture of one possible output

if 4

——————————————————————————————————————————–

Age program version 2

Code below

#include <iostream>
using namespace std;
int main()
{
 int enteredAge;
 
 cout << “Enter your age: ” << endl;
 cin >> enteredAge;
 
 if(enteredAge < 0)
 {
  cout << “Entered age cannot be a negative” << endl;
 }else if(enteredAge >= 0 && enteredAge <= 12)
 {
  cout << “You are a child” << endl;              
 }else if(enteredAge >= 13 && enteredAge <= 19)
 {
  cout << “You are a teen” << endl;
 }else if(enteredAge >= 20 && enteredAge <= 40)
 {
  cout << “You not old!” << endl;              
 }else if(enteredAge >= 41 && enteredAge <= 90)
 {
  cout << “You are old…” << endl;
 }else{
  cout << “You are very…very old” << endl;
 }
    
}

below is a picture of what, happens if the user enters 0.

ss

and with paint…

if

I will publish this and add more content later.

C++ part 2 – Boolean operations

(UPDATED 4.5.2013)

In this post I will slightly write about some boolean operations you can do in C++ programming language. The term boolean was named after George Boole (1815–1864) who was “an English mathematician and a founder of the algebraic tradition in logic”. As far as I know, boolean operations are used in many fields from electronics to programming these days so you might hear boolean in another context later also. Anyway we have many boolean operators in C++.  Below is a table of the operators with examples. Note that I have “pseudocode” in the examples so they wont work in C++ compiler.

Syntax  Example  Outcome  Meaning
<         If 1 < 2          True       If 1 is smaller than 2, do something
>         If 1 > 2          False
>=       If 2 >= 1       True       If 2 is bigger(equal) than 1, do something
>=       If 2 >= 2       True
<=       If 2 <= 2       True       If 2 is smaller(equal) than 2, do something
<=       If 2 <= 3       False
!=        If 2 != 1        True       If 2 is NOT 1, do something
!=        If 2 != 2        False
!=        If 2 != 3        True
==       If 2 == 2       True       If 2 equals 2, do something
==       If 2 == 1       False
||          i==2 || i==1  Depends

—————————————————

More operators and theory

Operators    Meaning with the operator “name”
!=               1 NOT 2 then do something
==               2 == 3
||                  OR
&&               If 5 > 3 AND 5 < 7, in programming if i > 0 && i < 5, do something
<                  i < b
>                  i > b

—————————————————

More…

Operator  Meaning (If the inputted or the value that we want to check is on the left side)
<          If the inputted value is smaller than what we want (or dont want) do something
>          If the inputted value is bigger than what we want (or dont want) do something
>=        –..– Bigger or Equals to
<=        –..– Smaller or Equals to
!=         Does not equal
==        Equals
||          OR for example if i == 2 || i == 3 then do something
&&       AND for example if i < 5 AND i > 0 then do something

—————————————————

Below is code with some boolean operations with a picture of the output.

#include <iostream>
using namespace std;
int main()
{

int i = 4;
int u = 2;
int v = 6;

cout << “Value 1 means true and 0 means untrue/false” << endl;
cout << “Is value i bigger than u? “<< ((i > u) && (i < v)) <<endl;
cout << “Is value u smaller than i? “<< (u < i) <<endl;
cout << “Is value v bigger than i AND bigger than u? “<<((v > i) && (v > u)) <<endl;
cout << “Is value i bigger than u AND smaller than v? “<< ((i>u) && (i<v)) <<endl;
cout << ((76573-541)>=4516) << endl;
cout << ((1<0) || (1>0)) << endl;
cout << ( 10 > 100 ) << endl;
cout << ( 40 < 30 ) << endl;
cout << ( 10 <= 10) << endl;
cout << ( 15 >= 10 && 15 <= 20) << endl;
cout << ( 15 != 15) << endl;
cout << ( 15 != 14) << endl;

biggersmaller
system(“PAUSE”);
return 0;
}

—————————————————

#include <iostream>
using namespace std;
int main()
{

int a;
int b;
int compare;
int sum;

cout << “Enter the first whole number: “;
cin >> a;

cout << “Enter the second whole number: “;
cin >> b;

cout << “Enter the whole number you want to compare with: “;
cin >> compare;

sum = a + b;

cout << “The sum of the two inputted whole numbers were: ” << sum << endl;
cout << “The whole number you wanted to compare it with was ” << compare << endl;
cout << “The output of this boolean operation is: ” << (sum > compare) << endl;

system(“PAUSE”);
return 0;
}

—————————————————

In the later tutorials you might also come in contact with the ! syntax where the syntax for example means that if you input a value in a variable a and then somewhere in the code you might have (Note that it’s pseudo code)

!a.equals(1)

Where we check that if a DOES NOT equal 1, do something (Or do not do) depending on your program logic.

—————————————————

By now you should have realized that programming has a lot of mathematical steps to solve a problem i.e. it is called a computational problem solving.

To be able to solve problems in programming, you do not need to have a good mathematical skills, except if you are creating a program to do mathematics.

The problem solving in programs usually contain simple arithmetic operations and algorithmic design to approach every ten line of code.

To be able to understand why, it is done this way, all you need to do is to look at the history of computers. Computers originally were created to be large “calculators”. Even the computer hardware is built around set of mathematical operations.

The problems you will work on, will be mechanical and to be able to solve any kind of problem you must create code so the computer knows what to do.

Right now you cannot see any real life benefits but later you will see more tutorials and syntax where you can actually achieve something. Right now you just need to play with the boolean operations to understand the logic behind them.

As a side note, there is bunch of more boolean operators that we did not cover in this post. You can Google more booleans operators for testing.

References:

http://www.cplusplus.com/doc/tutorial/operators/

http://ocw.mit.edu/index.htm

http://plato.stanford.edu/entries/boole/