Friday, July 19, 2024

Increment and Decrement Operators In Java

 

Increment Operators:

Increment operators are used to increase the value of a variable by 1.

Pre-Increment Operator:

The pre-increment operator (++var) increments the value of the variable before using it in an expression.

       ++ var;

Post-Increment Operator:

The post-increment operator (var++) increments the value of the variable after using it in an expression.

    variable ++;

package OperatorsBasics;

public class IncrementOperator1{

               public static void main(String[] args)

               {

                              int a = 2;

                              // Pre-Increment

                               ++ a;// pre  Increment operator

                               // a =  a+1

                               //   2+1

                               //     3

                               // a = 3

                               System.out.println("a="+ a);

                               // a=3

                               

                                a= 3;

                               a ++;//   post increment

                               //  a =  a + 1

                               //        3 + 1

                               //         4

                               //  a = 4

                               System.out.println("a="+ a);

                               //                                                                       a= 4                     

               }

}

 

package OperatorsBasics;

public class IncrementOperator2 {

               public static void main(String[] args)

               {

                              int a = 2;                            

                               System.out.println(++a);// PreIncrement

                               //             1st Increment value  +   displays the variable value

                               //             a= a+1

                               //                2 +1

                               //               a=   3

                               //                   3

                               

                               a = 4;

                               System.out.println(a++);//post Increment

                               //       displays variable value  +  increment value later

                               //          a = 4                      a=  a+1 =  4+1 =5

                               //                 4 (displays)

                               

                                a= 5;

                                System.out.println(++a);// Pre-Increment

                                //        1st increment value  +   later displays variable value

                                //       a= a+1

                                //           5+1

                                //         a=    6                     displays 6

                               

                                a = 4;

                              System.out.println(++a);// Pre-Increment

                              //   increment value + later display variable value

                              //    a+1

                              //   4+1

                              //   a= 5              displays 5

 

               }

}

Decrement Operators:

Decrement operators are used to decrease the value of a variable by 1.

Pre-Decrement Operator:

The pre-decrement operator (--var) decrements the value of the variable before using it in an expression.

   -- var;  PreDecrement

   -- a;

 

Post-Decrement Operator:

The post-decrement operator (var--) decrements the value of the variable after using it in an expression.

   variable --;

  a --;

package OperatorsBasics;

public class DecementOperator {

               public static void main(String[] args)

               {

                              int a = 3;                            

                               --a;// Pre-Decrement

                               //   a =  a -1

                               //        3 -1

                               //        2

                               //  a =2

                               System.out.println("a="+a);//

                               //                      a= 2

                               

                                a=  4;

                               a --;//  post decrement

                               //  a=  a - 1

                               //       4 - 1

                               //          3

                               //    a =3

                               System.out.println("a="+a);//     a= 3     

 

               }

}

 

package OperatorsBasics;

public class DecementOperator2 {

               public static void main(String[] args)

               {

                              int a = 3;                            

                              System.out.println(--a);//              pre decrement

                              //   1st perform decrement  +   display variable value

                              //     a-1

                              //    3-1

                              //   a= 2                      displays 2

                             

                              a= 5;

                              System.out.println(--a);//

                              //   1st perform decrement  +   display variable value

                              //      a =  a -1

                              //           5-1

                              //          a= 4    displays 4

                             

                               a=  6;

                               System.out.println(a--);//

                               //    display variable value  , +    perform Decrement

                               //           displays 6             a= a-1

                               //                                                                                                                                   6-1

                               //                                                                                                                                                  a=           5

                               

                               System.out.println("a="+a);//  a =5

                               

                                a =7;

                               System.out.println(a--);// 7

                               //      display variable value   +    perform Decrement

                               //       displays 7              a=  a-1

                               //                                   7-1

                               //                                      6                            

               }

}

Write o/p for the below program?

package OperatorsBasics;

public class USTWrittemExam {

