Control Flow Looping by Using For

Assoc. Prof. Leon Abdillah

Control Flow Looping by Using For

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:

 

—Structure control, consists of:

  1. Sequence
  2. Selection/Choice/Decision (if..else.. dan switch..case)
  3. Loop/Iteration/Repetition (while, do..while, for..)

Syntax

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

—Its syntax can be expressed as:

for(initialization; Boolean_expression; update) {   //Statements

}

Or

for (initialization_expression ; loop_condition ; increment_expression) {
// statements

}

—

 

Java Code

package eLooping;

public class eLooping3ForSum {

public static void main(String[] args) {
// TODO Auto-generated method stub
int Jum = 0;
for(int i=1; i<=5; i++){
System.out.println(i);
Jum += i; // Jum = Jum + i;
}
System.out.println(“Jumlah = ” + Jum);
}

}

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 *