Control statements in Java determine the flow of execution in a program. They allow decisions, loops, and branching, making your programs dynamic and intelligent.
π§ Types of Control Statements
-
Decision-making statements –
if
,if-else
,switch
-
Looping statements –
for
,while
,do-while
-
Jumping statements –
break
,continue
,return
1️⃣ Decision-Making Statements
✅ if
Statement
Executes a block if a condition is true.
π if-else
Statement
Executes one of two blocks depending on the condition.
π if-else-if
Ladder
Used when you have multiple conditions.
π§ͺ switch
Statement
Selects one of many code blocks based on a variable’s value.
2️⃣ Looping Statements
π for
Loop
Used when the number of iterations is known.
π while
Loop
Used when the condition is checked before loop execution.
↩️ do-while
Loop
Executes the block at least once, then checks the condition.
3️⃣ Jumping Statements
⛔ break
Exits from a loop or switch
.
⏭️ continue
Skips the current iteration.
π return
Ends the current method and optionally returns a value.
π§ͺ Sample Program
π Summary Table
Statement | Purpose |
---|---|
if / else | Conditional execution |
switch | Multiple conditions |
for , while , do-while | Repeating actions |
break , continue | Controlling loop execution |
return | Exiting a method |