Unit-1 Control statement
Unit-1 Control statement
Control Statements
A programming language uses control statements to cause the flow of execution to advance and
branch based on changes to the state of a program.
Java’s program control statements can be put into the following categories:
1. selection
2. iterationa
3. jump.
Selection statements allow our program to choose different paths of execution based upon the
outcome of an expression or the state of a variable.
if (condition) statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in curly
braces.
The condition is any expression that returns a boolean value.
The else clause is optional.
If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is
executed.
Example
int a, b;
if(a < b) a = 0;
else b = 0;
Nested ifs
A nested if is an if statement that is the target of another if or else. An else statement always
refers to the nearest if statement that is within the same block as the else and that is not already
associated with an else.
Example:
if(i == 10) {
if(j < 20)
a = b;
if(k > 100)
c = d;
else
a = c;
}
else
a = d;
The final else is not associated with if(j<20) ) because it is not in the same block
A common programming construct that is based upon a sequence of nested ifs is the if-else-if
ladder.
The if statements are executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest of the ladder is
bypassed.
If none of the conditions is true, then the final else statement will be executed.
Example
switch
The switch statement is Java’s multiway branch statement.
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
The expression must be of type byte, short, int, or char; each of the values specified in the case
statements must be of a type compatible with the expression.
The value of the expression is compared with each of the literal values in the case statements. If
a match is found, the code sequence following that case statement is executed.
If none of the constants matches the value of the expression, then the default statement is
executed.
Example
1. For
2. While
3. do-while.
While
The while loop is Java’s most fundamental loop statement. It repeats a statement or block while
its controlling expression is true.
while(condition) {
// body of loop
do {
// body of loop
} while (condition);
Each iteration of the do-while loop first executes the body of the loop and then evaluates the
conditional expression.
Example
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}}
If this expression is true, the loop will repeat. Otherwise, the loop terminates.
For
There are two forms of the for loop.
The first is the traditional form that has been in use since the original version of Java.
The second is the new “for-each” form
The general form of the traditional for statement is:
When the loop first starts, the initialization portion of the loop is executed. It is important
to understand that the initialization expression is only executed once.
Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop
control variable against a target value. If this expression is true, then the body of the loop
is executed. If it is false, the loop terminates.
Next, the iteration portion of the loop is executed. This is usually an expression that
increments or decrements the loop control variable.
The loop then iterates, first evaluating the conditional expression, then executing the
body of the loop, and then executing the iteration expression with each pass. This process
repeats until the controlling expression is false
Example
class ForTick {
int n;
}}
It is possible to declare the variable inside the initialization portion of the for.
Example
The scope of that variable ends when the for statement does.
There will be times when you will want to include more than one statement in the initialization
and iteration portions of the for loop.
For example
The initialization portion sets the values of both a and b. The two commaseparated statements in
the iteration portion are executed each time the loop repeats.
The for loop supports a number of variations that increase its power and applicability. The
reason it is so flexible is that its three parts—the initialization, the conditional test, and the
iteration
1. One of the most common variations involves the conditional expression. Specifically, this
expression does not need to test the loop control variable against some target value.
Example
With each pass through the loop, x is automatically given a value equal to the next element in
nums. Thus, on the first iteration, x contains 1; on the second iteration, x contains 2; and so on.
The for-each style loop iteration variable is “read-only” as it relates to the underlying array. An
assignment to the iteration variable has no effect on the underlying array.
The contents of the array can’t change by assigning the iteration variable a new value
Example
int sum = 0;
int nums[][] = new int[3][5]; // give nums some values
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1); // use for-each for to display and sum the values
for(int x[] : nums)
{ for(int y : x) {
System.out.println("Value is: " + y);
sum += y;
}}
System.out.println("Summation: " + sum);
The for-each style for can only cycle through an array sequentially, from start to finish.
A large number of algorithms require exactly this mechanism.
One of the most common is searching.
For example, the following program uses a for loop to search an unsorted array for a
value. It stops if the value is found
// Search an array using for-each style for.
class Search {
public static void main(String args[]) {
int nums[] = { 6, 8, 3, 7, 5, 6, 1, 4 };
int val = 5;
boolean found = false; // use for-each style for to search nums for val
for(int x : nums) {
if(x == val) {
found = true; break;
}}
if(found)
System.out.println("Value found!");
}}
The for-each style for is an excellent choice in this application because searching an
unsorted array involves examining each element in sequence.
The benefit of for-each style loops include computing an average, finding the minimum
or maximum of a set, looking for duplicates, and so on.
Nested Loops
Java allows loops to be nested. That is, one loop may be inside another.
Example
// Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10;i++){
for(j=i; j<10;j++)
System.out.println(“.”);
System.out.println();
}}}
Jump Statements
Jump statements allow our program to execute in a nonlinear fashion.
Using continue
In while and do-while loops, a continue statement causes control to be transferred directly to the
conditional expression that controls the loop.
In a for loop, control goes first to the iteration portion of the for statement and then to the
conditional expression.
Example