Conditions in java. In English

             CONDITIONS AND STATEMENTS


 Logical conditions

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to: a == b
  • Not Equal to: a != b
  • a OR b: a||b

If statement

The simplest expression for making decisions is the if clause in Java. It determines whether a given statement or block of statements will be performed or not; that is, whether a block of statements is executed if a given condition is met or not.

Syntax: -

if(condition) 
{
   // Statements to execute if
   // condition is true
}
Example: -
  1. public class Example {  
  2. public static void main(String[] args) {  
  3.     int age=25;  
  4.     if(age>20){  
  5.         System.out.print("Age is greater than 20");  
  6.     }  
  7. }  }  

Output - Age is greater than 20

Else statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax: -

if (condition) {
  // code if condition is true
} else {
  // code if condition is false
}

Example: -
  1. public class Example {  
  2. public static void main(String[] args) {   
  3.     int number=16;   
  4.     if(number%2==0){  
  5.         System.out.println("Even number");  
  6.     }else{  
  7.         System.out.println("Odd number");  
  8.     }  
  9. }  }

Output - Even number

Else-if statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax: -


if (condition1) { // block of code to be executed if condition1 is true 
} else if (condition2) { 
         //code if condition1 is false and condition2 is true
} else { 
      // block of code to be executed if the condition1 is false and condition2 is false }

Example: -
  1. int time = 23;
  2. if (time < 10) {
  3.   System.out.println("Good morning.");
  4. } else if (time < 18) {
  5.   System.out.println("Good day.");
  6. } else {
  7.   System.out.println("Good evening.");
  8.    }

Output -Good evening

Ternary operator

There are three components to the definition of ternary. There are three operands in the ternary operator (? :). Boolean expression evaluation is done with it. Which value is allocated to the variable is decided by the operator. Three operands can only be accepted by this one conditional operator. It can be applied in lieu of the if-else clause. It greatly simplifies, makes the code more understandable, and shortens it.

Syntax: -

variable = (condition) ? expression True : expression False;

Example: - 
  1. int time = 22;
  2. String result = (time<15)?"Good Evening!":"Good Night";
  3. System.out.println(result;)

Output -Good Night


switch statement
The switch statement is a multi-way branch statement.
To put it simply,the Java switch statement selects one sentence
to run based on certain factors.
It provides an easy way to dispatch execution to different
parts of code based on the value of the expression.
Basically, the expression can be a byte, short,
char, or int primitive data types. It basically tests the equality
of variables against multiple values.
A case can occur in any number of ways. simply enforcing the
condition check, but keep in mind that duplicate case/s values
are prohibited.
The data type of a case's value and the switch variable must
match.
A case's value needs to be literal or constant. There is no room
for variables.
To end a series of statements, the break statement is used inside
the switch.
The break statement is optional. If omitted, execution will
continue on into the next case.
The default statement may appear anywhere in the switch block
and is optional.

Syntax: -

          switch(expression)
          {
          case value1 :
          //body
          break; 
         case value2 :
         //body
         break; 
         default : 
         //body
         }

Example: -
  1. public class Example {
  2.     public static void main(String[] args)
  3.     {
  4.         int day = 5;
  5.         String dayString;
  6.         switch (day) 
  7.         case 1:
  8.             dayString = "Monday";
  9.             break;
  10.         case 2:
  11.             dayString = "Tuesday";
  12.             break;
  13.         case 3:
  14.             dayString = "Wednesday";
  15.             break;
  16.         case 4:
  17.             dayString = "Thursday";
  18.             break;
  19.         case 5:
  20.             dayString = "Friday";
  21.             break;
  22.         case 6:
  23.             dayString = "Saturday";
  24.             break;
  25.         case 7:
  26.             dayString = "Sunday";
  27.             break;
  28.         default:
  29.             dayString = "Invalid day";
  30.         }
  31.         System.out.println(dayString);
  32.    }

Output - Friday









Comments

Popular posts from this blog

Lambda expression in Java. In English

Loops in Java. In English