2. 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)
4. 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);
}
5. 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