Skip to main content

Control Statements Summary

Conditional Control Statements​

  1. Conditional control statements allow the program to select between alternative actions during program execution.
  2. The conditional part of an if statement must be of boolean type.
  3. Block notation is mandatory when an if block has more than one statement.
  4. Block notation is optional when an if block has only one statement.
  5. 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​

  1. 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)

  1. Case labels in a switch must be unique

  1. 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 default label 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 break is 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 values
  • break β†’ prevents fall-through
  • default β†’ 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 for statement.

28) Updating Multiple Variables

  • You can update multiple variables in the update section of a for loop.

29) Different Data Types (Declared Before Loop)

  • If variables are declared before the for loop, you can initialize multiple variables of different data types in the initialization part.

30) Condition Checking (for / while)

  • In for and while loops, 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-while loop:
    • The block executes first
    • Then the condition is checked

Break Statement


33) Definition​

  • break is 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​

  • break cannot be used without:
    • loop OR
    • switch

37) Labeled break​

  • Labeled break can be used without loop/switch block context directly, to exit labeled blocks

Continue Statement


38) Definition​

  • continue is a keyword
  • Used only inside looping statements

39) Purpose​

  • Skips current iteration and moves to next iteration

40) Behavior in for loop​

  • When continue is encountered: πŸ‘‰ Control goes to update statement

41) Behavior in while / do-while​

  • When continue is encountered: πŸ‘‰ Control goes to condition check

42) Types of continue​

continue;
continuelabel;

43) Usage Restriction​

  • Both continue and labeled continue: Cannot be used outside looping statements

Quick Summary

  • for / while β†’ check condition before execution
  • do-while β†’ executes at least once
  • break β†’ exit loop/switch
  • continue β†’ skip iteration
  • Labels β†’ allow control flow to jump out of nested blocks