               public static void main(String[] args) {

                              int x =5;              

                              System.out.println(x++);//

                              // displays variable value +   increment

                              //     dispays 5             x +1

                              //                           5+1

                              //                                                                                                      x  =6

                             

                              System.out.println(++x);//

                              //   performs Increment operation +   displays variable value

                              //     x +1

                              //     6 +1

                              //  x =  7           displays 7

                             

                              System.out.println(x--);//

                              //  displays variable value  +   decrement

                              //      dispalys 7            x = x-1

                              //                                 7-1

                              //                                 x=  6

                             

                              System.out.println(--x);  //

                              //        1st decrement  +  display variable value

                              //          x = x-1

                              //              6-1

                              //             5

                              //     x= 5            display  5

               }

}

Operators


Operators:

  1. Arithmetic operators: +, -, *, /, %
  2. Relational operators / Comparison operators
  3. Compound operators
  4. Logical operators: && (and), || (or), ! (not)
  5. Increment operators: ++ (pre-increment, post-increment)
  6. Decrement operators: -- (pre-decrement, post-decrement)

1. Arithmetic Operators:

·  Operators: +, -, *, /, %

·  Purpose: Used to perform arithmetic operations such as addition, subtraction, multiplication, and division.

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division operator - gives quotient value
  • % : Remainder operator - gives remainder value

·  / : Divides the first number by the second and returns the quotient.

  • ·  % : Divides the first number by the second and returns the remainder.

 

 

  2 + 3

  3 - 2

  3 * 2

  3 / 2  = Division operator - gives Quotient val =1

  3 % 2 =  Remainder operator --gives  Remainder val =1

 

package OperatorsBasics;

 

import com.sun.tools.sjavac.server.SysInfo;

 

public class ArithematicOperators {

 

               public static void main(String[] args) {

 

                              //                           Arithematic :        + - * / %

                              //                               2 nos

                              //Declare 2 variables a,b and store 3,2 values

                              int a,b;

                              a = 3;

                              b =2;

 

                              // Declare add var

                              int add;

 

                              // Declare add variable and perform addition of a and b, then store the result into variable 'add'

                              add = a +b;

 

                              //            =   3 +  2

 

                              // add  =  5    rt side exp is evaluated  and result will be stored in left side variable i.e add

                              //   so add contains 5 val

                              // display result

                              System.out.println("add ="+add);

                              //                                    add =  5

                              // Declare sub variable  and perform substraction

                              int sub;

                              sub = a-b;

                              //    3 - 2

                              //    =1

                              //   sub =   1

 

                              // Display sub o/p

                              System.out.println("sub="+ sub);

 

                              //                         sub=        1

 

                              //                           HW   Perform Multiplication Operation of 2 no's?

              

// Division operator - gives quotient value

                              int div =  a/b;

                              //          3/2   // Quotient = 1        

                              //  

                              // display division

                              System.out.println("div="+div);

                              //                  "div=" + 1

                              //                     div=1

 

                              //                           3 /2 -  Quotient val

 

                              //                              2) 3 (1   -(Quotient val)

                              //                                 2                        

                              //                           ---------------

                              //                                 1   (Remainder)

 

 

                              //                           % - Remainder operator

                              int rem =  a % b;

                              // rem =   3%2;

                              //   rem=    1

                              System.out.println("Rem="+ rem);

                              // rem val=1

 

                              //                             5/ 2  =     quotient val

                              //                           5 % 2  =       Remainder val

 

                              //                              2 ) 5 (2  Quotient val)

                              //                                  4

                              //                           ------------------------

                              //                                             1 (remainder val)

 

                              System.out.println(5/2);// 2 -Quotient

                              System.out.println(5%2);// remainder=1

 

                              System.out.println(9/2);//    -quotient 4

                              System.out.println(9%2);//    rem val =1

 

 

               }

}

// HW: Perform addition, subtraction, multiplication, division (quotient, remainder) of 2 numbers 6 and 2, and display the output

// HW: Perform addition, subtraction, multiplication, division (quotient, remainder) of 2 numbers 7 and 2, and display the output                                            

2. Relational Operators / Comparison Operators:

These operators are used to compare two numbers and give the output in the form of true or false.

Operators:

  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to
  • == : Equal to
  • != : Not equal to

Examples:

Check if these statements are true or false:

·        6 == 6 --> true

·        6 == 3 --> false

·        6 < 5 --> false

·        3 < 5 --> true

·        6 > 5 --> true

·        3 > 5 --> false

·        6 <= 5 --> false

·        6 <= 6 --> true

·        3 <= 6 --> true

·        3 < 6 --> true

