sorhanp.github.io

Update Oct 27, 2018: Noticed that I got loops bit mixed up during site building and showed picture claiming, that do while and for are almost the same. It is fixed now on this blog post.

After last blog post I have kept “attending” C++ Tutorial for Complete Beginners course. During my studies I have found out that I still have the ability to think like a programmer. Meaning, that I have been able to do much more than is required on current lesson and expand my learning with things from the past. For example, when there was lesson about conditions where the lecturer suggested, that after his you should try to do program that does certain multiplication table using an C++ standard array and set values. Instead of hard coding the values to the code itself, I wrote a little program that asks from user how long multiplication table is needed and what number should be multiplied. Example code is below:

#include <iostream>
using namespace std;

int main(){

    int iUserTableInput = 0;
    int iUserInput = 0;
    
    cout << "Input how long of a multiplication table do you need > " << flush;
    cin >> iUserTableInput;
    
    cout << "Input which number you want to multiples of > " << flush;
    cin >> iUserInput;
    
    int iMultiplication[iUserTableInput + 1] = {}; 
    // +1 to make sure that there are enough memory allocated to array. 
    
    for (int iLoop = 1; iLoop <= iUserTableInput; iLoop++){
        iMultiplication[iLoop] = iUserInput * iLoop; 
        cout << iLoop << " times " << iUserInput << " is " << iMultiplication[iLoop] << endl;
    }

    
    return 0;
}

As seen on the little program above, my current lessons have been covering loops and how to implement them to a program. To make sure that I understand how each of them works I decided to look up the differences between for-, do while- and while-loops and even went, and couple draw flowcharts of loops to visualize what happens in different loops. I used application called Pencil to draw and the charts are presented below.

For- and while-loop are pretty much the same when put in a simplest flowchart form: For- and Do while-loops

Do while loop does it differently, since it runs the code always atleast once. Here it is in a flowchart: For- and Do while-loops

I also read about what are the major differences between each of these loops and when should programmer select which to use, and came to conclusion that it all depends on the circumstances. My favourite anecdote was that each of these are tools for different situation in the same way as the tools in carpenters toolbox. There are several ways to hammer down a nail, but the most elegant and easiest way to use the hammer rather than wrench. Some programmers may dislike do while loop because it creates confusion. After I have more experience in a matter, I think I will continue using each of the tools provided to find the best solution to each of problems presented to me.

This time I decided to list all the new things that I have learned since my last blog post and comment them a little bit. There haven’t been a lot of completely new things but I still think that using the beginners course as a refresher is a good idea. I have never felt bored, but rather intrigued to find out that I still can remember all the basics. When I decided to change my career path some of the people who I told about my next move told me that returning to something that I have already done before is like riding a bicycle after all these years. You never really forget it all. After almost couple of weeks of setting up development environment to myself and starting to write programs I kind of agree with that sentiment. Anyways, here are the things that were something new to me, or made me do additional digging around the Internet:

One last piece of new information was comparing floating point values and I learned that that it is like comparing height of two people, as in one person can very rarely be exactly same height with another person, even when they might look as tall. Example code below shows one way to compare floating point values.

float fValue1 = 1.1;
    if (fValue1 < 1.11){
        cout << "equals" << endl;
    }
    else{
        cout << "not equal" << endl;

Next up on the course is going to be more advanced programming such as object oriented coding, inheritance and pointers. Can’t hardly wait. Until next time!

-sorhanp