may someone give me a sample code of loop within a loop, and other that I may use for decision in my program.

## All 7 Replies

I think u should provide some code, then we can help when u get stuck.

Run this:

``````int N = 4;
int M = 3;

for (int i=0;i<N;i++) {
System.out.println("I= "+i);

for (int j=0;j<M;j++) {
System.out.[B]print[/B]("J="+j+" ");
} // end inside loop

System.out.println();
} // end outside loop``````

Here is how i can explain loop within a loop(nested loop)

``````for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
......
}
}``````

when i=0 j goes from 0 to 3// both working as counters
when i=1 j again goes from 0 to 3
.
.
when i=3 j will go again from 0 to 3
think this way

for an exercise try sorting 5 numbers in ascending order u will need it in future(using nested loop).Use arrays ..arrays commonly need such a concept
.

//this is my unfinished code

``````import java.util.Scanner;
public class listProduct {
public static void main (String[] args){

int productChoice;{
Scanner scanAnything = new Scanner(System.in);
System.out.print("Processed Milk");
System.out.print("\t\t\t\t" + "Price");
System.out.println("\t\t" + "grams/size");
System.out.println("");
System.out.println("Condensed");
System.out.println("");

Double[] price = new Double[100];

price [1] = 46.30;
price [2] = 52.10;
String[] milk = new String [51];
milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml";
milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml";

milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml";
milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml";
milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml";

milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g";
milk [6] ="6 - Bear Brand Filled Milk  " + "\t\t" + "51.00" + "\t\t" + "180g";
milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g";

milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g";
milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g";

for (int r=0; r<=1; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Evaporated");
System.out.println("");
for (int r=2; r<=4; r++)
System.out.println(milk [r] +"\t\t");

System.out.println("");
System.out.println("Powdered");
System.out.println("");
for (int r=5; r<=7; r++)
System.out.println(milk [r] +"\t\t");

System.out.println("");
System.out.println("Coffee-refill");
System.out.println("");
for (int r=8; r<=9; r++)
System.out.println(milk [r] +"\t\t");

productChoice = scanAnything.nextInt();
if (productChoice == 0){
System.out.println("Alaka Filled Milk is selected");
System.out.println("Do you want to include more items?");
System.out.println("1 - yes!");
System.out.println("2 - no!");

}else if(productChoice == 1){
System.out.println("Milkmaid Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 2){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 3){
System.out.println("Angel Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 4){
System.out.println("Alphine Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 5){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 6){
System.out.println("Bear Brand Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 7){
System.out.println("Nido Fortified Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 8){
System.out.println("Bleand 45 is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 9){
System.out.println("Great Taste (granules) is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 10){
System.out.println("Nescafe Classic is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 11){
System.out.println("CDO Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 12){
System.out.println("Purefoods Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 13){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 14){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 15){
System.out.println("CDO corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 16){
System.out.println("Swift Juicy corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 17){
System.out.println("Purefoods corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 18){
System.out.println("Ligo corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 19){
System.out.println("Lucky Me instant noodles is selected");
System.out.println("Do you want to include more items?");
}else{
System.out.println("No such item");
System.out.println("Do you want to choose again?");
System.exit(1);}

}
}
}
``````

if he wants to be deliver he will choose 1 and I want the milk[] will be printed all.

Please take a look with my code. Help me please.

``````import java.util.Scanner;
public class listProduct {
public static void main (String[] args){

int productChoice;{
Scanner scanAnything = new Scanner(System.in);
System.out.print("Processed Milk");
System.out.print("\t\t\t\t" + "Price");
System.out.println("\t\t" + "grams/size");
System.out.println("");
System.out.println("Condensed");
System.out.println("");

Double[] price = new Double[100];

price [1] = 46.30;
price [2] = 52.10;
String[] milk = new String [51];
milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml";
milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml";

milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml";
milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml";
milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml";

milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g";
milk [6] ="6 - Bear Brand Filled Milk  " + "\t\t" + "51.00" + "\t\t" + "180g";
milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g";

milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g";
milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g";

for (int r=0; r<=1; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Evaporated");
System.out.println("");
for (int r=2; r<=4; r++)
System.out.println(milk [r] +"\t\t");

System.out.println("");
System.out.println("Powdered");
System.out.println("");
for (int r=5; r<=7; r++)
System.out.println(milk [r] +"\t\t");

System.out.println("");
System.out.println("Coffee-refill");
System.out.println("");
for (int r=8; r<=9; r++)
System.out.println(milk [r] +"\t\t");

productChoice = scanAnything.nextInt(); // dont scan anything  ask the user to choose between 0 to 19 use switch case instead of so many if else or an array
if (productChoice == 0){
System.out.println("Alaka Filled Milk is selected");
System.out.println("Do you want to include more items?");
System.out.println("1 - yes!");
System.out.println("2 - no!");

}else if(productChoice == 1){
System.out.println("Milkmaid Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 2){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 3){
System.out.println("Angel Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 4){
System.out.println("Alphine Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 5){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 6){
System.out.println("Bear Brand Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 7){
System.out.println("Nido Fortified Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 8){
System.out.println("Bleand 45 is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 9){
System.out.println("Great Taste (granules) is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 10){
System.out.println("Nescafe Classic is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 11){
System.out.println("CDO Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 12){
System.out.println("Purefoods Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 13){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 14){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 15){
System.out.println("CDO corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 16){
System.out.println("Swift Juicy corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 17){
System.out.println("Purefoods corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 18){
System.out.println("Ligo corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 19){
System.out.println("Lucky Me instant noodles is selected");
System.out.println("Do you want to include more items?");
}else{
System.out.println("No such item");
System.out.println("Do you want to choose again?");
System.exit(1);}

}
}
}``````

for now dont include the option ..if user wants to include more items

u seem to have repeated the code.Use a simple switch case

``````System.out.println("enter ur choice between 0 to 19");
//scan (productchoice)
swicth(productchoice)
{
case 0:
System.out.println("System.out.println("Alaka Filled Milk is selected");//add whatever u want here
break;
case 1:
....
break;``````

if u r done with that we will work out with the option for more product inclusions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.