Control structure adalah sebuah stament yang eksekusinya dikontrol oleh control statement.

Selection statement mengartikan pilihan antara dua atau lebih statement yang akan dieksekusi. Terdapat dua kategori, yaitu: two way selectors dan multiple way selectors.

Contoh dari two way selectors adalah nesting selector. Contohnya dalam Java:

if (sum == 0)

if (count == 0)

result = 0;

else result = 1;

Contohnya dalam C, C++, dan C#:

if (sum == 0) {

if (count == 0)

result = 0;

}

else result = 1;

Contohnya dalam bahasa Ruby:

if sum == 0 then

if count == 0 then

result = 0

else

result = 1

end

end

Contohnya dalam bahasa Phyton:

if sum == 0 :

if count == 0 :

result = 0

else :

result = 1

Contoh dari multiple way selectors adalah switch() case dan if else if.

Contohnya dalam C, C++, Java, dan JavaScript:

  switch (expression) {

case const_expr1: stmt1;

case const_exprn: stmtn;

[default: stmtn+1]

}

Contohnya dalam bahasa Ruby:

leap = case

when year % 400 == 0 then true

when year % 100 == 0 then false

else year % 4 == 0

end

Contohnya dalam Phyton:

if count < 10 :

bag1 = True

elif count < 100 :

bag2 = True

elif count < 1000 :

bag3 = True

 

Iterative Statements

iterative statements adalah sebuah perulangan eksekusi dari suatu statement atau compound statement yang dilakukan oleh iterasi maupun rekursi.

Counter Controlled Loops

Counter Controlled Loops adalah sebuah perhitungan iterative statement yang memiliki loop variable, memiliki initial dan terminal yang jelas, dan stepsize value.

Logically Controlled Loops

Logically controlled loops adalah perulangan yang dikontrol berdasarkan dari Boolean expression. Logically controlled loops dibagi menjadi 2, yaitu: pre-test dan post-test. Contoh dari pre-test adalah operasi while, dan contoh dari post-test adalah do-while. Contoh dalam bahasa C dan C++:

  • while (control_expr)

loop body

  • do

loop body

while (control_expr)

Iteration Based on Data Structures

Contoh:

  • PHP

– current points at one element of the array

– next moves current to the next element

– reset moves current to the first element

  • Java 5.0 (uses for, although it is called foreach)

For arrays and any other class that implements the Iterable interface, e.g., ArrayList

for (String myElement : myList) { … }