While and do...while loops
Welcome to the introductory lesson on loops. At the end of this section, we'll have command over Java's while
and do...while
loops. I'm pretty excited about this because loops allow us to execute a block of Java code over and over again as many times as we see fit. This is a pretty cool step in our learning process because the ability to perform small tasks many times in rapid succession is one of the things that makes computers better at certain tasks than humans are:
- To begin this topic, let's create a new NetBeans project, enter the
main
method, and simply declare an integer and give it a value. We can choose any positive value. We're going to ask our program to print out the phraseHello World
a number of times equal to the value of our integer.
- To do this, we'll employ a
while
loop. Thewhile
loop syntax looks a lot like we're writing anif
statement. We begin with the reservedwhile
keyword and follow it by two parentheses; inside these, we're eventually going...