Wednesday, February 10, 2016

Codecademy Learn Java tutorial walkthrough 10: relational operators

Codecademy basics tutorials always cover relational operators for each programming language taught. Relational operators refer to the !=, >, ==, <= in (5!=3) or (5>3) or  (5==5)  or (5 <= 7). Each is a test to see if the left and right side of the relational operator has the said relation. For example, 5!=3 will evaluate to true because it is testing for whether it is true 5 doesn't equal to 3, and that is indeed the case, so the result is true. What about (5==3)? It tests if 5 equals to 3, and the answer is no so it evaluates to false. The result of a statement with a relational operator almost always only returns only true or false. Do you remember what's the name of the data type that can only be true or false? Yes a Boolean. A Boolean can either be true or false. Why are relational operators useful?

For example in the previous exercise, we used Modulo to see if z % 2 is zero, which means z is even. We can ask the program to do something only of z is even. Below so the pseudo code to do that (pseudo code is descriptive English words that explains what the code which is yet to be written is intended to do).

if ( (z % 2) ==0) print "z is even!"

This code reads first calculate z % 2, and if that is 0, print out a message saying z is even

See the screenshot above for one of the many solutions to this exercise. Remember Codecademy requires you to use two whole numbers. And don't forget to end the line with a semicolon, else you may get a confusing error.

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai java 8 online training JavaEE Training in Chennai Java EE Training in Chennai