·        3 == 6 --> false

·        6 >= 5 --> true

·        6 >= 6 --> true

·        6 > 6 --> false

·        6 == 6 --> true

·        3 >= 6 --> false

·        3 > 6 --> false

·        6 != 3 --> true

·        6 != 6 --> false

 

package OperatorsBasics;

 

public class RelationalOperators {

 

               public static void main(String[] args) {

                              System.out.println(6<5);// false

                              System.out.println(3<5);// true

 

                              System.out.println(6>5);//  true

                              System.out.println(3>5);//  false

 

                              System.out.println(6<=5);//  false

                              System.out.println(6<=6);//  true

                              System.out.println(3<= 6);//  true

 

                              System.out.println(6>=5);//  true

                              System.out.println(6>=6);//  true

                              System.out.println(3>=6);//   false

 

                              //                                                               6 == 6   gives o./p  :  -->

                              //                                                              6 == 3  -->

                              // HW   do above work 

 

                              //

                              //                                                            6 != 3 - --> true

                              //                                                            6 != 6 - -->  false

                              // HW   do above work

 

                              // declare a,b variables of int and store 6,5

                              int a,b;

                              a = 6;

                              b = 5;    

 

                              //  compare a<b and store into int result variable             

//                           int res =               a < b;// error

                             

                              //          6 < 5

                              //           false

                              //  res= false;

                             

                              //                           int res =  a < b;// Type mismatch: cannot convert from boolean to int

                              // if we want to store boolean values true/ false values , we have to use 'boolean ' data type left side

                              // We cannot store boolean value true/ false values in int variable i.e res

 

                              boolean  res =    a < b;//

                              //                                                                        6 < 5 ;

                              //                                 res =      = is  a boolean val

 

                              // display result

System.out.println("result="+res);

//                                                         syso                                     res=false

               }

}

 

HW :  write the o/.p for below stmts?

 

  2 ==2

  2 == 3

 

 3 > 2

 2 > 3

 

 6 < 3

 6 < 8

 

 3 >= 2

 3 >= 3

3 >= 10

 

 3 <= 2

 3<=3

 3<=10

 

3 !=3

3! =10

 

Note :

3 > 3 --> False

3 < 3 --> False

               System.out.println(3>3);// false

               System.out.println(3<3);// false

 

3. Compound Operators:

Compound operators combine an arithmetic operation with an assignment operation. They simplify code by reducing the need to repeat variables.

Operators:

  • += : Addition and assignment
  • -= : Subtraction and assignment
  • *= : Multiplication and assignment
  • /= : Division and assignment
  • %= : Modulus and assignment

 

package OperatorsBasics;

 

public class CompoundOperators {

 

               public static void main(String[] args)

               {

                              int a = 2;                            

                               a += 5;                               

                               //  a  = a +5;

                               //      = 2 + 5 

                               //       = 7

                               //  a =  7

                               

                               System.out.println("a="+ a);// 7  but not 2

                               

                               a -= 5;// a=2 or 7  =  7

                               //   a =  a-5;

                               //        7-5

                               //         2

                               //    a =2

                               //           dont substitute  a= 2   , substitute a=7

                                //      

                               //    a =

                               System.out.println("a="+ a);//2

                               

                               a *= 5; //  a =  latest value 2

                               //  a =  a * 5; 

                               //      = 2*5

                               //      = 10

                               // a =10

                               System.out.println("a="+ a);// 10

                               

                                a/=5;

                                //   a = a / 5;  // a 's latest value = 10

                                //     = 10 / 5           /   == Quotient             5) 10 (2 Quotient

                                //        2

                                // a =  2

                                System.out.println("a="+ a);// 2

                               

                                a%=5;

                                // a = a % 5;

                                //     2 % 5;

                                //        2

                                // a =2

                                //        2  rem val

//                                     5) 2  ( 0    -      not 0.4

//                                        0

//                                       ---------

////                                       2 -- rem                

                                                             

                                System.out.println("a="+ a);// 2

                               

                                // we can store + ve no, and -ve no's in variable  

                              // store -3 val into variable a

                                 a = -3;

                                 System.out.println("a="+a); // -3

                                //   assign +3 val into variable i.e a

                                 a = +3;

                                 //   3

                                 //  a =3

                               

                                System.out.println("a="+a);//a=3

 

               }

 

}

 

