/* * @topic T00420 Multiply two numbers by adding them * @brief Demonstrates endless loop for( ; ; ) and while( condition ) */ package multiply; import java.util.Scanner; public class MultiplyMain { private static Scanner sc = new Scanner( System.in ); public static int multiply( int multiplicand, int multiplier) { int count = 0; int result = 0; while ( count < multiplier ) { result = result + multiplicand; count = count + 1; } return result; }//multiply public static void main(String[] args) { System.out.println( "Enter all zeroes to exit!" ); for(;;) { // while( true ) System.out.print( "multiplicand: " ); int multiplicand = sc.nextInt(); System.out.print( " multiplier: " ); int multiplier = sc.nextInt(); if ( ( multiplicand == 0 ) && ( multiplier == 0 ) ) { break; } int result = multiply( multiplicand, multiplier ); System.out.println( result ); }//endless loop //... the line where break takes us }//main }//class MainApp /* //2 * 5 //2 + 2 + 2 + 2 + 2 int count = 0; int result = 0; //repeat: while ( count < 5 ) { result = result + 2; //0+2 +2 +2 +2 +2 count = count + 1; // 1 2 3 4 5 //if ( count < 5 ) //goto repeat; } */