// example input
int day = 5;
/* dont forget to BREAK after each statement! otherwise
* all the statements below will be executed as well!
*/
switch (day) {
case 1:
System.out.println("monday");
break; // break after each case
case 2:
System.out.println("tuesday");
break; // break after each case
case 3:
System.out.println("wednesday");
break; // break after each case
case 4:
System.out.println("thursday");
break; // break after each case
case 5:
System.out.println("friday");
break; // break after each case
case 6: case: 7 // combine two cases
System.out.println("weekend!");
break; // break after each case
// DEFAULT will be run when no case is chosen! (similar to else)
default:
System.out.println("illegal input!");
// if default is last case no need to break
// add BREAK only when default is not last!
}
boolean condition = true;
while (condition == true) {
System.out.println("i will print this unless condition is met");
if (Math.random()<0.1) { // condition will be met with 10% probability
condition = false;
}
}
// int[]
int[] zahlen = {1,4,7,2};
for (int zahl : zahlen) {
System.out.println(zahl);
}
// possible also for other more complex datatypes like HashMap, ArrayList or Objects!
ArrayList<String> arrListStr = new ArrayList<String>();
arrListStr.add("first string");
arrListStr.add("second string");
arrListStr.add("third string");
for (String s : arrListStr) {
System.out.println(zahl);
}
// Loop1 is a label for the first loop
Loop1:
for (;;) { // for (;;){} is an endless loop, same as while(true)
for (;;) {
statement;
if (condition) {
break Loop1;
}
}
}