Coding Basics: Loops

1K Blog Marathon: Day 54

When I was in elementary, our class is always punished for being noisy, despite being the first section. And as a classic punishment for noisy / late / absentee / not doing homework is by writing a sentence of apology repetitively on a one whole sheet of paper, back and forth.

“Hindi na po ako male-late sa klase.” (“I will not be late in classes.”)

“Gagawa na po ako ng assignment.” (“I will do my assignment.”)

Repetitively, on a one whole sheet. And if you dare do it again, the sheet is incremented:

Sheet++;

If only I knew programming during those days, if only I knew “Loops” during those days.

In programming, loop is a vital element. It allows the programmer to repeat a certain function or process as many as needed, or until a certain condition is met.

There are many forms of Looping. I wish I knew at least one of them back then. By the way, I’ll share you the different loops in programming. Enjoy!

For Loop

For loop is a flow by which a certain iteration or repetition is executed. This is the basic form of looping. It has 2 part:

  1. Header – where the iteration condition is set; and
  2. Body – where the process is residing, executed once per iteration.
For (variable; condition; increment)
{
    //Body
}

Example (Java):

// declares num as integer,
// execute if num is less than 10,
// and then increment num by 1
for (int num = 0; num<10; num++)
{
	//prints the variable
	System.out.println(num);
}

While Loop

This is a control flow that is similar to for, minus the increment. The declaration of variable is outside the block, while the increment is inside the while braces. While loop checks for the condition before executing the body.

Variable;
While (condition is true)
{
    //Body
    Increment;
}

Example (Java):

int num = 0;
while (num < 10)
{
	System.out.println(num);
	num++;
}

Do While Loop

Compared to While Loop, Do While Loop is different in execution timing. This loop “executes first the body, then proceed to check the condition”.

Variable;
Do
{
    //Body
    Increment;
}
While (condition is true)

Example (Java):

int num = 0;
do
{
	System.out.println(num);
	num++;
}
while (num < 10);
The importance of knowing how to correctly use the while loop ...
Source: https://i.redd.it/6wksqjmmyw321.jpg

Nested Loops

Nested loops are loops…just nested! These are more complex type of loops, but if you understand the basic loops, working with nested loops is sweet as fruit loops ^_^

Example (Java):

int num = 5;
int num2 = num;

while (num>0)
{
    while (num2>0)
    {
        System.out.print("*");
        num2--;
    }

    System.out.print("\n");
    num--;
    num2 = num;        
}

Infinite Loop

Oh no sir, you don’t want to mess with infinite loops – unless you want to give you officemate an “April Fool’s Day”. Infinite Loops are happening when semantic and logical errors are found on your loop, it may be lacking or misplaced increment / decrement.

Example (Java):

int num = 5;
while (num>0)
{
	System.out.println(num);
}
// notice that there is no decrement given for the num,
// so it will always be TRUE in the While condition,
// and since it will never equate to FALSE,
// it will go on a loop forever…
//
//
//or until the computer is switched off.

Looping is a skill in programming that you should learn if you want a more refactored codes. Instead of hard-coding hundreds of lines of repetitive codes, you can put it inside a loop and evaluate the condition. Just don’t forget to terminate it with a counter, or else…

Thank you folks and happy coding!

“And that’s one blog, stay hungry!”

Take your Zealots from the Earth. End your assault on my world. Never come back. Do it and I’ll break the loop.”

Dr. Stephen Strange

Published by Christian Foster

Code-blooded, coffee-lover, tall, dark and chubby. I love to draw, has motion-sickness and a sleepy-head. BTW, graduate of BS Computer Science, Associate in Computer Science and certified UiPath RPA Developer. Loyal to my partner and a father of a cute bouncing baby daughter!

2 thoughts on “Coding Basics: Loops

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: