Control Statements in Java — Java Technical Interview Questions & Answers
Control Statements in Java
Control Statements in Java
- Control statements decide which lines run and how many times. Real programs are not a straight list — they react to marks, menus, and input.
- Three groups. Decision: if, else, switch pick a path. Loop: for, while, do-while repeat work. Jump: break stops a loop or switch; continue skips one round.
- Learn if and for first. Then switch, while, break, continue. A loop with no stop condition never ends — say that in the viva.
- Let's take this on the board with one tiny Main class — no extra files. Control Statements in Java — output: Pass. marks is 72, >= 40, so if runs. Change marks to 35 in your head — else prints Fail. Start from main, go line by line, and stop at each print. That output is the proof for Control Statements.
Technical Interview Tip: Say three groups: decision + loop + jump. One example each.
Code Examples for Control Statements in Java
Example
// Control statements overview
public class Main {
public static void main(String[] args) {
int marks = 72;
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}Control Statements in Java — output: Pass. marks is 72, >= 40, so if runs. Change marks to 35 in your head — else prints Fail.
How to Explain Control Statements in Java to an Interviewer
Name the three groups of control statements.
Decision Loop Jump
Give one example of each.
if / switch for / while break / continue