Tuesday, April 4, 2017

And the Strip Club On Your Birthday...

I keep reading this concept on the internet that if you are learning to program, you should write about it. Write about your journey and your understanding. I think the whole point of doing that is to make sure that you understand what you've been learning so far, because the logic is you can't write something that you don't understand. 

All right. Challenge accepted. I'll kick my first post on programming by talking about "for loop" in Java in a way that is understandable by even non programmers. 

So generally speaking, the concept of for loop is to make the computer do the same task over and over again, for as long as it is required, without writing repetitive code. As the writer of the code, you get to decide when it starts, and when it ends.

This is a generalized understanding of a for loop syntax:

for (when it starts; when it ends; when it should happen again) task;

This is an example of how to write a real for loop syntax:


int i; 
for (i = 0; i < 4; i = i + 1) System.out.print("ha");

Now the code above will print this word: 


hahahaha

It's okay if you don't get it yet. Because now is where I'm gonna explain for loop with an analogy involving a strip club.

Okay. Before we write the code, it's better to really understand and imagine what we want the computer to do for us.

So, I want you to imagine this scenario.

1. You have a little brother. And on his 17th birthday, you take him to a strip club. 

2. In addition to that, you also promise that you will take him there once a year, that is, on his birthday.

3. But here's the catch, you will only do it for as long as he is not yet 23. (Because by then it has gotten pretty old and you both probably have found other interests.)

So how do you translate this scenario to a for loop? Here's how:


int myBrosAge; 
for (myBrosAge = 17; myBrosAge < 23; myBrosAge = myBrosAge + 1)   
        System.out.println("Taking him to a strip club now that he's " + myBrosAge);

The code above will print the following message:


Taking him to a strip club now that he's 17 
Taking him to a strip club now that he's 18 
Taking him to a strip club now that he's 19 
Taking him to a strip club now that he's 20 
Taking him to a strip club now that he's 21 
Taking him to a strip club now that he's 22

So every year you take him out to a strip club on his birthday.

Now, what if you are a much richer big sis or big bro? On his birthday, you can take him to a strip club, AND, give him 50 dollars.

Well, here's the modified for loop for such scenario:


int myBrosAge; 
int myMoneyInTheBank = 500; 
for (myBrosAge = 17; myBrosAge < 23; myBrosAge = myBrosAge + 1) {   
        System.out.print("Today my bro turns " + myBrosAge + ". I'm taking him to a strip club... "); 
        myMoneyInTheBank = myMoneyInTheBank - 50;  
        System.out.println("And give him 50... Now I have " + myMoneyInTheBank + " dollars left."); 
}

Which will print this message:


Today my bro turns 17. I'm taking him to a strip club... And give him 50... Now I have 450 dollars left. 
Today my bro turns 18. I'm taking him to a strip club... And give him 50... Now I have 400 dollars left. 
Today my bro turns 19. I'm taking him to a strip club... And give him 50... Now I have 350 dollars left. 
Today my bro turns 20. I'm taking him to a strip club... And give him 50... Now I have 300 dollars left. 
Today my bro turns 21. I'm taking him to a strip club... And give him 50... Now I have 250 dollars left. 
Today my bro turns 22. I'm taking him to a strip club... And give him 50... Now I have 200 dollars left.

Notice that when you have more than one task for the for loop, you must wrap it inside curly braces.

What's that? You also catch the print and println difference? Good catch. The difference between them is that "println" will start adding one line below it after it has finished printing, whereas "print" does not do this.

Okay, that's all for this episode, I hope you understand it pretty clearly (and become interested in programming?). Ask me if you've got any questions and I will answer as best as I can.