ROAD TO ANDROID DEVELOPER-Season 1-JAVA-Looping Statements

Tharani balan SK
6 min readJan 16, 2024

--

Hi all, I am back again with a new article in the Android series, we are going to see about Looping statements. If you haven’t checked my previous article on Conditional statements, Please, I urge you to check it out before coming here especially if you are new to programming. I have provided the link at the end of this article, check it out.

Looping statements, like conditional statements, are another significant aspect of any programming language. Whenever I come across loops in code, I used to think
Thank God !! What would humans do if loops never existed in coding?
coz believe me loops make your job a lot easier!!
When you hear the word ‘loop’ what comes to your mind? Something spiral? something cycling back? something going round and round? It’s exactly what you think it is!!! In programming, a task that needs to be done continuously again and again until a specific amount of time is usually written inside a looping statement so that you don’t need to code the same task again and again, saving a lot of time.

Feeling lost? XD Don’t fret, I will explain to you with an example…Mmm…ok let’s take our Lost Keys example from my previous article (Those who are new, here’s the gist of that example : In our last article we were searching for our lost keys in our cupboard, then in our computer desk and then in our pockets using conditional statements ) Let’s modify that example a little bit how about let’s say we have found our keys but our room has 5 doors and among them, only one of them leads to the world outside, Now what will you do? You loop through right? you just try your keys on the five doors i.e. you are repeating the same task five different times to check which of the door locks matches with your keys.

Looping statements are used to make this kind of repetitive task easier. Ok…Now let’s talk about the different types of looping statements…

  • WHILE LOOP : The basic idea behind a “while” loop is to repeat a block of code as long as a given condition remains true. For instance, let’s say you want to print numbers from 1 to 10, there are many ways to do it either you can write println statement 10 times or you can write it just a single time inside a loop….OR….. LOL (Actually there are still other ways to do it but they are new concepts that we will see in further articles still looping is the easiest way to do this the rest of it is like making this easier thing complex).
int count = 1;

while (count <= 10) {
System.out.println(count);
count++;
}
//The meaning of the above loop is till the count variable reaches from 1 to 10
//print the count numbers i.e. 1 to 10. This loop runs 10 times.

// Initially the value stored in count is 1
// next the while statement checks if the value in count is less than or equal to
// 10, yes it indeed is less than 10 so now it enters the loop
// 1st loop : prints count value which is 1 and then increments count so now
// current count value is 2. Then it checks if count is less than 10, yes 2 is
//indeed less than 10, so now...
// 2nd Loop : print count value which is 2 then increment count to 3 and then
// again checks if count is less than 10.....and so on....
//.....
// 9th Loop : print count value 9 then increment count to 10 then checks if
// 10<=10, yes 10 equals 10 so.....
//10th Loop : print count value 10 then increments count to 11 then checks if
// 11<=10, which is false so the execution stops.
  • DO-WHILE LOOP : This type of loop is exactly like what the name suggests, Do something until a specific condition is reached. This type of loop makes the best use of user interaction. Confused? Let’s code a simple number game
import java.util.Scanner;

Scanner scanner = new Scanner(System.in); // I know we haven't covered this yet
// For now, just know that scanner is used for getting
//input from the users
int userInput;

do {
System.out.println("Enter a number:");
userInput = scanner.nextInt(); //Asks for a number to be entered

if (userInput%2 == 0) { //checks if the entered number is even
System.out.println("Try again! Please enter a Odd number.");
// If entered number is even it prints above statement
}
} while (userInput%2 != 0); //this loop continues until the user enters a number,
//which is an odd number

System.out.println("Well done! " + userInput + " is a odd number.");
// then it prints the above statement

What the above code does is that it asks for the user to enter any number let’s say we enter 2, it checks if 2 is even or odd. It is even so it asks us to enter an odd number and the loop continues and again asks us to enter a number, this time lets say we enter 3 which is odd, so it comes out of the loop and prints well done ! 3 is a odd number.

I know what you are all thinking Forget the looping, just tell me how to ask input from the user? how that works? lol sorry mate I hear you there are some minor stuffs like this which we will talk about in my next article. So until then there is 1 more important loop left out which is…..

  • FOR LOOP : This loop is basically like a cutdown version of while loop. It does more or less the same thing a while loop does but the code is still more smaller. Let’s take the same example of printing numbers from 1 to 10 which we saw in while loop.
for (int count= 1; count<= 10; count++) {
System.out.println(count);
}
// Looks simpler than in a while loop right? if you observe closely there are
//three different statements combined in a single line let's break it out
// This line for(int count=1; count<=10; count++) has 3 statements :
// 1st the initialization : int count=1; i.e. storing 1 in variable count.
// 2nd the condition : count <= 10; checks if count value <= 10, If true, goes
//inside loop and executes statement inside it and then...
// 3rd Incrementing : count++ ; after each loop count value increments
//the overall working of this loop is similar to while loop
// compare while loop with for loop and try to understand the above code yourself.
// That's right!! I can't spoon feed you I have given you everything, Be a Man!!
// LMAO !!

That’s the stuff there is to know about looping statements. The examples I am explaining to you in each article are just simpler ones just practice them and then search on Google like coding problems on looping statements…., coding problems on conditional statements and just work them out even if you don’t know don’t worry see the answer and learn how they are using the concepts to come to a solution to a problem. If you go and search the same concepts that I have explained on google, you will find tons and tons of articles already written on them….just open and read few of them cause they might have explained something extra, something which I haven’t. There is something known as nested ifs in conditional statements which I haven’t touched upon why? well….you know nested if is nothing but an if statement inside another if statement why haven’t explained it is there are different ways to use a concept in programming and if you just know about if statement, with a creative mindset you can write a nested if on your own right? LOOK…I am telling you guys again

THERE IS NO ONE PREDEFINED WAY IN PROGRAMMING YOU HAVE TO BE CREATIVE IN YOUR THOUGHTS AND LOGICAL IN YOUR APPROACH THAT’S WHAT IS IMPORTANT AND ONLY BY PRACTICE, YOU CAN BUILD THEM.

Sorry to have bored you with my lecture lol
So this pretty much sums up the Looping statements, hope it will be useful for you guys. Until we meet again in my next article, CIAO !! :)

If you missed the previous article on Java Conditional Statements, check out the link below

https://medium.com/@TechTiger/road-to-android-developer-season-1-java-conditional-statements-af60a223d951

In caseof any queries, reach out to me on,

Email : sktharanibalan@gmail.com
Instagram : name_is_tharani_

Disclaimer : The technical stuff I am mentioning in this series might be factually wrong at some places or it might be short on content so I would recommend reading my stuff just as a refresher or for getting started as a beginner. For more detailed stuff follow other sites out there on the internet. Thank you

--

--

Tharani balan SK

programmer, App developer,Writer and Cybersecurity enthusiast