6/28/2019 Object Oriented  Programming Week 2 Ferdin Joe John Joseph Thai‐Nichi Institute of Technology Agenda • Operators and Operands • Control Statements • If – else statement Operators in Java • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Miscellaneous Operators Arithmetic Opertors Format <operand1> <operator> <operand2> = result
6/28/2019 Arithmetic Operators • + adds two operands C=A+B • ‐subtracts second operand from the first C=A‐B • * multiplies two operands C= A* B • / divides numerator by de‐numerator C= A/B • % modulus operator C=A%B • ++ increment operator, increases integer value by one C=A++ • ‐‐decrement operator, decreases integer value by one C=A‐‐ Relational Operator Format <Operand1> <Operator> <Operand2> Returns 0 or 1 as answer Relational Operator • == Checks if two values are equal A==B • != Checks if two values are not equal A!=B • > Checks if operand1 is greater than operand2 A>B • < Checks if operand1 is lesser than operand2 A<B • >= Checks if operand1 is equal to or greater than operand2 A>=B • <= Checks if operand1 is equal to or lesser than operand2 A<=B Logical Operator • && Logical AND to check both operands are not zero A&&B • || Logical OR to check any of the two operands is zero A||B • ! Logical NOT operator to check if any logical operation is false  !(A&&B)
6/28/2019 Bitwise Operators • To do operations in binary level • Converts integer from decimal to binary and then perform operation Bitwise Operators • & to do bitwise AND • | to do bitwise OR • ^ to do bitwise XOR • ~  to do flipping of bits A = 60 = 0011 1100 • ~A = 1100 0011= 195 • << Binary left shift A = 0011 1100 • <<A = 1111 0000 • >> Binary right shift A = 00111100 • >>A = 0000 1111 Assignment Operators Expressions • Anything whose evaluation yields a numeric value is an expression • C=A+B, A+B is an expression whose evaluated value is stored in  variable C
6/28/2019 Conditonal Constructs • Provide Ability to control whether a statement list is executed • Two constructs If statement • if • if‐else • if‐else‐if • Switch statement Basic if statement • Syntax • if(Expression) Action • If the Expression is true then execute Action • Action • is either a single statement  • or a group of statements within braces Example if (Value < 0) { Value = ‐Value; } Example – sorting of two numbers int Value1,  Value2; Value1=5; Value2=10; System.out.println(Value1 +” “+Value2); if  (Value1  >  Value2)   { int RememberValue1  =  Value1; Value1  =  Value2; Value2  =  RememberValue1; System.out.println(Value1 +” “+Value2); }
6/28/2019 If – Else Statement if(Expression) Action 1 else Action 2 • If  Expression is true then execute Action1 • otherwise execute  Action2 if (v == 0) { System.out.println("v is 0“); } else { System.out.println("v is not 0“); } Finding type of number if ( nbr < 0 ){ System.out.println(nbr+" is negative“); } else if ( nbr > 0 ) { System.out.println(nbr+" is positive“); } else { System.out.println(nbr+" is zero“); } Nested if Next Week • Looping • For Loop • While Loop
6/28/2019 Classwork • Write java code to find whether the given year is a leap year or not • Write java code to find whether the given number is odd or even

DSA 103 Object Oriented Programming :: Week 2