0% found this document useful (0 votes)
24 views27 pages

Modul 1 - Flow Control & Looping

This document discusses various selection and looping statements in Java including if, if-else, else-if, switch, for, while, do-while loops. It provides syntax and examples for each statement. Special loop controls like break, continue and labels are also covered. Key statements allow conditionally executing code blocks based on boolean expressions or iterating code blocks a specified number of times.

Uploaded by

Thomas Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views27 pages

Modul 1 - Flow Control & Looping

This document discusses various selection and looping statements in Java including if, if-else, else-if, switch, for, while, do-while loops. It provides syntax and examples for each statement. Special loop controls like break, continue and labels are also covered. Key statements allow conditionally executing code blocks based on boolean expressions or iterating code blocks a specified number of times.

Uploaded by

Thomas Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

FLOW CONTROL &

LOOPING

Oleh : Thomas Brian, S.ST., M.Kom.


The Selection Statements
• if
• if-else
• else-if
• switch
if

if (ekspresi boolean) { if Y
pernyataan1; ekspresi = true

}
pernyataan2; N
pernyataan1

pernyataan2
public class If {
public static void main(String args[]) {
int bilangan = -5;
if (bilangan<0)
System.out.println(“Bilangan adalah negatif”);
}
}
if-else

if (ekspresi boolean) {
pernyataan1; Y if N
ekspresi = true
} else {
pernyataan2;
}
pernyataan1 pernyataan2
pernyataan3;

pernyataan3
If - else
• If() statement takes a boolean expression, not a numeric value.
• You cannot convert or cast boolean types and numeric types.
• If you have:
if (x) // x is int
use
if (x!=0)
public class IfElse {
public static void main(String args[]) {
int bilangan = -5;
if (bilangan<0)
System.out.println(“Bilangan adalah negatif”);
else
System.out.println(“Bilangan adalah positif”);
}
}
else-if N if
ekspresi1 = true

if (ekspresi boolean1) { Y
pernyataan1;
if
} else if (ekspresi boolean2) { Y N
pernyataan1
ekspresi2 = true
pernyataan2;
} else {
pernyataan3;
}
pernyataan2 pernyataan3
pernyataan4;

pernyataan4
switch (ekspresi) {
SWITCH N if
case konstanta1 : ekspresi = konstan1

pernyataan1;
break; Y
case konstanta1:
Y if N
pernyataan2; ekspresi = konstan2 pernyataan1
break;
default :
N if
pernyataan3; break
pernyataan2 pernyataan3
} Y
pernyataan4;
if N
break
pernyataan4
Y
switch(x)
• Variabel x harus bertipe byte, short, char, atau
int.
• Floating point, long, atau class references
(termasuk String) tidak diperbolehkan.
• Kedudukan statement pada default sama
dengan kedudukan else pada if-else.
public class Switch {
public static void main(String args[]) {
int i=2;
switch (i) {
case 1 : i+=3;
break;
case 2 : i+=5;
break;
default: i+=10;
}
System.out.println(i);
}
}
The Loop Statements
• The for() Loop
• The while() Loop
• The do – while() Loop
FOR Inisialisasi
for (inisialisasi; ekspresi boolean;
perubah) {
pernyataan; if N
} kondisi = true

pernyataan
for (inisialisasi; kondisi; perubah)

perubah
pernyataan
for
• Java programming language allows the comma separator in a for()
loop structure.
• Example:

for (i=0, j = 0; j<10; i++,


j++){}
for (int i=0; i<10; i++) {
System.out.println(“Hore !!”);
}

for (int i = 0; i < 10; i++) {


System.out.println("Are you finished yet?");
}
System.out.println("Finally!");
WHILE
while (ekspresi boolean) {
pernyataan;
}
if N
kondisi = true

while (kondisi) pernyataan

pernyataan
int i = 0;
while (i<10) {
System.out.println(“Hore !!”);
i++;
}

int i = 0;
while (i < 10) {
System.out.println("Are you finished yet?");
i++;
}
System.out.println("Done");
DO-WHILE
do {
pernyataan;
} while (ekspresi boolean);
pernyataan

if N
kondisi = true
pernyataan

while (kondisi)
int i = 0;
do {
System.out.println(“Hore !!”);
i++;
} while (i<10);

int i = 0;
do {
System.out.println("Are you finished yet?");
i++;
} while (i < 10);
System.out.println("Done");
Special Loop Control

• break [label];
• continue [label];
• label : statement; (statement ini berupa loop)
Special Loop Control
• break digunakan untuk keluar (“prematurely exit”)
dari switch statements, loop statements, dan labeled
blocks.
• continue digunakan untuk meneruskan (“skip over
and jump) ke akhir dari loop body, dan kembali ke loop
control statement.
• label digunakan untuk mengidentifikasi statement
lain dimana statement lain ini meminta supaya block
statement pada label ini dikerjakan.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy