Unit 3 Operators and Control Statements: Structure
Unit 3 Operators and Control Statements: Structure
3.1 Introduction
In the last unit, you have learnt the basic program structure in Java. In this
unit, we will explore various operators and control statements in Java.
There are many constructs in Java to implement conditional execution. For
example, flow of a program can be controlled using the if construct. The if
construct allows selective execution of statements depending on the value
of the expressions associated with the if construct. Similarly, repetitive tasks
can be done using for constructs which would be discussed in detail in this
unit.
Objectives:
After studying this unit, you should be able to:
explain different operators in Java
discuss various control statements in Java
The Java program in the figure 3.1 adds two numbers and prints the result.
Figure 3.1: Java Program to add two numbers and printing the result
When the operator ++ is placed after the variable name, first the assignment
of the value of the variable is done; then the value of the variable is
incremented by 1. This operation is also called post increment. Therefore,
the value of y1 will remain as 5 and the value of x1 will be 6. When the
operator is placed before the variable, first increment of the variable takes
place and then the assignment occurs. Hence the value x2 and y2 both will
be 6. This operation is also called as pre increment. Similarly – – operator
can be used to perform post decrement and pre decrement operations. If
there is no assignment and only the value of variable has to be incremented
or decremented, then placing the operator after or before does not make
difference.
3.2.3 Comparison Operators
In Java, to compare the values of two or more operators, comparison
operators are used. Table 3.2 lists various Comparison Operators in Java.
Table 3.2: List of Comparison Operators in Java
<= Less than or op1 <= op2 Checks if the operator is lesser than or
equal equal to the other operator.
>= Greater than op1 >= op2 Checks if the first operator is greater than
or equal or equal to the other operator.
Relational operators are mostly used in condition part but we can also use a
single Boolean variable also. The code given below shows the working of an
if statement:
boolean singleValue;
// ...
if (singleValue)
Perform1();
In Java for loop can be nested too, i.e. you can use a for loop within another
for loop. Following example shows the working of a nested for loop:
// Nested Loop.
class Nested {
public static void main(String args[ ]) {
int i, k;
for(i=0; i<5; i++) {
for(k=i; k<5; k++)
System.out.print("*");
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}
In this example, the variable item holds the current value from the numbers
array. The output from this program is as follow:
Count is: 1
Count is: 2
Count is: 3
The do-while loop is useful for programs where we have to create some
Menu Selection List, as in a Menu Selection the Menu has to be executed at
least once before the user can enter choice. Consider the following program
which implements a very simple help system for Java's selection and
iteration statements:
// Using a do-while to process a menu selection
class Menu {
public static void main(String args[])
throws java.io.IOException {
char choice;
3.6 Answers
Self Assessment Questions:
1 %
2 &
3 False
4 False
Terminal Questions
1. Arithmetic, Comparison and Logical Operators. (Refer Section 3.2.1 to
3.2.4)
2. When more than one operator is used in an expression, Java will use
operator precedence rule to determine the order in which the operators
will be evaluated. (Refer Section 3.2)
3. If…else, Switch…Case, For, While, Do…While, Break, and Continue.
(Refer Section 3.3.1 to 3.3.7)
(Terminal Questions 4 to 7 are programming exercises. For this, you should
go through this unit thoroughly.)
Sikkim Manipal University – DDE B2119 Page No. 55