-1
//        int i=0;
//        while(i<5){
//            System.out.println(i);
//            System.out.println("Java is great.");
//            if (i==2){
//                System.out.println("Ending the Loop.");
//                break;
//
//            }
//          i++;    //DOUBT:WHEN I WRITE i++ AFTER 4TH LINE WHY "2" IS NOT PRINTED IN OUTPUT.
//        }



//       int i=0;
//        do{
//            System.out.println(i);
//            System.out.println("Java is Great.");
//            if (i==2){
//                System.out.println("Ending the Loop.");
//                break;
//            }
//            i++;
//        } while (i<5);



//        for (int i=0; i<50; i++){
//            if (i==2){
//                System.out.println("Ending the Loop");
//                continue;
//            }
//            System.out.println(i);    
//            System.out.println("Java is Great.");
//        }


        int i=0;
        do{
            i++;
            if(i==2){
                System.out.println("Ending the loop.");
                continue;
            }
            System.out.println(i);
            System.out.println("Java is Great.");
        }while(i<5);
        //DOUBT:WHY 5 IS GETTING PRINTED IN THIS EVEN IF THE CONDITION IS (i<5).

Basically in all these codes my doubt is how can i decide the exact posiiton of certain codes to get the appropriate results. Like when i write i++; above the if statement and after the if statement then different results gets printed.

  • What is the question? Do you not know the difference between break and continue? – XtremeBaumer Nov 23 '22 at 05:41
  • no,i know the difference but i am not able to understand,how to decide the position of the codes like where I have to write i++ before if statements and where after if statements and In the last one why 5 is getting printed even after the condition is i<5 – Om Kumar Rath Nov 23 '22 at 06:36

2 Answers2

1

I am afraid the code is correct.

do{
    i++;
    if(i==2){
        System.out.println("Ending the loop.");
        continue;
    }
    System.out.println(i); 
    System.out.println("Java is Great.");
}while(i<5);

As far as this code is concerned, when 4<5 condition is true code executes the do block and increments the variable i and then prints it, thus the value 5.

I suggest you use the while condition and read about it.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sera
  • 31
  • 5
  • so if i place i++ outside the do block,5 won't get printed and the program will end after 4??? – Om Kumar Rath Nov 23 '22 at 06:39
  • and why u r afraid if the code is correct, may I know where did you notice a fault (Thank You). – Om Kumar Rath Nov 23 '22 at 06:39
  • Hi there Om, @laban_luka has explained it very well, the while() condition is checked first, if it's true then the do code is executed. and in do {} code you are incrementing the i variable to value 5. – sera Nov 23 '22 at 11:20
0

I understand what are you trying to ask.

Like when i write i++; above the if statement and after the if statement then different results gets printed

This is because when you add i++ after the if statement.

First time if will be skipped because i = 0, then comes i++, second time it will be skipped as well because i = 1, then comes i++ again. Now i = 2 and you enter if statement. In if statement you have continue which means "skip rest of the loop and start next cycle". In next cycles i will be 2 forever because you are not reaching part of the code that increments it. So either add i++ in if statement, before continue or keep i++ at start as you have in example.

If you want 4 to be last number printed then use while() loop instead of do-while() because in do-while() you always increment first and evaluate later.

laban_luka
  • 399
  • 1
  • 4
  • 11