Operators

Table of Contents























Assignment Operators
Simple Assignment
Problem
Assign the value 10 to the variable maximum
Assign the value false to the variable isBig
Print out the two values
Java
	//This is the most important operator
	int maximum;
	boolean isBig;
	maximum = 10;
	isBig = false;
	System.out.println("maximum is " + maximum);
	System.out.println("isBig is " + isBig);
Special Addition Assignment
Problem
Assign the int 1000 to the variable num
Add 100 to num
Java
	/*
	The += operator is a shorthand (abbreviation).
	That is,
		num += 10;
	is equivalent to
		num = num + 10;
	*/
	int num = 1000;
	num += 100;
	System.out.println("num (using +=) is " + num);
Special Subtraction Assignment
Problem
Assign the int 1000 to the variable num
Subtract 100 from num
Java
	/*
	The -= operator is a shorthand (abbreviation).
	That is,
		num -= 10;
	is equivalent to
		num = num - 10;
	*/
	int num = 1000;
	num -= 100;
	System.out.println("num (using -=) is " + num);
Arithmetic Operators
Addition
Problem
Add three integers and show the result
Pseudocode
	Let a, b, c, d = 10, 20, 30, 40
	Let sum = a + b + c + d
	Print sum
Java
	int a, b, c, d, sum;
	a = 10;
	b = 20;
	c = 30;
	d = 40;
	sum = a + b + c + d;
	System.out.println("sum of 10, 20, 30 and 40 = " + sum);
Subtraction
Problem
Subtract a smaller number from a larger number (use variable type double)
Pseudocode
	Let larger = 5.0
	Let smaller = 0.5
	Let difference = larger minus smaller;
	Print difference
Java
	double larger, smaller, difference;
	larger = 5.0;
	smaller = 0.5;
	difference = larger - smaller;
	System.out.println("Difference between 5 and 0.5 is " + difference);
Multiplication
Problem
Multiply two integers and show the result
Pseudocode
	Let a = 5
	Let b = 2
	Let product = a * b
	Print product
Java
	int a, b, product;
	a = 5;
	b = 2;
	product = a * b;
	System.out.println("product of 5 times 2 is " + product);
Division
Problem
Divide int 10 by int 5
Pseudocode
	Let a = 10
	Let b = 5
	Let quotient = a / b
	Print quotient
Java
	int a, b, quotient;
	a = 10;
	b = 5;
	quotient = a / b;
	System.out.println("a is " + a);
	System.out.println("b is " + b);
	System.out.println("quotient for a divided by b is " + quotient);
	/*What are the results when you try
		7/5
		7/0
	*/
Division Using double Types
Problem
Divide double 7.5 by double 5.0
Pseudocode
	Let a = 5.0
	Let b = 2.5
	Let quotient = a / b
	Print quotient
Java
	double a, b, quotient;
	a = 5.0;
	b = 2.5;
	quotient = a / b;
	System.out.println("a is " + a);
	System.out.println("b is " + b);
	System.out.println("quotient for a divided by b is " + quotient);
	/*What are the results when you try
		5.0/0.0
	*/
Remainder Or Modulus
Problem
Calculate the remainder of dividing 7 by 5
Java
	int a, b, remainder;
	a = 7;
	b = 5;
	//Using Java's Remainder (modulus) operator %
	remainder = a % 5;
	System.out.println("Remainder of 7 divided by 5 is " + remainder);
Using Remainder To Determine Even And Odd Numbers
Problem
Determine if a number is odd
Pseudocode
	Given:
		Even numbers have remainder 0 when divided by 2 (evenly divided by 2)
		Odd numbers have remainder 1 when divided by 2 (not evenly divided by 2)
	Let num = 9
	Calculate remainder of num divided by 2
	Let isEven be true if the remainder is equal to 0
	Let isOdd be true if the remainder is equal to 1
Java
	int num, remainder;
	boolean isEven, isOdd;
	num = 9;
	remainder = num % 2;
	isEven = remainder == 0;
	isOdd = remainder == 1;
	System.out.println("num is " + num);
	System.out.println("remainder (when num is divided by 2) is " + remainder);
	System.out.println("isEven is " + isEven);
	System.out.println("isOdd is " + isOdd);
Equality And Relational Operators
Equal To
Problem
Compare two ints, and print the result
Pseudocode
	Let int1 = 15
	Let int2 = 15
	Let result = "Equal to" comparison int1 and int2
	Print result
Java
	//Use the "Equal to" (equality) operator "==" that is used for primitive variables (int, boolean, double, etc)
	//The equality operator results in a boolean answer
	//NOTE WELL -- we used int types here, but this operator is similar for all primitive types
	int int1 = 15;
	int int2 = 15;
	boolean result;
	result = int1 == int2;
	System.out.println("'Equal to' operator result for (15 == 15) is " + result);
	//Now try setting int2 to 99, and see what happens
