Jump to content
kyathil

32 views

Good Day!

Been a bit of a hectic time for me lately, hence the lack of updates. Today we will talk about loops, if/else and a little bit about arrays. I am also planning on having my first coding stream on Sunday, 16th, starting at 7 pm CET. Expecting the lesson to be 1-2 hours or so, thought it might be a decent time for such things.

LOOPS, so many LOOPS!

Loops are a big part of programming languages and Java got them too, of course. There are mainly 3 different loops: the while-loop, for each-loop and for-loop. You will use either of these depending on what you need to loop over, I will show you how to accomplish the same programming task in all 3 loops with the help of an array of int. An array is basically a list of objects and in this case it will be a list of integers, aka numbers (remember that you cannot mix object types in regular arrays). 

loops.thumb.png.d8328c225014681e21ca8de92f304cba.png

In our example here we can see a couple of things. The object we are looping through is an array called... array, consisting of 9 objects (1,2,...9). The first loop we will talk about is the for-each loop. The for each loop will check a container, such as a list or an array, for certain elements (in this case it checks for objects of type int). Note that you cannot use the reference int i outside of the loop. This variable is only visible to this specific loop. In our code example the for each loop will check every element in the array to find elements of type int and then print this element to the console. The for each loop is especially good for looping through collections or arrays where you know the object type you are looking for, but don't know the length of the collection/array/container. Note that I have commented out two loops, which is useful to do for example while debugging in order to find where in the code a bug is hiding.

loops2.thumb.png.88f198f3e3bfbc04a7d1c746f3715058.png

Alright! Looping is so much fun! Our next loop is the classical for-loop. For-loops starts at  certain index, in this case the index int i is set to 0. In programming we count from 0 and not 1. After the index we have a condition to check, i < array.length which checks that the index i is less than the length of the array, and as long as our condition is true the index will increase with 1 (this can be decreasing with 1 or set to something else). For every index i in our array, we print out the object on this position to the console (array[1] would refer to the object on position 1 in the array which would be the number 2 in our example).

The last loop is the while-loop. Unlike the other loops, we are initializing a variable outside of the loop (see what happens if you put it inside the loop instead!). The while loop will continue as long as the condition in the while loop is true, in this case for as long as the variable i is less than the length of the array. After printing out the object, just like the for-loop, we increase i with 1.

All of these loops are doing the same thing in my little coding example and they can usually be used to perform the same job. Which loop you want to use depends on the situation! I do want to alert you about creating inifinity loops, which happens to the best of us. The infinity loops happen because the programmer forgot to add a statement which will break the loop and are very common bugs in programs.

IF/ELSE/ELSE IF

As a programmer you will use if/else/else if cases a lot. Basically what they are doing is that they will check if a condition is true, or another statement is true, the program will perform a certain task and if not it will do something else (or nothing at all). 

ifelse.thumb.png.629c39b534f41c7d26451b4364c8c7d5.png

In order to make our code a bit cleaner I removed 2 of the loops. Now, inside our for-loop we are checking a certain condition, array % 2 == 0. Wait... what? Why do we add a percentage after our array?!  And why == and not = ?? Well, I actually though this was a good time to show off the modulo operator and ==! Let us break this condition down. We have the object on position i in the array, modulo 2, and the condition is that the result should be equal to 0. The modulo operation finds the remainder after division of one number by another. So, the object to the left of % , array, is divided with by object to the right, 2. If there is no remainder of the operation, it will return 0... So 4 % 2 would return 0, but 5 % 2 would return 1 (5  = 4 + 1).

Great! Useful info! Now, what about ==? Well, in Java == is used to compare objects to check for equality (this is not always going to work though). This means that the object to the left of the == will be compared to the object to the right and if there is a match it will return true, else it returns false. In our code snippet we will get a number from our modulo operation, which is the object to the left, and this will be checked if it equals to 0. To write the whole operation in English: if the object on position i in the array is dividable by 2, then "I am even!" will be printed out to the console. Else, if this condition is false, "I am odd!" will be printed. We can see that it is correct through the prints in the console (1 is odd, 2 is even, 3 is odd, 4 is even... etc)

So... In a if/else case we check a certain condition and either perform operation X or Y depending on whether or not the condition returns true or false!

elseif.thumb.png.967580ceac9224e456a1cbf940ca354d.png

It is possible to build larger if/else cases, with more statements, by adding else if conditions too. These conditions always occur after the initial if-case and do not have any limit. You could, if you want, add 100 else if-cases in your program (not recommended though). The understanding here is that if the first condition returns false but the next condition is true, the program will perform the operation after the else if case. In our updated program we will first check if the object array is dividable is true and if not, we check if it is less than 3... Else we perform the last operation. We can see that the program works by observing the console. 1 is not dividable by 2, but is less than 3, and "I am less than 3" will be printed out to the console. 2 is also less than 3, but since the program checks for whether or not the object is dividable by 2 first, it will print out "I am even!" instead.

There is another conditional that is worth mentioning, just for you to remember the phrase: switch-cases. This can be useful to use instead of those 100 if else-cases in your program!

Up Next

This time I haven't decided on the topic of next post... Either more about operations, for example !=, ||, and &&, or starting with Android Studio. What would you like to see?

Also, cya on Sunday at www.twitch.tv/kyathil !

2305879_0.jpg



0 Comments


Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...