if statement
The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.Below is the structure of if statement:
if (condition) { // Do something here if 'condition' is true. }
The above image illustrates the flow of application when it meets a if statement.
- The program checks the condition.
- If the condition is true, code in the if block will be executed. It then continues executing the codes below theif block.
- If the condition is false, the program will ignores the code in the if block and execute code snippets below theif block.
Example: IfExample.java
package com.shishirkant.tutorial.javabasic.controlflow; public class IfExample { public static void main(String[] args) { // Declare a variable, representing your age int age = 30; System.out.println("Your age: " + age); // The condition to test is 'age> 20' if (age > 20) { System.out.println("Okey!"); System.out.println("Age is greater than 20"); } // The code after the 'if' block. System.out.println("Done!"); } }
Run the example:
if – else statement
The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
** if - else ** if( condition ) { // Do something here } // Else else { // Do something here }
Example: IfElseExample.java
package com.shishirkant.tutorial.javabasic.controlflow; public class IfElseExample { public static void main(String[] args) { // Declare a variable, representing your age int age = 15; System.out.println("Your age: " + age); // The condition to test is 'age> 18' if (age >= 18) { System.out.println("Okey!"); System.out.println("You are accepted!"); } else { System.out.println("Sorry!"); System.out.println("Age is less than 18"); } // The code after the 'if' block and 'else' block. System.out.println("Done!"); } }
Running the example:
if – else if – else statement
The structure of an if – else if – else statement is:
if(condition 1) { // Do something here } else if(condition 2) { // Do something here } else if(condition 3) { // Do something here } // Else else { // Do something here }
ElseIfExample1.java
package com.shishirkant.tutorial.javabasic.controlflow; public class ElseIfExample1 { public static void main(String[] args) { // Declaring a varible of type int (32 bit integer) // Represent your test scores. int score = 20; System.out.println("Your score =" + score); // If the score is less than 50 if (score < 50) { System.out.println("You are not pass"); } // Else if the score more than or equal to 50 and less than 80. else if (score >= 50 && score < 80) { System.out.println("You are pass"); } // Remaining cases (that is greater than or equal to 80) else { System.out.println("You are pass, good student!"); } } }
Boolean Value
boolean is a data type, it only has two values true or false.Create class BooleanExample:
BooleanExample.java
package com.shishirkant.tutorial.javabasic.controlflow; public class BooleanExample { public static void main(String[] args) { // Declare a variable of type boolean boolean value = true; // If value is true if (value == true) { System.out.println("It's true"); } // Else else { System.out.println("It's false"); } if (value) { System.out.println("It's true"); } // Else else { System.out.println("It's false"); } } }
Operators involved in conditional expression
This is a list of operators, commonly used in conditional expressions.
- > Greater Than
- < Less Than
- >= Greater Than or Equal To
- <= Less Than or Equal To
- && AND
- || OR
- == HAS A VALUE OF
- != Not Equal To
- ! NOT
Example:
package com.shishirkant.tutorial.javabasic.controlflow; public class ElseIfExample2 { public static void main(String[] args) { // Declare a variable of type int, represents your age. int age = 20; // Test if age less than or equal 17 if (age <= 17) { System.out.println("You are 17 or younger"); } // Test age equals 18 else if (age == 18) { System.out.println("You are 18 year old"); } // Test if age greater than 18 and less than 40 else if (age > 18 && age < 40) { System.out.println("You are between 19 and 39"); } // Remaining cases (Greater than or equal to 40) else { // Nested if statements // Test age not equals 50. if (age != 50) { System.out.println("You are not 50 year old"); } // Negative statements if (!(age == 50)) { System.out.println("You are not 50 year old"); } // If age is 60 or 70 if (age == 60 || age == 70) { System.out.println("You are 60 or 70 year old"); } } } }
You can change the value of “age” and rerun ElseIfExample2 class and see the results