Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i exit an if statement in java?

Tags:

java

How do I exit an if statement and continue the loop when the if statement is in the loop? I tried break but it didn't work. Continue just made the program to keep asking me to input a number.

import java.io.*;
public class pg48
{
    public static void main()throws IOException
    {
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        System.out.println("Type a num: ");
        int n = Integer.parseInt(in.readLine());
        int pro = 1,i,d;
        for (; n != 0; )
        {
            d = n%10;
            if (d == 0)
            {
                break;
            }
            pro = pro * d;
            n = n/10;
        }
        System.out.println("Product: "+pro);
    }
}
like image 432
Super Maniac Avatar asked Nov 27 '25 18:11

Super Maniac


1 Answers

You just need to write the method in this way: public static void main(String args[]) If you want to use continue;, you have to move n = n / 10; line before if statement.

like image 86
hadiafifi Avatar answered Nov 29 '25 09:11

hadiafifi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!