Session 6 (Control Structures Statement)
December 16th, 2016 - Posted by: vaniasalsabella in Uncategorized- Java
- Statement = perintah
- Looping = perintah berulang berurutan
- For
For (expression1; condition; expression2)
{statements}
- While
While (condition)
{statements}
- Do-While
Do {statements}
While (condition )
- Selection = menyeleksi perintah sesuai kondisi
- Switch case
Switch (variable){
Case a:
Statements;
Break;
Default:
Statements;
Break;
}
- If-else
If (condition)
{statement;}
Else if (condition)
{statement;}
Else (condition)
{statement;}
- Boolean
== equal
!= not equal
< less than
<= less than or equal with
> greater than
>= greater than or equal with
- C++
- Iterative
- Counter-control loop
For (expression1; condition; expression2)
{statements;}
- Pretest logically control loop
While (condition)
{statements;}
- Post test logically control loop
Do {statements;}
While (condition)
- Selection
- Multiple Ways
Switch (variable){
Case a:
Statements;
Break;
Default:
Statements;
Break;
}
- Two Ways
If (Boolean expression)
{statement;}
Else if (Boolean expression)
{statement;}
Else (Boolean expression)
{statement;}
- HTML (Javascript)
- Selection
- If-else
Example :
<html>
<body>
<script type=“text/javascript”>
var book=“maths”;
if(book==“history”){
document.write(“History book”);
}
else if (book==“maths”){
document.write(“Maths Book”);
}
else { document.write(“unknown book”);}
</script>
</body>
</html>
- Switch
Example :
<html>
<body>
<script type=”text/javascript”>
var grade=’A’;
document.write(“Entering switch block<br />”);
switch (grade)
{
case ‘A’: document.write(“Good job<br />”);
break;
case ‘B’: document.write(“Pretty good<br />”);
break;
case ‘C’: document.write(“Passed<br />”);
break;
case ‘D’: document.write(“Not so good<br />”);
break;
case ‘F’: document.write(“Failed<br />”);
break;
default: document.write(“Unknown grade<br />”)
}
document.write(“Exiting switch block”);
</script>
<p>Set the variable to different value and then try…</p>
</body>
</html>
- Iterative
- While
Example :
<html>
<body>
<script type=”text/javascript”>
var count = 0;
document.write(“Starting Loop “);
while (count < 10){
document.write(“Current Count : ” + count + “<br />”);
count++;
}
document.write(“Loop stopped!”);
</script>
<p>Set the variable to different value and then try…</p>
</body>
</html>
- Do – While
Example :
<html>
<body>
<script type=”text/javascript”>
var count = 0;
document.write(“Starting Loop” + “<br />”);
do{
document.write(“Current Count : ” + count + “<br />”);
count++;
}
while (count < 5);
document.write (“Loop stopped!”);
</script>
<p>Set the variable to different value and then try…</p>
</body>
</html>
- For
Example :
<html>
<body>
<script type=”text/javascript”>
var count;
document.write(“Starting Loop” + “<br />”);
for(count = 0; count < 10; count++){
document.write(“Current Count : ” + count );
document.write(“<br />”);
}
document.write(“Loop stopped!”);
</script>
<p>Set the variable to different value and then try…</p>
</body>
</html>