Search

Friday, December 14, 2018

Two Control Statements if Statement and for Loop

Two Control Statements

Inside a method, execution proceeds from one statement to the next, top to bottom. However, it is possible to alter this flow through the use of the various program control statements supported by Java. Although we will look closely at control statements later,two are briefly introduced here because we will be using them to write sample programs.

The if Statement 
You can selectively execute part of a program through the use of Java’s conditional statement: the if. The Java if statement works much like the IF statement in any other language. Its simplest form is shown here:
if(condition) statement; Here,condition is a Boolean expression. If condition is true, then the statement is executed. If condition is false, then the statement is bypassed. 
Here is an example:
if(10 < 11) System.out.println("10 is less than 11");
In this case, since 10 is less than 11, the conditional expression is true, and println( )will execute. However, consider the following:
if(10 < 9) System.out.println("this won't be displayed");
In this case, 10 is not less than 9. Thus, the call to println( )will not take place.

Java defines a full complement of relational operators that may be used in a conditional expression. 

They are shown here:

Operator        Meaning
 <                   Less than
 <=                 Less than or equal 
 >                   Greater than
 >=                 Greater than or equal
 = =                Equal to
 !=                  Not equal

Notice that the test for equality is the double equal sign. Here is a program that illustrates the if statement:

/*
Demonstrate the if.
Call this file If Demo.java.
*/ 
class If Demo { public static void main(String args[]) 
int a, b, c;
a = 2; 
b = 3;
if(a < b) System.out.println("a is less than b");
// this won't display anything 
if(a == b) System.out.println("you won't see this");
System.out.println();
c = a - b; // c contains -1
System.out.println("c contains -1"); 
if(c >= 0) System.out.println("c is non-negative"); 
if(c < 0) System.out.println("c is negative");
System.out.println();
c = b - a; // c now contains 1
System.out.println("c contains 1"); 
if(c >= 0) System.out.println("c is non-negative"); 
if(c < 0) System.out.println("c is negative");
}
}
The output generated by this program is shown here:
a is less than b
c contains -1 c is negative
c contains 1 c is non-negative
Notice one other thing in this program. The line
int a, b, c; declares three variables,a,b, and c, by use of a comma-separated list. As mentioned earlier, when you need two or more variables of the same type, they can be declared in one statement. Just separate the variable names by commas.

TheforLoop 

You can repeatedly execute a sequence of code by creating a loop. Java supplies a powerful assortment of loop constructs. The one we will look at here is the for loop. The simplest form of the for loop is shown here:
for (initialization; condition; iteration) statement; 
In its most common form, the initialization portion of the loop sets a loop control variable to aninitial value.The condition is a Boolean expression that tests the loop control variable.If the out come of that test is true, the for loop continues to iterate. If it is false, the loop terminates.The iteration expression determines how the loop control variable is changed each time the loop iterates. Here is a short program that illustrates the for loop:
/*
Demonstrate the for loop.
Call this file For Dem.
*/ 
class For Demo 
public static void main(String args[]) 
int count;
for(count = 0; count < 5; count = count+1)
 System.out.println("This is count: " + count);
System.out.println("Done!");
}
}
The output generated by the program is shown here:
This is count: 0 
This is count: 1 
This is count: 2 
This is count: 3
 This is count: 4 
Done!
In this example, count is the loop control variable. It is set to zero in the initialization portion of the for. At the start of each iteration (including the first one), the conditional test count < 5 is performed. If the outcome of this test is true, the println( )statement is executed, and then the iteration portion of the loop is executed. This process continues until the conditional test is false, at which point execution picks up at the bottom of the loop. As a point of interest, in professionally written Java programs, you will almost never see the iteration portion of the loop written as shown in the preceding program. That is, you will seldom see statements like this:
count = count + 1;
The reason is that Java includes a special increment operator that performs this operation more efficiently. The increment operator is ++ (that is, two plus signs back to back). The increment operator increases its operand by one. By use of the increment operator, the preceding statement can be written like this:
count++;
Thus, the for in the preceding program will usually be written like this:
for(count = 0; count < 5; count++)
You might want to try this. As you will see, the loop still runs exactly the same as it did before. Java also provides a decrements operator, which is specified as – –. This operator decreases its operand by one.

0 comments:

Post a Comment