Java Week 4
Java Week 4
Java Week 4
By default, programs are executed generally from top to bottom, in the order that
they appear. These instructions are always executed in sequence, that is, one after
the other, from the beginning to the end of the main method. This order of
execution is usually too restrictive when complex task needs to be carried out.
Hence, the need to have much more control over the order in which instructions is
Decision-Making Statement
The if Statement
The if statement is the most basic of all the control flow statement. It tells your
if ( CONDITION )
{
STATEMENTS
}
An example code is given below:
import java . util . Scanner;
public class Temperature
{
public static void main( String [] args )
{
Scanner keyboard = new Scanner( System.in );
System.out.print( "What is the average high temperature in August in
your area? : " );
double temp = keyboard.nextDouble ();
if ( temp > 90.0 )
{
System.out.println( "Wow! That must be very hot!");
}
}
}
hot!" if the value the user enters is greater than 90.0 and prints nothing if the value
of action. This extended form of selection is the if…else statement. As the name
implies, the instructions to be executed if the test evaluates to false are preceded by
import java.util.Scanner;
public class DisplayResult
{
public static void main(String[] args)
{
int mark;
Scanner keyboard = new Scanner(System.in);
System.out.println("What exam mark did you get? ");
mark = keyboard.nextInt();
if (mark >= 40)
{
// executed when test is true
System.out.println("Congratulations, you passed");
}
else
{
// executed when test is false
System.out.println("I'm sorry, but you failed");
}
System.out.println("Good luck with your other exams");
}
}
This program checks a student’s exam mark and tells the student whether or not he
or she has passed (based on the pass mark score which is 40), before displaying a
The switch statement works exactly the same way as a set of nested if statements,
but is more compact and readable. The body of a switch statement is known as a
Switch block. Another point of interest is the break statement. Each break
statement terminates the enclosing switch statement. The break statements are
The check involves specific values of that variable and not range of values
“month” has already been declared to be “8” which stands for August.
Looping Statement
Iteration is the form of program control that allows us to instruct the computer to
carry out a task several times by repeating a section of code. For this reason, this
that is used to control this repetition is often called a loop; we say that the loop
iterates a certain number of times. There are three types of loop in Java:
for loop;
while loop;
do…while loop.
The “for loop” statement provides a compact way to iterate over a range of values.
If we wish to repeat a section of code a fixed number of times we would use the for
This code will produce the same result as the one with the for loop. But using the
for loop will save a programmer a lot of time as it is easy to use, not time
The for loop is an often used construct to implement fixed repetitions. Sometimes,
however, a repetition is required that is not fixed and a for loop is not the best one
• A racing game that repeatedly moves a car around a track until the car crashes;
• A ticket issuing program that repeatedly offers tickets for sale until the user
• A password checking program that does not let a user into an application until he
Each of the above cases involves repetition; however, the number of repetitions is
not fixed but depends upon some condition. The while loop offers one type of non-
fixed iteration. The syntax for constructing this loop in Java is as follows:
The do…while loop is another variable loop construct, but, unlike the while loop,
the do…while loop has its test at the end of the loop rather than at the beginning.
do
{
// instruction(s) to be repeated go here
}
while ( /* test goes here */ );
The difference between the while loop and the do…while loop is that the while
loop evaluates its expression at the top of the loop whereas the do…while loop
evaluates its expression at the bottom of the loop. Therefore, the statements within
the do blocks are always executed at least once even if the condition happens to be
false, whereas it’s possible that no statements are executed in the while loop if the
The Break statement in Java is used to terminate from a loop (i.e. for loop, while
The continue statement skips the current iteration of a loop and jumps to the next
iteration of the loop immediately. An example code is given below for a continue
statement in a loop.
OUTPUT
013456789
A return statement causes the program control to transfer back to the caller of a
method. Every method in Java is declared with a return type and it is mandatory
for all java methods. A return type may be a primitive type like int, float, double,
a reference type or void type (returns nothing). There are a few important things
to understand about returning the values. The type of data returned by a method
must be compatible with the return type specified by the method. For instance, if
the return type of some method is Boolean, we cannot return an integer. The
variable receiving the value returned by a method must also be compatible with the
return type specified for the method. The parameters can be passed in a sequence