0% found this document useful (0 votes)
4 views

Java Control Flow Constructs

Uploaded by

Anilnailk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Control Flow Constructs

Uploaded by

Anilnailk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Control Flow Constructs in Java

1. Simple if Construct

Syntax:
if (condition) {
// statement(s)
}

Flowchart:
Start → [Condition true?] → Yes → Execute statement → End
↘ No → End

Example Code (5 lines):


public class Main {
public static void main(String[] args) {
int a = 10;
if (a > 5)
System.out.println("a is greater than 5");
}
}

Output:
a is greater than 5

2. if...else Construct

Syntax:
if (condition) {
// if block
} else {
// else block
}

Flowchart:
Start → [Condition true?] → Yes → If block → End
↘ No → Else block → End

Example Code (5 lines):


public class Main {
public static void main(String[] args) {
int a = 3;
if (a > 5)
System.out.println("a > 5");
else
System.out.println("a <= 5");
}
}

Output:
a <= 5

3. Nested if...else Construct

Syntax:
if (condition1) {
if (condition2) {
// code
} else {
// code
}
} else {
// code
}

Flowchart:
Start → [Condition1?] → Yes → [Condition2?] → Yes → Inner If block → End
↘ No → Inner Else block → End
↘ No → Outer Else block → End

Example Code (5 lines):


public class Main {
public static void main(String[] args) {
int a = 10;
if (a > 0)
if (a % 2 == 0)
System.out.println("Positive even");
else
System.out.println("Positive odd");
}
}

Output:
Positive even
4. else if Ladder Construct

Syntax:
if (cond1) {
// code
} else if (cond2) {
// code
} else {
// default
}

Flowchart:
Start → [Cond1?] → Yes → Block1 → End
↘ No → [Cond2?] → Yes → Block2 → End
↘ No → Else block → End

Example Code (5 lines):


public class Main {
public static void main(String[] args) {
int a = 0;
if (a > 0)
System.out.println("Positive");
else if (a < 0)
System.out.println("Negative");
else
System.out.println("Zero");
}
}

Output:
Zero

5. switch Construct

Syntax:
switch (expression) {
case val1:
// code
break;
default:
// default code
}
Flowchart:
Start → Evaluate expression
→ Match case1? → Yes → Case1 code → End
→ Match case2? → Yes → Case2 code → End
→ No match → Default code → End

Example Code (5 lines):


public class Main {
public static void main(String[] args) {
int day = 1;
switch(day) {
case 1: System.out.println("Monday"); break;
default: System.out.println("Other day");
}
}
}

Output:
Monday

6. Difference Between while and do...while


Feature while do...while

Condition Check Before the loop After the loop

Executes at least once No guarantee Yes, always once

Syntax while (cond) { } do { } while (cond);

Example (while):
public class Main {
public static void main(String[] args) {
int i = 5;
while (i < 5)
System.out.println("Hello");
}
}

Output:
(no output)
Example (do...while):
public class Main {
public static void main(String[] args) {
int i = 5;
do
System.out.println("Hello");
while (i < 5);
}
}

Output:
Hello

You might also like