Introduction to Java
Java is a platform-independent and high-level programming language based on the concept of Object Oriented Programming. It is platform independent as it can run on any platform which are compatable with java. It is the nature of bytecode to be executed by the interpreter as it is a low level code and need to be execued by the interpreter on any machine supporting Java Virtual Machine.
Java is a high-level programming language as it is code written in a human-understandable form. The code in Java is written in such a way that it can be easily understandable by humans who have knowledge of Java, making Java a high-level programming language.
Basic Syntax in Java
Conditional Statement Syntax
Conditional Statements are the decision-making statements in Java. It executes the block of code that meets certain criteria. Java uses different conditional statements like if, nested if, if-else, nested if-else and Switch statement. The syntax and uses of different conditional statements in Java are given below.
1) if statement
if statement is conditional statement that executes a certain block of code if the condition is true. If the condition is true, it will execute the code inside if statement. Otherwise, if the condition is false in if statement, the instruction inside if statement will not be followed, and the program simply jumps to the instruction outside the if block. Given below is a syntax of if statement.
Syntax of if statement:
if(condition) {
// statement to execute if condition is true
}
Example of if statement
// Java program to illustrate the working of if conditional statement
import java.util.*;
class Tpoint {
public static void main(String args[])
{
int value = 20;
if(value > 15) { // this statement is besed upon condition
System.out.print(“ The condition is true”);
}
// this statement is always execute as it is outside the if statement
System.out.print(“ This statement will execute”);
} // end of the main function
} // closing of the class
Output:
The condition is true
This statement will execute
2) Nested if condition
Nested if means it contains multiple if statements or multiple if blocks. Each if statement holds different conditions. The code inside that if block will execute when the condition results true or when the condition meets the criteria for implementing the block of code. Given below is the syntax of a nested if statement.
Syntax:
if(condition 1) {
// this statement will execute if condition 1 is true
}
if(condition 2) {
// this statement will execute if condition 2 is true
}
if(condition 3) {
// this statement will execute if condition 3 is true
}
……
if(condition n) {
// this statement will execute if condition n is true
}
Example:
// Java program to demonstrate the working of nested-if condition
import java.util.*;
class Tpoint {
public static void main(String args[]) {
int a = 16
if( a > 20 || a == 20) {
System.out.println(“Value of a is greater than or Equal to 20”);
}
if( a > 30 || a == 30) {
System.out.println(“Value of a is greater than or Equal to 30”);
}
If( a > 15 || a == 15) {
System.out.println(“Value of a is greater than or Equal to 15”) ;
}
} // end of the main function
} // end of the class
Output:
Value of a is greater than or Equal to 15
3) if-else condition
The if-else statement is another conditional statement in Java in which the program first enters into if block and if the condition is true, it will execute the code inside the if block otherwise, the program enters into else block and executes the code written inside the else block. Given below are the syntax and example of if-else condition
Syntax:
if(condition) {
// this block will be executed if the condition results true
}
else {
this block will be executed if the condition is false
}
Example:
// Java program to demonstrate the working of if-else condition
import java.util.*; // to import java libraries
class Tpoint {
public static void main(String args[]) {
int check = 80;
if(check <= 100) {
System.out .print(“Value of check is less than or equal to 100”);
}
else {
System.out.println(“Value of check is greater than or equal to 100”);
}
} // end of the main function
} // end of the class
Output:
Value of check is less than or equal to 100
4) Nested if-else condition
Nested if-else condition is the conditional statement in Java where you have an if-else block inside another if or else block. It is used when you have to check another condition after checking the previous condition. Also, it is used when you have to execute a certain block of instructions after checking one or more conditions.
This task can be performed with the help of a nested if-else block. If all the conditions inside the multiple if blocks are true, the code will be executed written inside the multiple if blocks otherwise, the else condition will be followed. The syntax and example of a nested if-else condition are as follows:
Syntax:
if(condition 1) {
// code to execute if condition 1 is true
if(condition 2) {
// code to execute if condition 2 is true
}
else {
// code to execute if condition 2 is false
} }
else {
// code to execute if condition 1 is false
}
Example
// Java program to demonstrate the working of nested if-else condition
class trainPrice {
public static void main(String[] args) {
int passengerAge = 45;
String journey = “Bombay to Delhi”;
String trainClasss = “general”;
if( passengerAge >= 30 && trainClass = = “general”) {
if( journey == “Bombay to Delhi”) {
System.out.println(“Price of the ticket is Rs. 350”);
}
else if(journey == “Delhi to Varanasi”) {
System.out.println(“Price of the ticket is Rs. 450”);
}
}
else {
System.out.println(“Price of the ticket is 250”);
}
} }
Output:
Price of the ticket is Rs. 350
5) Switch statement
Switch statement is a conditional statement in which one condition is executed after checking multiple conditions. It consists of a different number of cases, each case holding a different condition. The case in a switch statement whose condition matches with an expression in the switch condition in programming, that case is executed, and the rest of the cases are not executed if there is a break statement after the matching case.
The break keyword is used to terminate the flow of the program after the matching case is found inside the switch block, so that the rest of the cases are not executed and returns the instruction written inside the matching case in the switch block. There is a default statement in a switch block that executes when none of the cases match the condition provided in the switch block under the switch keyword.
The keyword default is used to create the default statement in a switch block in Java. The Syntax and examples of the Switch statement in Java are as follows:
Syntax:
switch(expression) {
case value 1:
// code to execute
break;
case value 2:
// code to execute
break;
…………..
……………
default :
// code to execute if none of the cases matches with switch condition
// default statement code
} // end of the switch block
Example
// Java program to demonstrate the working of switch statement
import java.util.*;
class Main {
public static void main(String[] args) {
// Enter the grade of a student in semister exam
System.out.println(“Enter the grade of a student ”);
String grade = “B”;
switch(grade) {
case “A”:
System.out.println(“Student has scored the marks between 90 to 100”);
break;
case “B”:
System.out.println(“Student has scored the marks between 80 to 90”);
break;
case “C”:
System.out.println(“Student has scored the marks between 70 to 80”);
break;
case “D”:
System.out.println(“Student has scored the marks below 70”);
break;
default:
System.out.println(“You have entered an invalid marks”);
} // closing of the switch statement
} }
Output:
Student has scored the marks between 80 to 90
6 Ternary Operator
Ternary operator is a conditional statement that can also be used in place of an if-else statement in Java. It is a single-line conditional statement that improves code readability and also takes less space as compared to an if-else statement in Java. Given below is the Syntax of the ternary operator
Syntax:
Variable = expression 1 ? expression 2 : expression 3
Example:
// Java program to demonstrate the working of ternary operator
import java.util.*;
class tpointTech {
public static void main(String[] args) {
// program to print the maximum of two number
int num1 = 56, num2 = 63, max;
// maximum number between num1 and num2
max = num1 > num2 ? num1 : num2 ;
// result will be stored in max variable
System.out.println(“Maximum number is =” + max);
} // end of the main function
} // end of the class
Output:
Maximum number is 63
Conclusion
Java is a very popular programming language from the time it was developed, and it will be in demand in the future as well because of the various functionality and performance it provides, which can not be achieved by other programming languages. I hope this article has provided you with valuable information about the Java programming language and syntax for different conditional statements in Java.
If you are looking for more about these programming languages, I suggest you visit Tpoint Tech Website, where you can get all the valuable information and clear-cut concepts with examples and an online compiler as well for the programming languages.