KEMBAR78
Java Control Structures Assignment | PDF
0% found this document useful (0 votes)
12 views3 pages

Java Control Structures Assignment

Uploaded by

ktsreenidhi06
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)
12 views3 pages

Java Control Structures Assignment

Uploaded by

ktsreenidhi06
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/ 3

Syntax and Examples

1. if Statement
Syntax:

if (condition) {
// code to run if condition is true
}

Example:

public class IfExample {


public static void main(String[] args) {
int age = 20;

if (age >= 18) {


System.out.println("You are an adult.");
}
}
}

2. while Loop
Syntax:

while (condition) {
// code to repeat while condition is true
}

Example:

public class WhileExample {


public static void main(String[] args) {
int count = 1;

while (count <= 5) {


System.out.println("Count is: " + count);
count++;
}
}
}

3. do-while Loop
Syntax:

do {
// code to run at least once
} while (condition);

Example:

public class DoWhileExample {


public c static void main(String[] args) {
int number = 1;

do {
System.out.println("Number is: " + number);
number++;
} while (number <= 3);
}
}

4. for Loop
Syntax:

for (initialization; condition; update) {


// code to run in loop
}

Example:

public class ForExample {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("i is: " + i);
}
}
}
5. switch Statement
Syntax:

switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}

Example:

public class SwitchExample {


public static void main(String[] args) {
int day = 3;

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Another day");
}
}
}

You might also like