Published 15 October 2018

Conditional and logical statement in Java

Conditional and logical statements in Java, what are they used for.

Conditional and logical statements in Java, what are they used for.

Conditional statement is a design of a programming language that provides execution of a certain command (set of commands), provided the truth of some logical expression, or execution of one of several commands (sets of commands) depending on the value of some expression.

A conditional statement is used in cases where the execution or failure of a certain set of commands must depend on the execution or failure of some condition.

Conditional statements if, if-else, switch-case.

Conditional statements if, if-else, switch-case.

There are three conditional statements in Java: if, if-else, switch-case.

The conditional if statement has the structure:

if (condition) statement;

If the Boolean expression (condition) in parentheses is true, then the statement or code block in curly braces {} after if is executed. If the Boolean expression (condition) in parentheses is false, nothing is done.

Example:

int a=10;

int b=20;

if (b>a) System.out.println("b>a");

The conditional if-else statement has a construct:

if (boolean_statement) {

//code block

}

else {

//code block

}

If the Boolean expression is true, then the operator or code block in curly braces {} after if is executed, if the Boolean expression is false, the code block that is in curly braces after the else keyword is executed.

Example:

int c=10;

int b=20;

if (c>b) {System.out.println("c>b");}

else {System.out.println("c<=b");}

The conditional switch-case operator is convenient when there are many options, and writing if-else for each of them is very time consuming.

The switch-case conditional statement has the following structure:

switch (expression) {

  case value1:

    //code block 1;

    break;

  case value2:

    //code block 2;

    break;

    ...

  case value N:

    //code block N;

    break;

  default:

    block N+1;

}

Example:

int myday = 7;

String dayStr;

switch (myday) {

   case 1:  dayStr = "Monday";

           break;

    case 2:  dayStr = "Tuesday";

             break;

   case 3:  dayStr = "Wednesday";

             break;

   case 4:  dayStr = "Thursday";

                break;

   case 5:  dayStr = "Friday";

             break;

   case 6:  dayStr = "Saturday";

             break;

   case 7:  dayStr = "Sunday";

             break;

       default: dayStr = "Error";

             break;

    }

System.out.print(dayStr);

}

Ternary operator

Ternary operator

The ternary operator uses three operands has a construct:

boolean_condition ? expression1 : expression2

If the boolean_condition is true, returns true, then the first expression to the left of the colon is computed, if false is returned - the second expression to the right of the colon.

Example:

int Num1 = 54;

int Num2 = 46;

int Num3 = Num1 < Num2 ? Num1 : Num2;

System.out.print(Num3);

Logical operators in Java

Logical operators in Java

Logical operators &, &&, |, ||, ^, !, &=, |=, ^=, ==, != are used to perform operations on expressions.

Example:

boolean x = true;

boolean z = false;

System.out.println("x && z = " + (x&&z));

System.out.println("x || z = " + (x||z));

System.out.println("!(x && z) = " + !(x && z));

Votes 0, average rating 0

Contents