

Not every if statement must have an else statement.


Every else statement must be preceded by an if statement.Keep in mind the following rules of thumb when writing if/else statements: Suppose we want Karel to move if he can, and turn left otherwise. It is clear that the former is more concise and pragmatic. If it is Sunday, Monday, Wednesday, Friday, or Saturday, go off at 8 AM.Without using an "otherwise" clause, the logic follows: If it is Tuesday or Thursday, go off at 7 AM.Using an "otherwise" clause, the logic is as follows: Suppose we want the alarm to go off at 7 AM on Tuesday and Thursday, and 8 AM on all other days. However, when writing comprehensive programs, it may not be practical to account for every possible scenario. In this example, we could have executed the same logic using two if statements, as follows: The "otherwise" clause allows us to account for all possible scenarios. Consider the following pseudocode for an alarm clock function: Think of the else statement as the "otherwise" conditional. This is when the else statement becomes useful. When writing comprehensive conditionals, you will often want to have your program do something specifically when a particular if statement is evaluated as false. One of the driving forces behind writing conditionals is the need to vary the program's behavior based on certain conditions. Using java reflection we can inspect a class, interface, enum, get their structure, methods and fields information at runtime even though class is not accessible at compile time. Reflection in Java is one of the advance topic of core java. Top Down Design and Decomposition in Karel Java Reflection provides ability to inspect and modify the runtime behavior of application.
