# LOOPS

Can anyone please explain me these two for loops step by step?

//first loop
for (int i=1; i<=n; i++){
for (int j=1; j<=i; j++){
body
}
}

//2nd loop
for (int i=1; i<=n; i++){
for (int j=i; j<=n; j++){
body
}
}

• : Can anyone please explain me these two for loops step by step?
:
: //first loop
: for (int i=1; i<=n; i++){
: for (int j=1; j<=i; j++){
: body
: }
: }
:
:
: //2nd loop
: for (int i=1; i<=n; i++){
: for (int j=i; j<=n; j++){
: body
: }
: }
:

FOR LOOP::
firstly it will initialize ur variable(i here) with 1.then check the test condition i.e. whether i<=n or not.if condition is true then it will enter the loop and now we counter another for loop which means that for every value of i,inner for loop will run n times and whatever b the body part will get executed n*n times.

• : : Can anyone please explain me these two for loops step by step?
: :
: : //first loop
: : for (int i=1; i<=n; i++){
: : for (int j=1; j<=i; j++){
: : body
: : }
: : }
: :
: :
: : //2nd loop
: : for (int i=1; i<=n; i++){
: : for (int j=i; j<=n; j++){
: : body
: : }
: : }
: :
:
: FOR LOOP::
: firstly it will initialize ur variable(i here) with 1.then check the
: test condition i.e. whether i<=n or not.if condition is true then it
: will enter the loop and now we counter another for loop which means
: that for every value of i,inner for loop will run n times and
: whatever b the body part will get executed n*n times.
:
:

THANKYOU very much sachin for the answer.
I have got another question. i wonder if u reply:). I have made a program but i have no idea if im supposed to do the way i have done it or if my program is totally right or wrong. Can u please check it.Here it goes;

///Write a program fragment using a while loop to do the dice game from earlier.Roll a dice until you roll a 6 - get as high a score as possible.
Assume you already have written a method that generates a rolls a dice giving a number between 1 and 6
roll = diceroll();///

import javax.swing.*;

public class Dice
{
public static void main(String[] param){

diceroll();
System.exit(0);
}

public static void diceroll(){
String userinput;
int rollnum;

userinput = JOptionPane.showInputDialog("Roll a dice and tell us what u got.");
rollnum = Integer.parseInt(userinput);

int score=0;

for(int i=1; i<=rollnum; i++){
while(rollnum!=6){

if(rollnum==1){score = score +1;}
else if(rollnum==2){score = score+2;}
else if(rollnum==3){score = score+3;}
else if(rollnum==4){score = score+4;}
else if(rollnum==5){score = score+5;}
else
JOptionPane.showMessageDialog(null,"You can only choose a number b/w 1 and 6.");

userinput = JOptionPane.showInputDialog("Roll a dice and tell us what u got.");
rollnum = Integer.parseInt(userinput);

}
}

System.out.println("Your score is " +score+ ".");
}
}

Please Do write to me if u think the program is wrong.
The question says that "Write a program fragment using a while loop to do the dice game FROM EARLIER".I stuck on this "From earlier" part. They have also given the example in the end of the question i.e roll = diceroll();
I don't know if roll is an integer or a method, if its a method then how can we save the earlier game and start a new game from this earlier game.

I know there are some other getters and setters ,and other different methods but i haven't reached till there. I have only done while, for loops,if else statements, basically the things which u can see on my program.

Thanks
jas
• Think of a dice as an object.

It is not quite the same as a physical dice, because it is not truly random. So it contains a random number generator.

It's got a few public methods. A constructor (for extra generality, pass in the number of faces) and a roll method. The roll method calls the random number generator, does some calculations to put the result into the right range, and returns an integer 1 ... Nfaces.

Then we need a few more programming methods. We need to set the seed to random, using the clock as the new seed. We need to pass in a new seed. These aren't too difficult to provide.

Finally we need to serialise our dice to store in on disk. This is a bit tricky. Essentially you need to serialise the random number generator it contains, using its serialise method, then serialise the number of faces, then serialise any other data members you mgith have added.

When we load or deserialise the dice, the state is restored. So any series of rolls will almways produce the same random numbers, until we call one of the seed-setting methods.

• : Think of a dice as an object.
:
: It is not quite the same as a physical dice, because it is not truly
: random. So it contains a random number generator.
:
: It's got a few public methods. A constructor (for extra generality,
: pass in the number of faces) and a roll method. The roll method
: calls the random number generator, does some calculations to put the
: result into the right range, and returns an integer 1 ... Nfaces.
:
: Then we need a few more programming methods. We need to set the seed
: to random, using the clock as the new seed. We need to pass in a new
: seed. These aren't too difficult to provide.
:
: Finally we need to serialise our dice to store in on disk. This is a
: bit tricky. Essentially you need to serialise the random number
: generator it contains, using its serialise method, then serialise
: the number of faces, then serialise any other data members you mgith
:
: When we load or deserialise the dice, the state is restored. So any
: series of rolls will almways produce the same random numbers, until
: we call one of the seed-setting methods.
:
: Thanks Malcolm, i really appreacite the way u have explained to me, now i have understood it completely:)
:
:
: