Unit1 Contro Statements
Unit1 Contro Statements
if(condition) {
statement 1; //executes when condition is true
}
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
2) if-else statement
• Syntax:
• if(condition) {
• statement 1; //executes when condition is true
• }
• else{
• statement 2; //executes when condition is false
• }
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
3) if-else-if ladder:
default:
System.out.println(num);
}
}
}
Output:
number is 2
Loop Statements
• for loop
• while loop
• do-while loop
Java for loop
• do
• {
• //statements
• } while (condition);
Calculation.java
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
System.out.println(i);
i = i + 2;
}while(i<=10);
}
}
Output:
Printing the list of first 10 even numbers 0 2 4 6 8 10
Jump Statements
if(j == 4) {
continue;
}
System.out.println(j);
}
}
}
}
Output:
012351235235