Perintah Selection ada 3 jenis : If-Else, Switch-case dan Ternary Operator
1. Contoh IF-Else :
Syntax :
if (boolean expression) statement1;
else statement2;
atau
if (boolean expression){
statement1;
statement2; Block statement1
……
}
else {
statement3;
statement4; Block statement2
…
}
2. Contoh Switch-case :
Syntax:
switch (expression) {
case constant1 : statements1; break;
.
.
case constant2 : statements2; break;
default : statements;
}
3. Contoh Ternary Operator :
Syntax:
condition ? then-expression : else-expression
Using this operator, you can rewrite
if(a > b)
max_value = a;
else
max_value = b;
as
max_value = (a > b) ? a : b;
Perintah Repetition Repetition/looping ada 3 jenis, yaitu : for, while, do-while
1. Contoh lopping for :
Syntax :
for(exp1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
statement1;
statement2;
…….
}
exp1 : initialization
exp2 : conditional
exp3 : increment or decrement
exp1, exp2 and exp3 are optional
2. Contoh looping while :
Syntax :
while (exp) statements;
or:
while(exp){
statement1;
statement2;
…..
}
3. Contoh looping do-while :
Syntax :
do{
< statements >;
} while(exp);
Keep executing while exp is true
exp evaluation done after executing the statement(s
No comments:
Post a Comment