We can write below stmts like

Ex1:

        a= 3;

        b =2;

 

        a += b;

//       a =   a + b;

//             3 + 2

//                5

//     a =5

Ex2:

a = 4;

b= 2;

               a -= b;

//        a =  a -b;

//             4 - 2

//              2

//     a =2

 

HW   int a= 5

   int b= 2;

 

               a*=b;

               a/=b;

               a%=b;

 

2) 5 (  -Quotient val

  

-------------

       -rem val

----------------

Logical Operators:

Logical operators are used to perform operations on boolean values. The common logical operators are:

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT

&& - Logical AND Operator:

The logical AND operator (&&) is used to perform a logical conjunction of two boolean values. The result of a && b is true only if both a and b are true.

It can be used to perform Mulitplication (*) operation on Boolean values

  0 - false

  1 - true  (or any no  235)

 

 a  b   a && b  (Multiplication)

-------------------------------

0   0     0     false      

0   1     0               false

1   0     0     false

1   1     1               true

--------------------

 a         b     a && b  (Multiplication)

---------------------------------------

false   false     false

false   true      false

true    false     false

 

true    true       true

·  If both a and b are true, then a && b evaluates to true.

·  If any one of a or b is false, then a && b evaluates to false.

package OperatorsBasics;

public class LogicalAndOperator {

               public static void main(String[] args) {

                              // Declare boolean variable a, b  and store false , false

                              boolean a,b;

                              a = false;

                              b = false;

 

                              // Declare boolean variable  result  and perform logical and operation

                              boolean result; 

                              result = a && b;

 

                              //                           false && false                                                                               *  Multiplication

                              //       false           

                              //   result  =  false             

                              // if any one i/p is false-  && gives o/p - false

                              //  if both i.p's are true - &&          -  true

 

                              // Display Result a &&b

                              System.out.println("result= f &&f " + result);

                              //                                                                                                      false

 

                              // Store false, true values into a, b 

                               a = false;

                               b = true;                            

 

                              // Declare boolean variable result and performs logical and operation

//                           boolean result;

                              //  no 2 var's can have same name.

                              // Variable name must be unique

                              // duplicate variable names are not allowed in java lang.

                               result =  a && b;

 

                              //         false && true

                               //         false

                               //result  = false

                              // Display variable result and and performs logical and operation      

                               System.out.println("result= f && t " + result);

                               //                                      false

 

                              // Store true, false values into a, b 

                               a  = true;

                                b = false;

                                result = a && b;

                                //       true && false

                                //          false

                                // result = false

                              //                                                        // Display Result a &&b

 

                              //       

                              System.out.println("Result true && false ="+ result); // false

 

                              //HW  Store true, true values into a, b 

 

                              // Display Result a &&b

 

                              //         == 

                              System.out.println("Result a &&b =");// true      

 

//                           result =  10 && 2;

//                                                                                       System.out.println("&& on numbers" + (1&&3));

                              // the operator && is undefined for the argument type(s) int, int

                              // Note:  we should not use any numbers for Logical and (&&) operator

                              // always we must give boolean val / boolean variable for logical &&

               }

}

&& --  multiplication *

   if any one i/p is false -- && gives o/p = false

 

|| - Logical OR Operator:

                             Addition + operation

Pipe symbol

- can be used to perform addition (+) operation on boolean values

The result of a || b is true if either a or b is true.

 a         b     a || b  (addition+)

--------------------------------

false   false      false

 

false   true       true

true    false      true

true    true       true

·  If either a or b is true, then a || b evaluates to true.

·  If both a and b are false, then a || b evaluates to false.

 

 a  b   a || b  (Addition  +)

------------------

0   0     false    0+0 = 0            

0   1     true             0 +1 =1       

1   0     true         1 +0=1

1   1     true                         1+1 =2

package OperatorsBasics;

public class LogicalOROperator {

               public static void main(String[] args) {

                              // Declare boolean variables a,b and store false, false

                              boolean a,b;

                               a= false;

                               b= false;

                               

                              // declare boolean variable result and performs logical OR operation

//                           if any one i/p is true - Logical OR gives o/p: true

//                           if both i/p's are false -                        false

                               boolean result  = a|| b;

                               

                              //                 false || false                         addition

                              //                  false

                              // Display Result 

                               System.out.println("result f || f ="+ result);

                              //                                          false

 

                              // store false, true into a,b

                               a = false;

                               b = true;

                              //  perform logical OR operation  and store the o/p into  result  var

                               result = a || b;

                               //       false || true

                               //           true

                               // result  = true

                               

 

                              //

                              // Display Result      

                               System.out.println("result f || t ="+ result);

                               //                                         true

                             

                               //HW  store true,false into a,b

 

 

                              //  perform logical OR operation  and store the o/p into  result  var

 

                              //

                              // Display Result        

 

 

                              //HW  store true,true into a,b    

 

 

                              //  perform logical OR operation  and store the o/p into  result  var          

 

                              // 

                              // Display Result    

 

 

                              // Can we use || for numbers?  1 || 3

                               

                              // System.out.println(" || on numbers" + (1||3));

                              // the operator || is undefined for the argument type(s) int, int

                              // Note:  we should not use any numbers for Logical OR operator

                              // always we must give boolean val / boolean variable   with Logical OR operator                            

 

 

               }

}

 

 

! - Logical NOT Operator:

The logical NOT operator (!) is used to perform negation on a boolean value. It inverts the value of a boolean expression.

 

  !true  = false

  !false = true

package OperatorsBasics;

 

public class LogicalNotOperator {

 

               public static void main(String[] args) {

                              // TODO Auto-generated method stub

                              boolean a = true;

                              boolean res =  !a;// logical not operator

                              ///            !true

                              //       res =      false

 

                              System.out.println("res ="+ res); //  false

 

                              a = false;

                              res =  !a;

                              //      ! false

                              //         true

                              System.out.println("res ="+ res);// true

               }

}

 Revision:

Logical Operators:

  • AND (&&)
  • OR (||)
  • NOT (!)

AND (&&) - Logical AND Operator:

  • Equivalent to multiplication (*) for boolean values.
  • && returns false if any one input is false; otherwise, it returns true.

OR (||) - Logical OR Operator:

  • Equivalent to addition (+) for boolean values.
  • || returns true if any one input is true; otherwise, it returns false.

NOT (!) - Logical NOT Operator:

  • Performs negation.
  • !true becomes false.
  • !false becomes true.

Examples:

  • && (AND):
    • true && true -> true
    • true && false -> false
    • false && true -> false
    • false && false -> false
  • || (OR):
    • true || true -> true
    • true || false -> true
    • false || true -> true
    • false || false -> false
  • ! (NOT):
    • !true -> false
    • !false -> true
    •  

Write o/p for below programs?

Ex1:

boolean result = true || false && true;

         //                          f

         //           t

         System.out.println(result);

 

Ex2:

     boolean result = (5 > 3) && (2 < 4);

         //                  true  && f

         //                      f

     System.out.println(result);

Ex3:

int x = 5;

     boolean result = (x >= 5) || (x < 2);

     //                  t           f

     //                      t

     System.out.println(result);

 

Ex4:

     boolean result = (7 < 5) && (8 > 3) || (9 != 9);

//   //                 f          t            f

//   //                     f                    f

//   //

     System.out.println(result);

    

Ex5:

     int x = 4;

     int y = 6;

     boolean result = (x == 4) && (y != 6) || (x < y);

//   //                 t           f           t

//   //                     f                  t

//   //                      t

     System.out.println(result);

 

Ex6:

     int a = 8;

     int b = 3;

     boolean result = (a % 2 == 0) || (b % 2 != 0);

     //                8 %2

     //                 t               t

     //                 tr

     System.out.println(result);

Ex7:

     boolean result = (4 >= 4) && (2 != 2) || (7 <= 7);

     //                  t          f               t

     //                     f                        t

     //                        t

     System.out.println(result);

Ex8:

     boolean a = true;

     boolean b = false;

     boolean result = (a && b) || (!b);

     //                    f           t

     //                   t

     System.out.println(result);

Ex9:

     int x = 10;

     boolean result = (x < 5) && (x == 10) || (x > 5);

     //                 f            t            t

     //                       f                      t

     //                              t

     System.out.println(result);

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...