In this section, we will discuss conditional statements in Java. We briefly looked at control flow and if statement in “Our Second Java Program” chapter earlier in ICSE Computer Applications With BlueJ course. Now is the time to understand the conditional statements of Java in depth.
To recap, the order in which the statements of a program are executed is known as control flow. By default, the statements of a program are executed from top to bottom in order in which they are written. To solve any real-world problem through your program, you will need to alter the control flow. Programming languages provide statements to alter the control flow of the program and these statements are known as control statements.
In Java, control statements can be categorized in 3 categories:
- Selection — We use selection statements to choose different paths of execution based upon the outcome of an expression or the state of a variable.
- Iteration — We use iteration statements to execute a set of statements repeatedly until some end condition is met.
- Jump — Jump statements are used to transfer program control to specific points in the program.
Let’s look at each of them in detail starting with the selection statements.