We used functions like exit() in C++ to abnormally terminate the execution of program, which function can we use in Java. For ex :- In following program I want to terminate the execution as soon the value of i is printed for 1st time.
//following is the program :
class Lcm {
public static void main( String args[] ) {
int a = Integer.parseInt( args[0] );
int b = Integer.parseInt( args[1] );
for ( int i=1 ; i<=a*b ; i++ ) {
if ( i%a==0 && i%b==0 ) {
System.out.println( "lcm is: " + i );
/* i want to terminate the program
* when the value of i is printed
* for very 1st time
*/
}
}
}
}
}
Use break
to get out of the loop and let the function end naturally, or a plain return
to exit the function altogether.
Perhaps it is finally time to learn to control your flow without goto
statements and their equivalents (exit
, longjmp
, try..catch
used for flow control etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With