Control Statements Summary
Conditional Control Statementsβ
- Conditional control statements allow the program to select between alternative actions during program execution.
- The conditional part of an if statement must be of boolean type.
- Block notation is mandatory when an if block has more than one statement.
- Block notation is optional when an if block has only one statement.
- When you declare any variable within a conditional block, that variable can be accessed only within that block. It cannot be accessed outside the block.
6) Initialization of Variables inside Conditional Statementsβ
When initializing an uninitialized variable inside conditional statements, consider the following:
β Case 1:β
int a;
int x=99;
if(x==99) {
a=88;
}elseif(x==10) {
a=77;
}else {
a=66;
}
System.out.println(a);
β Valid because all possible paths assign value to a
β Case 2:β
int a;
int x=99;
if(true) {
a=88;
}elseif(x==10) {
// not required
}else {
// not required
}
System.out.println(a);
β Valid because condition is always true β a is definitely initialized
Switch Statement Summaryβ
- The following types of values can be passed in
switch:
byte,short,char,intβ (up to Java 2)enumβ (from Java 5)Stringβ (from Java 7)
- Case labels in a switch must be unique
- Case label can be:
β Literalsβ
case9:
case'A':
β Final Variablesβ
final int A=99;
caseA:
β Constant Expressions (Literals / Final Variables)β
case10+20:
final int A=90;
final int B=9;
caseA+B:
π Quick Recap
ifβ decision making using boolean conditions- Variables inside blocks β scope limited
switchβ works with constant values only- Case labels β must be unique + constant
10) Case label canβt be the following:
Variableβ
int a=99;
casea:// NOT allowed
π Reason: a is not a constant, so it cannot be used in a case label.
Blank Final Variable (not initialized at declaration)β
final int A;
A=99;
caseA:// NOT allowed
π Reason: Even though it's final, it is not a compile-time constant.
Expression with Variables / Blank Final Variablesβ
int a=90;
int b=9;
casea+b:// NOT allowed
final int A;
A=90;
final int B=9;
caseA+B:// NOT allowed
π Reason: Expressions must be compile-time constants, not runtime values.
11) Case label must be within range of switch argument data type
π Example: If switch uses byte, case values must be within byte range.
12) Default label in switch
- Usually one
defaultlabel should be present. - But it is not mandatory.
13) Position of default label
- Usually placed at the end
- But it can be:
- Before all cases
- Between cases
14) Break statement in switch
- Usually required for every case
- But not mandatory
15) Missing break behavior
- If
breakis not present: π Execution falls through to next case
Looping Control Statements
16) Definitionβ
Looping control statements are also called Iteration statements
17) Purposeβ
Used to execute a block repeatedly
18) Conditionβ
Loop requires an expression that returns boolean
19) Block notationβ
Mandatory if loop contains more than one statement
20) Block notation optionalβ
If loop has only one statement
21) Variable scope in loopβ
- Variables declared inside loop: π Accessible only inside the loop
22) Initialization executionβ
- Executed only once (first time)
23) Condition & Updateβ
- Can execute multiple times
24) Variable inside loop blockβ
- Memory is:
- Allocated each time loop runs
- Deallocated after each iteration
25) Variable in initialization (for loop)β
- Memory allocated only once
- Reused in all iterations
26) Multiple variables in for loopβ
- You cannot declare variables of different types in initialization
Invalid:
for(int i=0,float j=0.0; ...)// NOT allowed
Quick Summary
switchβ only constant valuesbreakβ prevents fall-throughdefaultβ optional, flexible position- Loops β boolean condition required
- Variable scope β limited to block
27) Multiple Variables in for Loop
- You can declare and initialize multiple variables of the same type in the initialization part of a
forstatement.
28) Updating Multiple Variables
- You can update multiple variables in the update section of a
forloop.
29) Different Data Types (Declared Before Loop)
- If variables are declared before the
forloop, you can initialize multiple variables of different data types in the initialization part.
30) Condition Checking (for / while)
- In
forandwhileloops, the condition is checked before executing the block.
31) First Condition False Case
- If the condition is false in the first check, the loop body will not execute.
32) do-while Behavior
- In a
do-whileloop:- The block executes first
- Then the condition is checked
Break Statement
33) Definitionβ
breakis a keyword- It can be used in:
switch- Looping statements
34) Purposeβ
- Used to terminate execution of the current loop or switch
35) Types of breakβ
break;
breaklabel;
36) Usage Restrictionβ
breakcannot be used without:- loop OR
- switch
37) Labeled breakβ
- Labeled
breakcan be used without loop/switch block context directly, to exit labeled blocks
Continue Statement
38) Definitionβ
continueis a keyword- Used only inside looping statements
39) Purposeβ
- Skips current iteration and moves to next iteration
40) Behavior in for loopβ
- When
continueis encountered: π Control goes to update statement
41) Behavior in while / do-whileβ
- When
continueis encountered: π Control goes to condition check
42) Types of continueβ
continue;
continuelabel;
43) Usage Restrictionβ
- Both
continueand labeledcontinue: Cannot be used outside looping statements
Quick Summary
for / whileβ check condition before executiondo-whileβ executes at least oncebreakβ exit loop/switchcontinueβ skip iteration- Labels β allow control flow to jump out of nested blocks