Often, you'll need to compare 2 variables to each other to decide what to do next, such as a statement like (In pseudo code):
if (currentValue is larger than baseValue) { Lower currentValue }
Comparison statements result in a boolean value (either true or false), so in the above example, if currentValue > baseValue, True is returned, otherwise, False.
To compare 2 strings, say "test string" and "test string 2", we use the .equals() function. For ease, lets put these strings inside variables, test_1 and test_2, at which point we have:
if ( test_1.equals(test_2) ) { ... }
But for other things, numerical values, we can use (some) normal math operators:
if ( a == 2 ) ... Use 2 equals to test for equality. This is because a single equals sign is already used for assignment
if ( a != 2 ) ... The exclamation point in front of the equals means "not", so this statement only returns false if 'a' holds the value '2'
if ( a > 2 ) ... This is simply the greater than operator
if ( a < 2 ) ... Likewise, this is the simple less than operator
if ( a <= 2 ) ... This, as you can guess, means less than, or equal to
if ( a >= 2 ) ... And lastly, this is the greater than/equal to
Comments
Comments are used in the source code of a program as...commentary. It allows a programmer to go back to earlier work and get the gist of a line or paragraph without having to read it, or allows another programmer to do the same. Place "//" somewhere on a line to make the rest of the line a comment.
int i = 9; // This variable holds the number of employees....
If you want a comment spanning more than 1 line though, surround it with "/* ... */"
/* this is a
multi line comment
Normally used for documentation */
No comments:
Post a Comment