Introduction
Java beginners have many questions related to the use of if-else statement and other decision making statements. There one of the main question is to know how an if-else statement work ?. Today I am providing a simple source code to make you understand how if-else statement work in Java. To illustrate this , I am explaining if-else through a program. Let us begin our example by simple program to check whether a given number is even or odd by applying if-else statement. This program also teaches us basic of array definition as well as use of modulus operator. Before moving to the source code and explanation of the code. I will give you some idea about basic elements used in this program.
Syntax :
if(boolean-expression)
{
if-code;
}
else
{
else-code;
}
rest-of-code;
if-else statement working
If the boolean-expression is true if-code gets executed. If boolean-expression is false else-code gets executed. In this decision making statements generally either of the block gets executed whether its if-code or else-code, after either execution rest-code gets executed. Using if-else statement to check whether a given number is even or odd public class EvenOrOdd { public static void main(String[ ] args) { int[ ] numbers = new int[ ]{11,23,34,45,56,67,78,89,90,104}; for(int i=0; i < numbers.length; i++){ if(numbers[i]%2 == 0) System.out.println(numbers[i] + " is even number."); else System.out.println(numbers[i] + " is odd number."); } } } A Closer Look at the Sample Program Let us analyze the code step-by-step, after having known few basic functionalities of the elements used. Line 2 to 3 :- Line 2 begins with a main method. Generally its a starting point for every Java program. JVM upon execution of program first looks for this method. Line 3 begins with definition and initialization of an array variable. We define an array variable by statement say in our case as : int [ ] numbers, here we have defined an int array which can hold int values with a index. When we write new operator we are creating an object. Here we are creating an object of an int array holding values such as 11, 23 , 35, 45 etc. Line 5 to 9 :- Line 5 begins with a for loop. Generally, we need a for loop to loop through array variables one by one because we need to check for the each element stored in array that whether its even or not. To loop through all the elements of an array we can use length variable defined for array object. length variable gives us the total length of an array. After looping through the array by the test condition applied on length variable we come across if-else statement on line 6. IF-ELSE statement applies a boolean expression to have a decision making flow between even or odd. Here we check in if statement that number retrieved in each for loop execution is even or not. If boolean expression is true, if body gets executed and number gets print stating its even or if statement boolean expression comes out to be false else body gets executed stating that number is odd integer. Testing Boolean Condition :- Modulus Operator is used to test the boolean condition. Generally Modulus Operator % performs division between two operands and returns their remainder. Here let say in our case if number in for loop come as 45. So it will divide 45 by 2 and return remainder which is 1. Generally this 1 indicates that 45 is odd. If the number return with remainder as 0 , it indicates that number is even. Thus based on above logic if-else statement gets the decision making.
Java beginners have many questions related to the use of if-else statement and other decision making statements. There one of the main question is to know how an if-else statement work ?. Today I am providing a simple source code to make you understand how if-else statement work in Java. To illustrate this , I am explaining if-else through a program. Let us begin our example by simple program to check whether a given number is even or odd by applying if-else statement. This program also teaches us basic of array definition as well as use of modulus operator. Before moving to the source code and explanation of the code. I will give you some idea about basic elements used in this program.
Syntax :
if(boolean-expression)
{
if-code;
}
else
{
else-code;
}
rest-of-code;
if-else statement flow chart
if-else statement working
If the boolean-expression is true if-code gets executed. If boolean-expression is false else-code gets executed. In this decision making statements generally either of the block gets executed whether its if-code or else-code, after either execution rest-code gets executed. Using if-else statement to check whether a given number is even or odd public class EvenOrOdd { public static void main(String[ ] args) { int[ ] numbers = new int[ ]{11,23,34,45,56,67,78,89,90,104}; for(int i=0; i < numbers.length; i++){ if(numbers[i]%2 == 0) System.out.println(numbers[i] + " is even number."); else System.out.println(numbers[i] + " is odd number."); } } } A Closer Look at the Sample Program Let us analyze the code step-by-step, after having known few basic functionalities of the elements used. Line 2 to 3 :- Line 2 begins with a main method. Generally its a starting point for every Java program. JVM upon execution of program first looks for this method. Line 3 begins with definition and initialization of an array variable. We define an array variable by statement say in our case as : int [ ] numbers, here we have defined an int array which can hold int values with a index. When we write new operator we are creating an object. Here we are creating an object of an int array holding values such as 11, 23 , 35, 45 etc. Line 5 to 9 :- Line 5 begins with a for loop. Generally, we need a for loop to loop through array variables one by one because we need to check for the each element stored in array that whether its even or not. To loop through all the elements of an array we can use length variable defined for array object. length variable gives us the total length of an array. After looping through the array by the test condition applied on length variable we come across if-else statement on line 6. IF-ELSE statement applies a boolean expression to have a decision making flow between even or odd. Here we check in if statement that number retrieved in each for loop execution is even or not. If boolean expression is true, if body gets executed and number gets print stating its even or if statement boolean expression comes out to be false else body gets executed stating that number is odd integer. Testing Boolean Condition :- Modulus Operator is used to test the boolean condition. Generally Modulus Operator % performs division between two operands and returns their remainder. Here let say in our case if number in for loop come as 45. So it will divide 45 by 2 and return remainder which is 1. Generally this 1 indicates that 45 is odd. If the number return with remainder as 0 , it indicates that number is even. Thus based on above logic if-else statement gets the decision making.