loop within a loop

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 51
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

loop within a loop

 
0
  #1
Sep 3rd, 2009
may someone give me a sample code of loop within a loop, and other that I may use for decision in my program.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 318
Reputation: Thirusha is an unknown quantity at this point 
Solved Threads: 51
Thirusha's Avatar
Thirusha Thirusha is offline Offline
Posting Whiz

Re: loop within a loop

 
0
  #2
Sep 3rd, 2009
I think u should provide some code, then we can help when u get stuck.
Life... Is a Moment
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: loop within a loop

 
0
  #3
Sep 3rd, 2009
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.print("J="+j+" ");
  } // end inside loop

  System.out.println();
} // end outside loop
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: loop within a loop

 
0
  #4
Sep 3rd, 2009
Here is how i can explain loop within a loop(nested loop)
  1.  
  2. for(int i=0;i<4;i++)
  3. {
  4. for(int j=0;j<4;j++)
  5. {
  6. ......
  7. }
  8. }

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
.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: loop within a loop

 
0
  #5
Sep 3rd, 2009
//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);}




}
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: loop within a loop

 
0
  #6
Sep 3rd, 2009
if he wants to be deliver he will choose 1 and I want the milk[] will be printed all.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: loop within a loop

 
0
  #7
Sep 3rd, 2009
Please take a look with my code. Help me please.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: loop within a loop

 
1
  #8
Sep 3rd, 2009
  1.  
  2. import java.util.Scanner;
  3. public class listProduct {
  4. public static void main (String[] args){
  5.  
  6. int productChoice;{
  7. Scanner scanAnything = new Scanner(System.in);
  8. System.out.print("Processed Milk");
  9. System.out.print("\t\t\t\t" + "Price");
  10. System.out.println("\t\t" + "grams/size");
  11. System.out.println("");
  12. System.out.println("Condensed");
  13. System.out.println("");
  14.  
  15.  
  16. Double[] price = new Double[100];
  17.  
  18.  
  19. price [1] = 46.30;
  20. price [2] = 52.10;
  21. String[] milk = new String [51];
  22. milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml";
  23. milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml";
  24.  
  25. milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml";
  26. milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml";
  27. milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml";
  28.  
  29. milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g";
  30. milk [6] ="6 - Bear Brand Filled Milk " + "\t\t" + "51.00" + "\t\t" + "180g";
  31. milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g";
  32.  
  33. milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g";
  34. milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g";
  35.  
  36.  
  37.  
  38. for (int r=0; r<=1; r++)
  39. System.out.println(milk [r] +"\t\t");
  40. System.out.println("");
  41. System.out.println("Evaporated");
  42. System.out.println("");
  43. for (int r=2; r<=4; r++)
  44. System.out.println(milk [r] +"\t\t");
  45.  
  46. System.out.println("");
  47. System.out.println("Powdered");
  48. System.out.println("");
  49. for (int r=5; r<=7; r++)
  50. System.out.println(milk [r] +"\t\t");
  51.  
  52. System.out.println("");
  53. System.out.println("Coffee-refill");
  54. System.out.println("");
  55. for (int r=8; r<=9; r++)
  56. System.out.println(milk [r] +"\t\t");
  57.  
  58.  
  59.  
  60.  
  61. 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
  62. if (productChoice == 0){
  63. System.out.println("Alaka Filled Milk is selected");
  64. System.out.println("Do you want to include more items?");
  65. System.out.println("1 - yes!");
  66. System.out.println("2 - no!");
  67.  
  68.  
  69. }else if(productChoice == 1){
  70. System.out.println("Milkmaid Full Cream Milk is selected");
  71. System.out.println("Do you want to include more items?");
  72. }else if(productChoice == 2){
  73. System.out.println("Alaska Filled Milk is selected");
  74. System.out.println("Do you want to include more items?");
  75. }else if(productChoice == 3){
  76. System.out.println("Angel Filled Milk is selected");
  77. System.out.println("Do you want to include more items?");
  78. }else if(productChoice == 4){
  79. System.out.println("Alphine Full Cream Milk is selected");
  80. System.out.println("Do you want to include more items?");
  81. }else if(productChoice == 5){
  82. System.out.println("Alaska Filled Milk is selected");
  83. System.out.println("Do you want to include more items?");
  84. }else if(productChoice == 6){
  85. System.out.println("Bear Brand Filled Milk is selected");
  86. System.out.println("Do you want to include more items?");
  87. }else if(productChoice == 7){
  88. System.out.println("Nido Fortified Full Cream Milk is selected");
  89. System.out.println("Do you want to include more items?");
  90. }else if(productChoice == 8){
  91. System.out.println("Bleand 45 is selected");
  92. System.out.println("Do you want to include more items?");
  93. }else if(productChoice == 9){
  94. System.out.println("Great Taste (granules) is selected");
  95. System.out.println("Do you want to include more items?");
  96. }else if(productChoice == 10){
  97. System.out.println("Nescafe Classic is selected");
  98. System.out.println("Do you want to include more items?");
  99. }else if(productChoice == 11){
  100. System.out.println("CDO Chinese Style luncheon meat is selected");
  101. System.out.println("Do you want to include more items?");
  102. }else if(productChoice == 12){
  103. System.out.println("Purefoods Chinese Style luncheon meat is selected");
  104. System.out.println("Do you want to include more items?");
  105. }else if(productChoice == 13){
  106. System.out.println("Argentina corned beef is selected");
  107. System.out.println("Do you want to include more items?");
  108. }else if(productChoice == 14){
  109. System.out.println("Argentina corned beef is selected");
  110. System.out.println("Do you want to include more items?");
  111. }else if(productChoice == 15){
  112. System.out.println("CDO corned beef is selected");
  113. System.out.println("Do you want to include more items?");
  114. }else if(productChoice == 16){
  115. System.out.println("Swift Juicy corned beef is selected");
  116. System.out.println("Do you want to include more items?");
  117. }else if(productChoice == 17){
  118. System.out.println("Purefoods corned beef is selected");
  119. System.out.println("Do you want to include more items?");
  120. }else if(productChoice == 18){
  121. System.out.println("Ligo corned beef is selected");
  122. System.out.println("Do you want to include more items?");
  123. }else if(productChoice == 19){
  124. System.out.println("Lucky Me instant noodles is selected");
  125. System.out.println("Do you want to include more items?");
  126. }else{
  127. System.out.println("No such item");
  128. System.out.println("Do you want to choose again?");
  129. System.exit(1);}
  130.  
  131.  
  132.  
  133.  
  134. }
  135. }
  136. }

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

  1.  
  2. System.out.println("enter ur choice between 0 to 19");
  3. //scan (productchoice)
  4. swicth(productchoice)
  5. {
  6. case 0:
  7. System.out.println("System.out.println("Alaka Filled Milk is selected");//add whatever u want here
  8. break;
  9. case 1:
  10. ....
  11. break;
  12.  
if u r done with that we will work out with the option for more product inclusions
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 448 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC