CONTROL STATEMENTS
IF STATEMENT |
The java if statement is used to test the condition. it checks boolean condition true or false.
There are various types of if statement in java
- if statement
- if-else statement
- if else-if ladder
- nested if statement
The java if statement tests the condition. if executes the if block it condition is true
SYNTAX:
if(condition) {
}
EXAMPLE:
package mypackage;
public class Ifcondition {
public static void main(String[] args) {
int a, b;
a=20;
b=29;
if (a>18) {
System.out.println("Age is greater then 18");
}
}
}
OUTPUT:
Age is greater then 18
2. IF-ELSE STATEMENT
The java if-else statement also tests the condition. it executes the if block if condition is true other wise else block is executed
SYNTAX:
if(Condition) {
}else {
}
EXAMPLE:
package myPackage;
public class IfelseCondittion {
public static void main(String[] args) {
String actual="ABCD";
String expected="ABCD123";
boolean status=actual.equals(expected);
if(status) {
System.out.println("THIS IS SUCCESS");
} else {
System.out.println("THIS IS FAILURE");
}
}
}
CONSOLE OUTPUT:
THIS IS FAILURE
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) {
int a , b;
a=5;
b=5;
if (a == b) {
System.out.println("pass");
} else {
System.out.println("fail");
}
}
}
CONSOLE OUTPUT:
PASS
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) {
int Telugu, English;
Telugu=50;
English=59;
if (Telugu > 35 || English > 35) {
System.out.println("pass");
} else {
System.out.println("fail");
}
}
}
CONSOLE OUTPUT:
PASS
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) {
int Telugu, English;
Telugu=50;
English=29;
if (Telugu > 35 && English > 35) {
System.out.println("pass");
} else {
System.out.println("fail");
}
}
}
OUTPUT:
fail
USING TERNARY OPERATOR
We can also use ternary operator ( ?, : ) to perform the task of if - else condition it is a short hand way to check the condition if the condition is true the result of ? is returned but it the condition is false the result of : is returned.
package mypackage;
public class Ifelsecondition {
public static void main(String[] args) {
int number=13;
String output=(number%2==0)?"Even Number":"Odd Number";
System.out.println(output);
}
}
OUTPUT
Odd Number
SWITCH STATEMENT |
- Java switch statement executes one statement from multiple conditions. It is like if-else-ladder statement.
- The switch statement works with byte, short, int, long, enum types, string and some wrapper types.
- Since java 7 you can use strings in the switch statement
- There are one or N number of case values for a switch expression
- The case value must be of switch expression type only.
- the values must be unique.
- Each case statement can have a break statement which is optional.
- The case value can have a default label which is optional
FOR LOOP |
TABLES
package hellojava;
public class Myclass {
public static void main(String[] args) {
int a = 10;
for (int i = 0; i < 10; i++) {
System.out.println(a +" x " + (i+1) + " = " + (a*(i+1)));
}
}
}
OUTPUT:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100