Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By fatabass
  • 1 Post By JosAH

Thread: repeat if statement in loop

  1. #1
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default repeat if statement in loop

    i want help in repeating if statement in loop
    i mean if it is a loop and has inner if statements this problem occur ...
    if first condition hold enter first if body then repeat loop
    when repeating the loop first condition is still hold it enter again in first if body
    and repeat again and so on
    loop becomes infinite loop because first if statement usually hold
    i want after entering first if in second iteration it enters in next if
    i hope you are understand me

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: repeat if statement in loop

    eng_hyzoom likes this.

  3. #3
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Re: repeat if statement in loop

    thanx fatabass
    for example
    int x=3;
    int y=2;
    int z=1;
    boolean ok = true;
    while(ok){
    if(x>y){
    x=4;
    y=3;
    System.out.println("hello");
    }
    else if(y>z){
    x=4;
    y=3;
    }
    }
    this infinite loop i want after finishing first if check second one and so on

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: repeat if statement in loop

    Quote Originally Posted by eng_hyzoom View Post
    i hope you are understand me
    Erm ... no, sorry.

    kind regards,

    Jos
    eng_hyzoom likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Re: repeat if statement in loop

    Quote Originally Posted by JosAH View Post
    Erm ... no, sorry.

    kind regards,

    Jos
    thanx Jos
    try this example and try to solve it
    int x=3;
    int y=2;
    int z=1;
    boolean ok = true;
    while(ok){
    if(x>y){
    x=4;
    y=3;
    System.out.println("hello");
    }
    else if(y>z){
    x=4;
    y=3;
    }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: repeat if statement in loop

    Then you need some way for the code to know it has already done 'x>y'.
    At the moment there is nothing there to say.

    TO be honest, I suspect your problem lies not in this, but in whatever it is you are actually trying to do that this is the apparent solution.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. If Else Statement in While Loop
    By rockintyler in forum New To Java
    Replies: 3
    Last Post: 02-23-2012, 11:33 PM
  2. Storing A Repeat Loop In An Array
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 07-22-2011, 05:57 AM
  3. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 04:39 PM
  4. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 06:19 PM
  5. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM

Posting Permissions

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