Control Flow Looping by Using While

Assoc. Prof. Leon Abdillah

Control Flow Looping by Using While

Leon Andretti Abdillah

Introduction

—There may be a situation when we need to execute a block of code several number of times, and is often referred to as a “loop”. Other terminology could be “repetition” or “iteration”.

—Java has very flexible three looping mechanisms. You can use one of the following three loops:

1.while Loop

2.do…while Loop

3.for Loop

While

—A while loop is a control structure that allows you to repeat a task a certain number of times

—The while statement continually executes a block of statements while a particular condition is true.

—Its syntax can be expressed as:

—

while (boolean_expression) {

  statement(s)

}

or

while ( test ) {

  body;

}

or

while (true){

// your code goes here

}

Java Code

package eLooping;

public class eLooping1WhileNumbers {

public static void main(String[] args) {
// TODO Auto-generated method stub

int count = 1;
while (count <= 5) {
System.out.println(“Number : ” + count);
count++;
}
}

}

 

 

References

C. H. Chuan. Introduction to Java Programming (for Novices & First-Time Programmers).  Retrieved from http://www3.ntu.edu.sg/home/ehchua/programming/index.html

L. A. Abdillah. (2013). Algorithms & Programming. Available:http://blog.binadarma.ac.id/mleonaa/teaching/programming/algorithm-and-programming-2/

L. A. Abdillah. (2014). Data Structures & Algorithms. Available:http://blog.binadarma.ac.id/mleonaa/teaching/programming/data-structures/

L. A. Abdillah. (2016). Algorithms & Data Structures. Available: http://blog.binadarma.ac.id/mleonaa/teaching/programming/algorithms-and-data-structures/

—https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Leave a Reply

Your email address will not be published. Required fields are marked *