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 5 of 5

Thread: for loop and while loop problems

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default for loop and while loop problems

    Write a program to do the following:
    · Use a for loop to sum all of the numbers between 1 and 50 that are divisible by 5.
    · Use a while loop to sum all of the numbers between 1 and 50 that are divisible by 5.
    Check that you are getting the answer you expected!

    Hi im getting a compile error for my for loop and ive no idea where to start with the while loop can anybody could point me in the right direction for my while loop

    class WS1Q2{
    	public static void main(String[] args){
    		int x, total= 0;
    		for(x=1;x<=50;x++)
    		{
    			if(x / 5)
    			{
    				total = total + x;
    			}
    		}
    		System.out.println("The total is "+total);
    	}//close main
    }//close class


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop and while loop problems

    Try this (didn't compile it but i think it should work that way). And by the way, try to read the errors most time they will tell you whats wrong

    "if (x / 5) {" -> x / 5 is no boolean expression, inside the brackets of an if-condition there must always be an expression which should be true or false

    class WS1Q2{
    	public static void main(String[] args){
    		int total= 0;
    		for(int x=1;x<=50;x++) // as x is only need inside the loop initialize it there
    		{
    			if((x % 5) == 0) // % is modulo function, if x is dividable by 5 it will be zero
    			{
    				total = total + x;
    			}
    		}
    		System.out.println("The total is "+total);
    	}//close main
    }//close class

  3. #3
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: for loop and while loop problems

    If you think about it, you can easily turn any for loop into a while loop.
    Consider the following:
    for(initialise; condition; increment)
    {
        //stuff
    }
    How can you write that as a while loop? I'm sure you can figure it out!

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop and while loop problems

    Here is a total solution incase you are still having difficulties


    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int total = 0;
            for(int i = 0; i <= 50; i++){
                if(i%5 == 0){
                    total = total + i;
                }
                System.out.println("i is " + i + " Total is " + total);
            }
            total = 0;
            int x = 0;
            while(x <= 50){
                if(x%5 == 0){
                    total = total + x;
                }
                x++;
                System.out.println("x is " + x + " total is " + total);
            }
     
        }
     
    }

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: for loop and while loop problems

    all your post are very good!

Similar Threads

  1. Need help with do-while loop
    By sk8rrr in forum Loops & Control Statements
    Replies: 2
    Last Post: February 10th, 2010, 07:33 PM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  3. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  4. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM