943,742 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2007
  • Java RSS
Sep 3rd, 2009
0

loop within a loop

Expand Post »
may someone give me a sample code of loop within a loop, and other that I may use for decision in my program.
Similar Threads
Reputation Points: -1
Solved Threads: 0
Junior Poster in Training
coud_ren_26 is offline Offline
67 posts
since Aug 2009
Sep 3rd, 2009
0

Re: loop within a loop

I think u should provide some code, then we can help when u get stuck.
Reputation Points: 36
Solved Threads: 57
Posting Whiz
Thirusha is offline Offline
355 posts
since Mar 2008
Sep 3rd, 2009
0

Re: loop within a loop

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 3rd, 2009
0

Re: loop within a loop

Here is how i can explain loop within a loop(nested loop)
Java Syntax (Toggle Plain Text)
  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
.
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Sep 3rd, 2009
0

Re: loop within a loop

//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);}




}
}
}
Reputation Points: -1
Solved Threads: 0
Junior Poster in Training
coud_ren_26 is offline Offline
67 posts
since Aug 2009
Sep 3rd, 2009
0

Re: loop within a loop

if he wants to be deliver he will choose 1 and I want the milk[] will be printed all.
Reputation Points: -1
Solved Threads: 0
Junior Poster in Training
coud_ren_26 is offline Offline
67 posts
since Aug 2009
Sep 3rd, 2009
0

Re: loop within a loop

Please take a look with my code. Help me please.
Reputation Points: -1
Solved Threads: 0
Junior Poster in Training
coud_ren_26 is offline Offline
67 posts
since Aug 2009
Sep 3rd, 2009
1

Re: loop within a loop

JAVA Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: working with netbeans
Next Thread in Java Forum Timeline: Making Your Own Programming Language





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC