Basic operators, if-else statement, ternary statement and switch case in Java

Operators in java:-



    Java operators are symbols that perform operations on variables and values. They are used to manipulate data and perform various computations in a Java program.

    There are several types in java operators. Some of them are,
      • Arithmetic operators.
      • Relational operators.
      • Logical operators.
      • Bitwise operators.
      • Assignment operators.

Arithmetic operators:-

  • Addition (+) : Adds two operands.
  • Subtraction (-) : Subtracts the right operand from the left operand.
  • Multiplication (*) : Multiplies two operands.
  • Division (/) : Divides the left operand by the right operand.
  • Modulus (%) : Returns the remainder of the division of the left operand by the right operand.

Relational operators:-

  • Equal to (==) : Checks if two operands are equal.
  • Not equal to (!=) : Checks if two operands are not equal.
  • Greater than (>) : Checks if the left operand is greater than the right operand.
  • Less than (<) : Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=) : Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=) : Checks if the left operand is less than or equal to the right operand.

Logical operators:-

  • Logical AND (&&) : Returns true if both operands are true.
  • Logical OR (||) : Returns true if at least one of the operands is true.
  • Logical NOT (!) : Returns true if the operand is false and vice versa.


Bitwise operators:-

  • Bitwise AND (&) : Performs bitwise AND on each corresponding bit of the operands.
  • Bitwise OR (|) : Performs bitwise OR on each corresponding bit of the operands.
  • Bitwise XOR (^) : Performs bitwise XOR on each corresponding bit of the operands.
  • Bitwise NOT (~) : Flips the bits of the operand.
  • Left shift (<<) : Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
  • Right shift (>>) : Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
  • Unsigned right shift (>>>) : Shifts the bits of the left operand to the right, filling the leftmost bits with zero.

Bitwise operators:-

  • Assignment (=): Assigns the value of the right operand to the left operand.
  • Add and assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiply and assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  • Modulus and assign (%=): Computes the modulus of the left operand with the right operand and assigns the result to the left operand.

Statements in java:-

    There are many statements in java programming. In this blog we discuss about ,
      • if statement
      • if-else statement
      • if-else-if statement
      • Ternary statement
      • switch-case statement

if statement :-

    if statement is use to the specify a block of Java code to be executed if a condition istrue.


   
           Example:-



if-else statement :-

    We can also use an else statement to specify a block of code to be executed if the condition in the if statement is false:

 


           Example:-



if-else-if statement :-

    Its used to check multiple conditions. First if-else conditions are false , then this statement will execute.




           Example:-



Ternary statement :-

    The ternary operator is a concise way to express a conditional statement used in java. It is also known as the conditional operator.

    Here, condition is a boolean expression that evaluates to either true or false. If the condition is true, the value of expressionIfTrue is assigned to the variable; otherwise, the value of expressionIfFalse is assigned.



           Example:-




Switch-case statement :-

    The switch statement is used in Java to pick branches in multiple ways. It offers a clear method for dealing with a variable's or expression's multiple possible values. 

    break keyword:-  Java exits the switch block when it comes across the break keyword. This will put an end to case testing and additional code execution within the block. After a match is made and the task is completed, a break is in order. Further testing is not required. Because a break "ignores" the execution of the remaining code in the switch block, it can save a significant amount of execution time.

    default keyword:- The default case is optional and is executed if none of the specified case values match the value of the expression.

    One evaluation of the switch expression occurs. Every case's value and the expression's value are compared. The related block of code is run if there is a match.



           Example:-










Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points