NLP-AI
Java Lecture No. 4
Operators & Decision Constructs
Satish Dethe
<satishd@[Link]>
03 August 2004
Contents
• Increment Operator
• Decrement Operator
• Boolean Data Type
• Relational Operators
• Equality Operators
• Conditional Operators
• Selectional Constructs
03 August 2004
nlp-ai@[Link]
Increment Operators
• i + + first use the value of i and then increment it by 1.
‘i + +’ equivalent to ‘i = (i)+1’. // postfix increment
• + + i first increment the value of i by 1 and then use it.
‘+ + i’ equivalent to ‘i = (i+1)’. // prefix increment
int i=2;
•[Link](“ i = ”+ i++);//print 2, then i becomes 3
•[Link](“ i = ”+ ++i);//add 1 to i, then print 4
•Refer to [Link]
03 August 2004
nlp-ai@[Link]
Decrement Operators
• i - - first use the i ’s value and then decrement it by one
i - - equivalent to (i)-1. // postfix decrement
• - - i first decrement i ’s value by one and then use it.
- - i equivalent to (i-1). // prefix decrement
int i=5;
[Link](“i = ” + i--);//print 5, then i becomes 4
[Link](“i = ” + --i);//subtract 1 from i, then print 3
Refer to [Link]
03 August 2004
nlp-ai@[Link]
Boolean Data Type
This data type can store only two values; true and false.
Declaring a boolean variable is the same as declaring any other
primitive data type like int, float, char.
boolean response = false; //Valid
boolean answer = true; //Valid
boolean answer = 9943; //Invalid,
boolean response = “false”; // Invalid,
This is return type for relational & conditional operators.
Refer to bool_op.java
03 August 2004
nlp-ai@[Link]
Relational Operators
a < b a less than b. (true/false)
a <= b a less than or equal b. (true/false)
a > b a greater than b. (true/false)
a >= b a greater than or equal to b. (true/false)
These operations always return a boolean value.
[Link](“23 is less than 65 ” +23<65); // true
[Link](“5 is greater than or equal to 25.00?” +
5>=25.00); // false
Refer to [Link]
03 August 2004
nlp-ai@[Link]
Equality Operators
a==b a equal to b. (true/false)
a!=b a not equal to b. (true/false)
boolean equal = 12 = = 150; // false
boolean again_equal = ‘r’= = ‘r’); // true
boolean not_equal = 53!=90); // true
Refer: [Link]
03 August 2004
nlp-ai@[Link]
Conditional Operators
•A conditional operator is used to handle only two boolean
expressions.
Boolean expression always returns ‘true’ or ‘false’.
•Conditional AND ‘&&’
Return value is ‘true’ if both, x and y are true, else it is ‘false’.
[Link](“x&&y ” + x&&y); // Refer cond_and.java
•Conditional OR ‘||’
return value is true if any one of x or y, is true else it is false.
[Link](“x||y ” + x||y); // Refer cond_or.java
03 August 2004
nlp-ai@[Link]
The ‘ if ’ construct
It is used to select a certain set of instructions. It is used to decide
whether this set is to be carried out, based on the condition in the
parenthesis. Its syntax is:
if (<boolean expression>){
//body starts
<statement(s)>
//body ends
}
The <boolean expression> is evaluated first. If its value is true, then
the statement(s) are executed. And then the rest of the program. Refer
if_cond.java, if_cond1.java
03 August 2004
nlp-ai@[Link]
The ‘if else’ construct
It is used to provide an alternative when the expression in if is
false. Its syntax is:
if(<boolean expression>){
<statement(s)>
}
else{
<statement(s)>
}
The if construct is the same. But when the expression inside if
is false then else part is executed.
Refer ifelse_cond.java
03 August 2004 nlp-ai@[Link]
Assignments
int a_number=1; // (range: 1 to 5 including both)
Print the value of a_number in word. For example, it should print
“Four” if a_number contains 4.
1. Use equality ‘= =’ operator.
2. Do not use equality ‘= =’ operator.
03 August 2004 nlp-ai@[Link]
End
Thank You!
03 August 2004
nlp-ai@[Link]