Structure |
Description |
Example |
If Else |
Selection statements that can check many different cases and tells the program to do a specific task. |
if (type == 0) {
System.out.println("Account Type = Checking");
}
else {
System.out.println ("Account Type = Savings");
} |
Switch |
Selection statements that can check many different cases and tells the program to do a specific task. |
switch(day){
case 1:
temp = "Sunday";
break;
case 2:
temp = "Monday";
break;
case 3:
temp = "Tuesday";
break
case 4:
temp = "Wednesday";
break;
case 5:
temp = "Thursday";
break
case 6:
temp = "Friday";
break;
case 7:
temp = "Saturday";
break
} |
For each loop |
a counting loop that repeats the same task over and over again x number of times. |
for(Account accounts: account){
accounts.print();
}
for(int i =1; i<= num; i++){
int temp = temp + 1;
System.out.println(temp);
} |
While loop |
A conditional loop that repeats the same task till a conditional statement is proved to be false |
Iterator<Account> it = account.iterator();
boolean found =false;
while (!found && it.hasNext()){
currentAccount = it.next();
if(currentAccount.getName() ==name){
found = true;
}
} |
Exceptions( try/catch ) |
It catches errors that might happen in a program |
try{
writer.write(text);
}
catch(IOException e){
System.out.println("Exception is: " + e);
} |