Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Using a For loop for repetition

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post Using a For loop for repetition

    I am trying to write a for-loop that lets the user enter how many times they want to repeat a given action.

    Basically the program asks the user how many washers or stoves they want to buy then calculates the cost of those stoves or washers. I am trying to include a for-loop that will let the user keep repeating the action as many times as they would like.

    Any feedback would be appreciated.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Using a For loop for repetition

    A normal for loop takes the following form
    for(declaration; condition; step)
     
    or
     
    for(int i = 0; i < 5; i++)

    How would you make the item loop 6 times, 7, 8, 9? What part of the for loop determines how many times it will loop? Is it the declaration, condition, or step?

    Answer this question and you are set. Just get an input from the user then set up the loop with the users input.

  3. The Following User Says Thank You to sunde For This Useful Post:

    cb5950 (March 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a For loop for repetition

    ok that makes sense, as long as the input doesn't exceed the condition, it will keep running correct?

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using a For loop for repetition

    Actually, as long as the condition is true the for loop will keep running. This can lead to some very interesting for-loops.

    for (int i = 5; i >= 0; --i) // a for loop which counts down
    {}
    int count = 0;
    for (int i = 0; count != 128; ++i) // a for loop which tests for a different number exactly equaling a number
    {}
     
    for(int i = 0;; ++i) // Java will automatically assume a true condition. This is an infinite for-loop

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a For loop for repetition

    Oh ok. I am trying to nest the for loop inside a switch with two cases.

    switch(choice)
    			{
    				case 1:
    				{
    					System.out.printf("Enter number of washers purchased: ");
    					washer=input.nextInt();
     
    				//for loop
     
    				totalWashers = a1.getPrice() * washer;
     
    					msg3 = String.format("You purchased %s washers for $%.2f\n", 
     
    washer, totalWashers);
     
    				JOptionPane.showMessageDialog(null, msg3);
     
    				break;
    				}
    				case 2:
    				{
    					System.out.printf("Enter number of stoves purchased: ");
    					stove = input.nextInt();
     
    				totalStoves = a2.getPrice() * stove;


    How would i make the for loop keep running as long as the number of washers or stoves is not zero?

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Using a For loop for repetition

    I would personally do something like this:

    import java.util.*;
     
    public class WashingMachine {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		System.out.println("Washing Machines(1) Stove(2)");
    		Scanner input = new Scanner(System.in);
    		int choice;
     
    		while(input.hasNext()){
     
    			choice = input.nextInt();
     
    			if(choice == 0){
    				break;
    			}
    			if(choice == 1){
    				System.out.println("Washing Machines");
    			}
    			if(choice == 2){
    				System.out.println("Stoves");
    			}		
     
    		}		
     
    	}
    }

    The 'while loop' will continue to loop until 0 is entered.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    cb5950 (March 8th, 2011)

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using a For loop for repetition

    Normally, I would discourage the use of break/continue statements used for something that could easily be fixed by changing the conditions. The reason is these statements (especially breaking to a label) encourage "spaghetti code", or code that's difficult to follow the logic of. While break/continue statements aren't anywhere near as bad as goto statements, they're still something you may want to avoid if possible. There are a few key exceptions to this rule (obviously having break statements in switches is almost always a must).

    In this case, this problem could be fixed by using a "1 and a half" while loop, or a do-while loop.

    // the do-while loop solution
    do
    {
        System.out.println("Washing Machines(1) Stove(2)");
        choice = input.nextInt();
        if(choice == 1){
    				System.out.println("Washing Machines");
    			}
    			if(choice == 2){
    				System.out.println("Stoves");
    			}	
    }
    while(input.hasNext() && choice != 0);

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    cb5950 (March 8th, 2011)

  11. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a For loop for repetition

    I appreciate your help

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  3. java Logic Random with out repetition
    By Jhovarie in forum Loops & Control Statements
    Replies: 1
    Last Post: January 13th, 2011, 03:25 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. [SOLVED] changing character repetition
    By vendetta in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 16th, 2010, 06:16 PM

Tags for this Thread