Skip to main content

Conditional Control Statements

  • Conditional Control Statements are also called as decision-making statements or selection statements.
  • It allows the program to select between alternative actions during the program execution.
  • The execution will be based on the result of conditional expression.

9.1.1 if Statement

1. Simple If

if (condition) {
// Statements (IF BLOCK)
}

// Other Statements
It indicates whether IF BLOCK statements will be executed or not, as per the condition.

2. If-else Statement

if (condition) {
// Statements (IF BLOCK)
}else {
// Statements (ELSE BLOCK)
}
  • It indicates which block will be executed based on the condition.
  • If condition is true, IF block executes.
  • Otherwise, ELSE block executes.

3. If Else-if Statement

if (condition1) {
// Statements (Block 1)
}elseif (condition2) {
// Statements (Block 2)
}elseif (condition3) {
// Statements (Block 3)
}
...
else {
// Statements
}

Important Points

  • Condition can be any expression that returns a boolean value.
  • Block notation {} is mandatory when there are multiple statements.
  • Block notation is optional when there is only one statement.
  • Variables declared inside an if or else block are local to that block only and cannot be accessed outside.

Lab161.java

class Lab161 {

public static void main(String[] args) {
if (true) System.out.println("IF BLOCK");
System.out.println("Hello Guys");
}
}

Lab162.java

class Lab162 {

public static void main(String[] args) {
if (false) System.out.println("IF BLOCK");
System.out.println("Hello Guys");
}
}

Lab163.java

class Lab163 {

public static void main(String[] args) {
if (false) {
System.out.println("IF BLOCK");
System.out.println("Hello Guys");
}
}
}

Lab164.java

class Lab164 {

public static void main(String[] args) {
int a = 99;
if (a == 99) System.out.println("Value is 99");
System.out.println("Hello Guys");
}
}

Lab165.java

class Lab165 {

public static void main(String[] args) {
int a = 88;
if (a == 99) System.out.println("Value is 99");
System.out.println("Hello Guys");
}
}

Lab166.java

class Lab166 {

public static void main(String[] args) {
int a = 99;
if (a == 99) {
int b = 12;
System.out.println(b);
System.out.println(a);
}
System.out.println(a);
}
}

Lab167.java

class Lab167 {

public static void main(String[] args) {
int a = 99;
if (a == 99) {
int b = 12;
System.out.println(b);
System.out.println(a);
}
System.out.println(a);
System.out.println(b); // Error: b is not accessible here
}
}

Lab168.java

class Lab168 {

public static void main(String[] args) {
int a = 42;
if (a % 2 == 0) System.out.println("Value is EVEN");
else System.out.println("Value is ODD");
}
}

Key Learnings

  • if(true) → always executes IF block
  • if(false) → never executes IF block
  • Without {}, only one statement belongs to if
  • Variables inside {} block → block scope only
  • Accessing block variable outside → compile-time error
  • if-else helps in decision making (e.g., even/odd check)

Lab169.java

class Lab169 {
public static void main(String[] args) {
int a = 0;

if (a++==0)
System.out.println("Zero");
elseif (a++==1)
System.out.println("One");
else
System.out.println("Not Matching");

System.out.println(a);
}
}


Lab170.java

class Lab170 {
public static void main(String[] args) {
int a = 1;

if (++a==1)
System.out.println("One");
elseif (++a==2)
System.out.println("Two");
else
System.out.println("Not Matching");

System.out.println(a);
}
}


Lab171.java

class Lab171 {

public static void main(String[] args) {
if (0) System.out.println("IF BLOCK");
System.out.println("Hello Guys");
}
}

Lab172.java

class Lab172 {

public static void main(String[] args) {
int a = 10;
int b = 20;

if (a > b) System.out.println(
"Max value between " + a + " and " + b + " is " + a
);
else System.out.println(
"Max value between " + a + " and " + b + " is " + b
);
}
}

Lab173.java

class Lab173 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 15;
int max = 0;

if (a>b&&a>c)
max=a;
elseif (b>a&&b>c)
max=b;
else
max=c;

System.out.println("Max value is "+max);
}
}


🔑 Key Learnings

  • a++ → post-increment (value used first, then increment)
  • ++a → pre-increment (increment first, then use value)
  • if (0) → invalid (condition must be boolean in Java)
  • if-else-if ladder executes first matching condition only
  • Logical AND (&&) is used for multiple condition checks
  • Useful for finding max of numbers

2.9.1.2 switch Statement

  • Switch statement allows the program to select one action among multiple actions during program execution.

Syntax

switch (variable/expression/value) {

caseval1:
// Statements
break;

caseval2:
// Statements
break;

...

default:
// Statements
}

Key Points

  • First, the switch expression is evaluated.
  • Then it tries to find a matching case value.
  • If a matching case is found → corresponding statements are executed.
  • Otherwise → default block will be executed.

Allowed Data Types (up to Java 7)

✔ Allowed:

  • byte
  • short
  • char
  • int
  • enum (from Java 5)
  • String (from Java 7)

Not Allowed:

  • long
  • float
  • double
  • boolean

Lab174.java

class Lab174 {
public static void main(String[] args) {
int a = 3;

switch (a) {

case0:
System.out.println("Sunday");
break;

case1:
System.out.println("Monday");
break;

case2:
System.out.println("Tuesday");
break;

case3:
System.out.println("Wednesday");
break;

case4:
System.out.println("Thursday");
break;

case5:
System.out.println("Friday");
break;

case6:
System.out.println("Saturday");
break;

default:
System.out.println("Wrong Input");
}
}
}


Key Learnings

  • break is used to stop fall-through behavior
  • Without break, execution continues to next cases
  • default works like else block
  • Best used when checking multiple fixed values

Lab175.java

class Lab175 {

public static void main(String[] args) {
int a = 30;
switch (a) {
}

char ch = 'A';
switch (ch) {
}

byte by = 123;
switch (by) {
}

short sh = 234;
switch (sh) {
}
}
}

Lab176.java

class Lab176 {

public static void main(String[] args) {
long a = 30;
switch (a) {
} // Invalid

float f = 30.0F;
switch (f) {
} // Invalid

double d = 30.0;
switch (d) {
} // Invalid

boolean b = true;
switch (b) {
} // Invalid
}
}

Lab177.java

class Lab177 {
public static void main(String[] args) {

C oursec=Course.CORE;

switch (c) {
caseADV:
System.out.println("100% PLACEMENT GUARANTEE");
break;

caseCORE:
System.out.println("NO PLACEMENT GUARANTEE");
break;
}
}
}

enumCourse {
CORE,ADV
}


Lab178.java

class Lab178 {

public static void main(String[] args) {
String loc = "MKR";

switch (loc) {
case "BTM":
System.out.println("Branch Office");
break;
case "MKR":
System.out.println("Head Office");
break;
}
}
}

Lab179.java

class Lab179 {
public static void main(String[] args) {

int a = 98;

switch (a) {
case98:
System.out.println("Val - 98");
break;

case98:
System.out.println("Val - 98");
break;
}
}
}


Lab180.java

class Lab180 {
public static void main(String[] args) {

int a = 98;
int x = 96;

switch (a) {

case98:
System.out.println("Val - 98");
break;

casex:
System.out.println("Variable - x");
break;
}
}
}


Key Learnings from this Page

  • Allowed in switch: byte, short, char, int, enum, String
  • Not allowed: long, float, double, boolean
  • enum can be used in switch (Java 5+)
  • String can be used in switch (Java 7+)
  • Case labels must be:
    • constant values
    • unique (no duplicates)
  • Duplicate case → compile-time error
  • Variables (non-final) cannot be used in case

Lab181.java

class Lab181 {
public static void main(String[] args) {
byte b = 20;

switch (b) {
case10:
System.out.println("TEN");

case20:
System.out.println("TWENTY");
}
}
}


Lab182.java

class Lab182 {
public static void main(String[] args) {
int a = 45;
final int X = 20;

switch (a) {

case10:
System.out.println("TEN");

caseX:
System.out.println("TWENTY");

case'A':
System.out.println("CHAR - A");

case10+20+15:
System.out.println("CONSTANT EXPR");

default:
System.out.println("DEFAULT");
}
}
}


Lab183.java

class Lab183 {
public static void main(String[] args) {
int a = 10;
final int X = 20;

switch (a) {

case10:
System.out.println("TEN");
break;

caseX:
System.out.println("TWENTY");
break;

case'A':
System.out.println("CHAR - A");
break;

case10+20+15:
System.out.println("CONSTANT EXPR");
break;

default:
System.out.println("DEFAULT");
}
}
}


Lab184.java

class Lab184 {
public static void main(String[] args) {
int a = 23;

switch (a) {

case10:
System.out.println("TEN");
break;

default:
System.out.println("DEFAULT");
break;

case10+10:
System.out.println("TWENTY");
break;
}
}
}


Key Learnings from this Page

  • If break is not used, switch will fall through to next cases
  • final variables can be used in case labels
  • Case labels must be:
    • constant expressions
    • evaluated at compile time
  • Valid case labels:
    • literals (10)
    • characters ('A')
    • constant expressions (10 + 20 + 15)
    • final variables
  • default:
    • optional
    • can appear anywhere in switch

Lab185.java

class Lab185 {

public static void main(String[] args) {
char ch = 'A';

if (ch >= 65 && ch <= 90) {
switch (ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println("Character is Vowel");
break;
default:
System.out.println("Character is Consonant");
}
} else {
System.out.println("Invalid Alphabet");
}
}
}

Key Concept from This Program

✔ Checking Character Type

  • ch >= 65 && ch <= 90 → Ensures character is uppercase alphabet (A–Z) using ASCII values