Not Equal To
Problem
Compare two ints, and print the result
Pseudocode
	Let int1 = 15
	Let int2 = 15
	Let result = "Not Equal to" comparison int1 and int2
	Print result
	Change int2 to 99
	Let result = int1 == int2
	Print result
Java
	//Use the "Not Equal to" (inequality) operator "!=" that is used for primitive variables (int, boolean, double, etc)
	//The equality operator results in a boolean answer
	//NOTE WELL -- we used int types here, but this operator is similar for all primitive types
	int int1 = 15;
	int int2 = 15;
	boolean result;
	result = int1 != int2;
	System.out.println("'Not Equal to' operator result for (15 != 15) is " + result);
	//Now try setting int2 to 99 and see what happens
Relational Operators Like Greater Than Etc
Problem
Demo equality and relational operators
Java
	int a, b;
	a = 1000;
	b = 5;
	System.out.println("");
	System.out.println("Relational Operators");
	System.out.println("a = " + a);
	System.out.println("b = " + b);
	System.out.println("a == b is " + (a == b));
	System.out.println("a != b is " + (a != b));
	System.out.println("a > b is " + (a > b));
	System.out.println("a >= b is " + (a >= b));
	System.out.println("a < b is " + (a < b));
	System.out.println("a <= b is " + (a <= b));
	//Try changing a and b
Equal To With booleans
Problem
Compare two booleans, and print the result
Pseudocode
	Let bool1 = true
	Let bool2 = true
	Let result = "Equal to" comparison bool1 and bool2
	Print result
Java
	//Use the equality or equals operator "==" that is used for primitive variables
	boolean bool1 = true;
	boolean bool2 = true;
	boolean result = bool1 == bool2;
	System.out.println("'Equal to' operator result for (true == true) is " + result);
	//Now try setting bool2 to false and see what happens
		
	/*NOTE WELL: THIS IS SORT OF A TRICK PROBLEM
	The reason is that
		true == true results in true
		false == true results in false
	So we could just use the left operator as-is (it does not change)*/
Logical Operators
Logical A N D
Problem
Determine if boolean1 AND boolean2 are true
Java
	boolean boolean1, boolean2, booleanResult;
	boolean1 = true;
	boolean2 = false;
	//We use Java's AND operator which is &&
	booleanResult = boolean1 && boolean2;
	System.out.println("" + boolean1 + " && " + boolean2 + " is " + booleanResult);
	//Try: Try changing the variable boolean2
Logical O R
Problem
Determine if boolean1 OR boolean2 are true
Java
	boolean boolean1, boolean2, booleanResult;
	boolean1 = true;
	boolean2 = false;
	//We use Java's OR operator which is ||
	booleanResult = boolean1 || boolean2;
	System.out.println("" + boolean1 + " || " + boolean2 + " is " + booleanResult);
	//Try: Try changing the variable boolean2
Unary Operators
Negation
Problem
Given an int variable, negate the value
e.g. for 8, derive -8
for -205, derive 205
(that is, just flip the sign of the number)
Pseudocode
	Let value = 205
	Let flippedValue = -value
	Print value
	Print flippedValue
Java
	int value, flippedValue;
	value = 205;
	flippedValue = -value;
	System.out.println("value is " + value);
	System.out.println("flippedValue (using unary -) is " + flippedValue);
	//Try: Try changing the variable 
Increment
Problem
Given the value 8, increment it (that is, increase it by one)
Pseudocode
	Let value = 8
	Increment the value
	Print value
Java
	int value;
	value = 9;
	value++;
	System.out.println("Incremented value is " + value);
Decrement
Problem
Given the value 8, decrement it (that is, decrease it by one)
Pseudocode
	Let value = 8
	Decrement the value
	Print value
Java
	int value;
	value = 9;
	value--;
	System.out.println("Decremented value is " + value);
Logical Not
Problem
Given a boolean, derive and print the opposite value (called logical NOT, negation, or complement)
For true, derive false
For false, derive true,
That is: flip it
Pseudocode
	Let value = false
	Let flippedValue = the logical negation of value
	Print value
	Print flippedValue
Java
	boolean value, flippedValue;
	value = false;
	flippedValue = !value;
	System.out.println("value is " + value);
	System.out.println("flippedValue (using !=) is " + flippedValue);
	//Try: Try changing the variable 
Special Operators
Ternary If Else Operator
Problem
Find the min of two int variables a and b
Java
	int a, b, max;
	a = 10;
	b = 8008;
	max = (a < b) ? a : b;
	System.out.println("min (using ternary operator) is " + max);