Results 1 to 6 of 6
Like Tree3Likes
  • 3 Post By Fubarable

Thread: What is equivalent for loop?

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default What is equivalent for loop?

    int i = 0;
    int sum = 0;

    while (i < 4) {
    if (i++ % 3 == 0)
    continue;
    sum += i;
    }

    Explain output?
    Last edited by diamonddragon; 01-14-2012 at 08:09 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: What is equivalent for loop?

    That was my first, don't You see? :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: What is equivalent for loop?

    Quote Originally Posted by diamonddragon View Post
    That was my first, don't You see? :)
    You won't learn if you don't put in effort, and I'm sure that you wouldn't have posted this if you didn't try to solve it first. So please show us the fruits of your efforts. It will also give us a better idea of what you do and don't understand. Also we frown on folks who just dump assignments in here without first showing their work, so help us all out by changing this.

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: What is equivalent for loop?

    Here is one solution, and wonder if there is better one?

    int i, sum;

    for (i = 0, sum = 0; i < 4;) {
    if (i++ % 3 != 0)
    sum += i;
    }

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

    Default Re: What is equivalent for loop?

    A while loop:

    Java Code:
    wile (<condition>)
       <statement>;
    can always be transformed to a for loop as follows:

    Java Code:
    for(; <condition>; )
       <statement>;
    But I don't think you had this in mind ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. HQL equivalent to this Sql Query
    By shomid in forum Advanced Java
    Replies: 0
    Last Post: 10-27-2011, 09:43 AM
  2. Equivalent of PHP isset in JAVA
    By africanhacker in forum New To Java
    Replies: 11
    Last Post: 03-24-2011, 03:41 PM
  3. Machiuke equivalent in J2ME
    By kimhonoridez in forum New To Java
    Replies: 3
    Last Post: 01-07-2011, 02:50 AM
  4. VB Net FIX equivalent?
    By bullstreetboy in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 06:31 PM
  5. Replies: 1
    Last Post: 11-21-2007, 01:18 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
  •