简体   繁体   中英

do-while loop using IF

I am using if condition statement with a do while loop. I want that everytime when the compiler runs the program it should print the statement in the" IF " till it reaches less then 15 what is happening is that the job is going quite well in do-while loop but the statement in" IF " only printed once? Where am i going wrong. If someone explains the flow it would be really appreciated as i am a beginner. Please see below for the code :

package loopexamples2;

public class DoWhileLoop {

public static void main(String[] args) {
    int n = 1;
    if(n<15)

        System.out.println("print value of n is equal to"+ n);

    do {
     System.out.println(n);
     n++;
    }
     while(n<10);           
    }
}

output--> print value of n is equal to1 1 2 3 4 5 6 7 8 9

Your if statement is outside your do while loop. Put it inside the do {} block

package loopexamples2;

public class DoWhileLoop {

public static void main(String[] args) {
    int n = 1;

    do {
     if(n<15) {
        System.out.println("print value of n is equal to"+ n);
     }
     System.out.println(n);
     n++;
    }
     while(n<10);           
    }
}

You're looking for something closer to this:

while(n<15) {
    System.out.println("print value of n is equal to"+ n);
    n++
}

The block of code within { and } will be run constantly until n reaches a value greater or equal to 15.

With your current code, the full print statement is only output once, as it isn't included in your while loop.

I guess this is what you want:

package loopexamples2;

public class DoWhileLoop {

    public static void main(String[] args) {
        int n = 1;        
        do {
           System.out.println("print value of n is equal to"+ n);
           n++;
        } while(n<10);           
    }
}

Code flow:

int n = 1; Assigns n to 1

do { Repeat code between braces from here until while .

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to1

n++; Increments n by 1, so n = 2

} while(n<10); Is n < 10? Yes so repeat from do

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to2

n++; Increments n by 1, so n = 3

} while(n<10); Is n < 10? n = 3 so yes, repeat from do

[ ...same thing repeats until... ]

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to9

n++; Increments n by 1, so n = 10

} while(n<10); Is n < 10? n = 10 so no, exit loop.

End of program.

The statement inside the IF is passed only once. Since it is not inside any loop.

The flow of the program is procedural. So the execution is from top to bottom. It first pass the IF block, executes it, procedes to the next line, finds the do-while block which is loop until the while condition is met. Loops only iterates on it's block and not on lines before it.

I explain the workflow, since you are a beginner:

    int n = 1;

1 is assigned to n .

    if(n<15)
        System.out.println("print value of n is equal to"+ n);

The program check if n is less than 15. it is the case, so it execute the action: print "print value of n is equal to" + n. It only does that once. It's not a while or a for loop that execute a loop repeatedly.

    do {
     System.out.println(n);
     n++;
    }
     while(n<10);           
    }

Here the program execute everything between the { and the } until n=10.

You are saying "it should print the statement in the" IF " till it reaches less then 15"

Then you need a similar do/while loop:

 do {
     System.out.println("print value of n is equal to"+ n);
     n++
 }
 while (n<15);

I want that everytime when the compiler runs the program it should print the statement in the" IF " till it reaches less then 15

Since you are using n<10 condition in do while loop , the loop will not proceed beyond n=9 . So it won't reach n=15 .

First off you need to put the if() statement INSIDE the do while loop. otherwise the program only sees the if once.

the next thing you're doing is in your statement:

do {
    System.out.println(n);
n++;
}
while(n<10);

you have it only going until n is less then 10, you want it to be less then 15.

I assume you wan't it to print out 15 as well so you can change

while(n < 10)

to

while(n <= 15)

so all together your code would look like this:

package loopexamples2;

public class DoWhileLoop {

public static void main(String[] args) {
    int n = 1;

    do {
        if(n <= 15){
            System.out.println("The value of n is equal to " + n);
            n++;
    } while(n <= 15);
}
}

note <= means "less then or equal to"

You can refer this any time... It will be more helpful to you..

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM