Conditional statements are used
as parameters to flow control statements to determine the direction
of flow. They are generally a comparison between two variables
or a variable and a constant. The comparison that is performed
is determined by an operator. A list of available operators is
as follows:
Var1 OP Var2
where OP is on of the following:
==
- Is equal to
!=
- Is not equal to
>=
- is greater than or equal to
<=
- is less than or equal to
>
- is greater than
<
- is less than
!
- the 'not' operator
The return value of the comparison is true if the comparison is true,
false otherwise. For more information on the various types of operators,
see the Operators section.
Some data types, such as bool, int, and window, can
be placed in a comparison by itself without an operator. If the
boolean variable is true or the int value is not zero, then the conditional
test returns true, else it returns false. The window returns true
if it contains a See the following example code which illustrates this.
Example
Code |
int
i = 10;
int j = 8;
bool b = true;
string s = "hello";
if(i == 10){
disp("i is 10");
}
if(i != j){
disp("i is not j");
}
if(i == j+2){
disp("i is j+2");
}
if(i > j){
disp("i is greater than j");
}
if(j <= i){
disp("j is less than or equal to i");
}
if(b){
disp("b is true");
}
if(i){
disp("i is not zero");
}
if(!(!b)){
disp("b is not-not true!");
}
if(!b){
disp("Should not get here!");
}else{
disp("OK to get here");
}
if(s == "hello"){
disp("s is hello");
}
if(s < "he"){
disp("Should not get here!");
}else{
disp("OK to get here");
} |
The output from the
above example is:
Output |
i is
10
i is not j
i is j+2
i is greater than j
j is less than or equal to i
b is true
i is not zero
b is not-not true!
OK to get here
s is hello
OK to get here |
See Also:
if-else, for, while