Results 1 to 2 of 2
  1. #1
    kedecr is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default While loop problem

    how do I make this for loop into a while loop?

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter start number: ");
    int start = sc.nextInt();
    System.out.print("Enter last number: ");
    int end = sc.nextInt();
    System.out.println();
    int count = 0;
    for (int i = start; i <= end; i+=3) {
    System.out.print(i + " ");
    count++;
    if (count==10) {
    count = 0;
    System.out.println();
    }
    }


    // it prints every third number (user input numbers).... and I want to do the same with a while loop but I can't figure it out...help please!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    As you could've known from your text book, a for-loop like this:

    Java Code:
    for (<init>; <cond>; <incr>) <statement>;
    is equalvalent to a while-loop like this:

    Java Code:
    <init>;
    while (<cond>) {
       <statement>;
       <incr>;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problem with a for loop
    By ile4 in forum New To Java
    Replies: 3
    Last Post: 12-02-2010, 03:23 PM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  3. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 08:16 PM
  4. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 AM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •