Friday, January 27, 2017

Learn Java for Beginner easily ! part 12 : While Loop

While loop is one of looping function in programming languange. What I mean by looping function is this function will keep run the program inside the brackets until the condition is fully required. The syntax of While loop  is "while ( ) { }". In programming world, there will be a lot of moment where you want to make a program that keep doing the same keyword but using a different value. This Loop function will make your work easier because you don't need to type the same keyword for a thousand times, you only need to put a condition inside the regular brackets about when to stop running the program inside the curly brackets. Now before I explain it to you guys further, I want you guys to take a look at the program below.




code :
========================================================================

    public class whileloop {


public static void main (String args[])    //Line 5
{
int banana = 0;

while (banana<10)                         //Line 9
{
System.out.println(banana);
banana++;                              //Line 12
}
}

}

========================================================================

Then as usually I want you guys to type in your program to your Text Editor (I really hope you guys are using Eclipse so that you guys can understand my explanation easier, Learn how to download Eclipse in Part 3 ) .Then run your program to check the results. I'm absolutely sure the output of the program is going to be look exactly like this picture below.

See ? It is a lot of easier than type the "System.out.println( );" keyword ten times.It will save you a bunch of time which is gonna make you more productive.Actually there is a lot more of these kind of looping statements that will be very useful when you have reached the advance level of programming. But in this tutorial I will make this one clear first.

So as usual I'm going to explain to you guys each lines on the program above. But I'm not really going to explain each lines because I'm pretty sure you guys already feel familiar with some lines that I have explained in the couple of tutorials before.

Line 9 :

This is the most important syntax in the program above. This while loop declared that It will run the programs inside the brackets while the value of the variable is less than 10. I will give you guys a scheme of this program so that you guys can understand it easier below



So from the scheme above we can conclude that the while loop keep doing the syntax inside the curly brackets as long as the value inside the regular brackets is required. The reason why is this program only printing the value until number 9 is because the condition inside the  regular brackets tells java that this looping has to be stopped if the variable's value has reached 9 (banana<10). I'm pretty sure that you guys have learn the less (<) and greater (>) sign in your school so I don't think I have to explain it anymore.


So that's it guys for now. Thank you guys for visiting my blog, hope you guys enjoy it and see you on the next tutorial. If you guys got something to be asked, please leave a comment below and thank you very much.

Greetings from Programmer !

No comments:

Post a Comment