Test condition

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Loops


Introduction

A test condition is a mathematical comparison that evaluates to a boolean value(true or false). Typically, this boolean value is used to make a decision. If it's true, then do something, and if it is false, then do something else thing.

   

{{{Body}}}

(Vic)

Comparison Operators

Test conditions typically contain one of the following operators:

>  : greater than
>= : greater than or equal to
<  : less than
<= : less than or equal to
== : equal to
!= : not equal to

In java, there is a boolean type as seen previously in primitive variables, which can either be true or false.

If no comparison operator is present where a boolean evaluation is going on it really means ( 'variable' == true )

Mathematical Operators

Most basic mathematical operators are allowed in expression, including

+  : add
-  : subtract
*  : multiply
/  : divide
%  : modulus
() : brackets

Logical Operators

There are also operators to represent AND and OR

&& : AND
|| : OR
!  : NOT

If you use AND, the expression on both sides of the AND must be true for the whole expression to be true.

If you use OR, one or more of the expressions beside the OR Must be true for the whole expression to be true.

If you use NOT, the truth value of the expression will be inverted(true becomes false, false becomes true)

Examples

1 < 3          : true
3 < 3          : false
3 <= 3         : true
(1+2) <= (6/2) : true
4 == 4         : true
4 == 5         : false
4 != 5         : true
4*(3+1) == 16  : true
false != false : false
false          : false
true != false  : true
true           : true
false && true  : false
true && true   : true
false || false : false
false || true  : true
!true          : false
!false         : true