Sei sulla pagina 1di 16

First of all

Another way to generate random numbers!!



Java like other programming languages have his own random number
generator. In Java the method random() from the class Math is located
inside the java.lang library (API). And it functions like this:
Math.random();

Activity .- Please try the Math.random() method.
oLook what kind of number (primitive data type) deliver it.
oWhat should you have to do to obtain an integer number of one digit?
oWhat should you have to do to obtain an integer number of two
digits?
Loops
Junio-Diciembre 2013

Loops
Loops are constructs that control repeated executions of a
block of statements. The concept of looping is fundamental
to programming. Java provides three type of loop
statements:

owhile loops
odo-while loops
oand for loops
The while loop(1/3)
The syntax for the while loop is as follows:
while (loop-continuation-condition){ //loop
body Statement(s); }

Common errors in the loop-continuation-condition
oMake sure that the loop-continuation-condition eventually becomes
false to that the program will terminate. A common programmin error
involves infinite loops
oProgrammers often make mistakes to execut a loop one more or lees
time. For example instead of execute an statement 100 times the
statement is executed 101 times. The error will be in the condition
The while loop(2/3)
Activity: Writes a program that guess what a number a computer
has in mind. You will write a program that randomly gnerates an
integer between 0 and 100 inclusive. The program prompts the user
to enter a number continuously until the number matches the
randomly generated number. For each user input, the program tells
the user whether the input is too low or too high, so the user can
make the next guess intelligently.
The while loop(3/3)
Another common technique for controlling a loop is to designate a
special value when reading and processing a set of values.
oThis special input value, known as a sentinel value, signifies the
end of the loop
oA loop that uses a sentinel value to control its execution is called
a sentinel-controlled loop

Activity: Writes a program that reads and calculates the sum of an
unspecified number of integers. The input 0 signifies the end of the
input.
The do-while loop (1/2)
The do-while loop is a variation of the while loop. Its syntax is given
below:
oThe syntax for the while loop is as follows:
do{ //loop
body Statement(s); }while (loop-
continuation-condition)
The difference between a while loop and a do-while loop is the order in
which the loop-continuation-condition is evaluated and the loop body
executed.
Sometimes one is a more convenient choice than the other.
The do-while loop (2/2)
Activity: Rewrite the previous example using a do-while loop

Using the do-while loop if you have statements inside the loop that
must be executed at least once.
The for loop (1/1)
In general, the syntax of a for loop is as shown below:

for(initial-action; loop-continuation-condition; action_after_each-
iteration) {
//Loop body
Statements(s);
}

A for loop generally uses a variable to control how many times the
loop body is executed and when the loop terminates.
othis variable is referred to as a control variable,
othe initial-action often initializes a control variable,
othe action-after-each-iteration usually increments or decrements
the control variable ,
oand de loop-continuation-condition tests whether the control
variable has reached a termination value
Which Loop to Use?
The while loop and for loop are called pretest loops because the
continuation condition is checked before the loop body is executed.
The do-while loop is called a posttest loop because the condition is
checked after the loop body is executed
In general a while loop may be used if the number of repetitions is
not fixed
A do-while loop can be used to replace a while loop if the loop body
has to be executed before the continuation condition is tested
The for loop may be used if the number of repetitions is known in
advance

Nested Loops
Nested loops consist of an outer loop and one or more inner loops.
Each time the outer loop is repeated, the inner loops are reentered
and started a new.

Activity: Writes a program that displays in the computer screen a
multiplication table.

Keywords break and continue (1/2)
You have used the keyword break in a switch statement. You can
also use break in a loop to immediatly terminate the loop. Look at
the next program

public class TestBreak{
public static void main(String[] args) {
int sum=0;
int number=0;

while (number<20) {
number++;
sum+=number;
if (sum>=100)
break;
}
System.out.println("The number is "+number);
System.out.println("The sum is "+sum);
}

Keywords break and continue (1/2)
You can also use the continue keyword in a loop. When it is
encountered, it ends the current iteration. In other words,
continue breaks out of an iteration while the break keyword breaks
out of a loop
public class TestContinue{
public static void main(String[] args) {
int sum=0;
int number=0;

while (number<20) {
number++;
if (number==10)||(number==11)
continue;
sum+=number;
}
System.out.println("The number is "+number);
System.out.println("The sum is "+sum);
}

Exercises
Write a program that displays the following table (note that 1 mile is
1.609 kilometers)








Computing PI. You can approximate PI, by using the following
series:
Miles Kilometers
1 1.609
2 3.218
.. ...

9 14.481
10 16.090
Homework
1. Write a program that reads integers, finds the largest of them, and
counts its ocurrences. Assume that the input ends with number 0.
Suppose that you entered 3,5,2,5,5,5,0 the program finds that the
largest is 5 and the ocurrence count for 5 is 4.

1. A square is divided into four smaller regions as shown below in (a).
If you throw a dart into the square 1000000 times, what is the
probability for a dart to fall into an odd-numbered region? Write a
program to simulate the process and display the result. (Hint, Place
the center of the square in the center of a coordinate system, as
shown in (b). Randomly generate a point in the square and count
the number of times a point falls into an odd-numbered region)





(a) (b)
1
2
3
4
1
2
3
4
Cdigo para calcular pi
int terminos=100000;
double pi=0;
short signo=1;
for (int i=0;i<terminos;i++)
{
pi=pi+(signo*(1/((2.0*i)+1)));
signo*=-1;
}
System.out.println("El numero pi es "+pi*4.0);

Potrebbero piacerti anche