I'm a student in an into-level to intermediate-level Java programming class. In one of the feedbacks I received the teacher said -
"Break statements should be avoided unless you have a very good reason to use it. Break statements makes sense if you are iterating through 10000 values to look for a specific value, and you want to stop after you find it."
below is the the assignment description and the code I had submitted. I'm curious to know if anyone can elaborate on the why of what my teacher said.
/*
Assignment Description:
Write a Java program asking the user to keyin an integer number and calculate the square of that number using
(Squared with addition) method.
Example Output:
Enter number for squaring: 6
1 + 3 + 5 + 7 + 9 + 11 = 36
Do you want to repeat for another number (Y/N): n
Bye…
*/
import java.util.Scanner; // for user input
public class Project5 {
/*
squareInput method will use the addition method from assignment description to square the desired number.
*/
static int[] squareInput(int input) {
//setup the variables needed.
int loopCounter = input - 1 ; //loopCounter is the number to be squared.(keeps "input" pure). is -1 bc the #1 is added in the array setup.
int accumulator = 1; // the first odd number.
int addend = 1; // the first number to be added starts as 1 and is incrimented in the loop.
int[] returnedOddNumbers = new int [input + 1]; // the array of odd values cycled through while adding. (return value)
// is +1 bc the total of the square is the last element.
int index = 1; // the array index so we can set elements as a loop. 0 is set to the value 1 below.
returnedOddNumbers[0] = 1 ; // the initial value in the array is 1.
// the loop will use the variable to create a list where the final index is the product and previous indexes are the addends.
while ( loopCounter > 0 ) {
addend += 2; // add 2 means we are going to the next odd number so 3 on first itteration.
accumulator += addend; // add the new odd value to the accumulator/total.
returnedOddNumbers[index] = addend ; // add the addend at the index.
index += 1; // add one to the index.
loopCounter -= 1; // reduce the counter for the loop. (last Opperation)
}
returnedOddNumbers[input] = accumulator; // add the final "total value to the list"
return returnedOddNumbers;
}
/*
check input method to change a number from neg to pos if given a neg number.
*/
static int checkInput(int input) {
// set x with the input. if x is negative make it positive. return x.
int x = input ;
if ( x < 0 ) {
x = x * -1;
}
return x;
}
/*
The display results method will take the list and display the contents as described in the assignment directions.
*/
static String displayResults(int[] input) {
// set the variables we will need to make an output string.
int itterate = input.length ; // set a loop counter var "itterate"
int index = 0; // the initial index we look at.
String output = "";
// while loop will take the contents of the array and apend them to a string. not the final "total" so > 1.(last index)
while (itterate > 2) {
itterate -= 1 ; // -1 from loop counter.
int contents = input[index]; // get array contents at "index".
index += 1; // get the next index contents next loop.
output = (output + contents + " + "); // remake the string with old string and add "contents" and a "+"
}
int contents = input[index]; // contents fot the second to last index are set.
output = (output + contents); // the second to last index is added but doesnt get a "+" sign.
index += 1; // set index for the final index.
output = (output + " = " + input[index]); // remake the string with old string and add "=" and the contents at the final index(total)
//print the output with an empty line below.
System.out.println(output);
System.out.println();
return output;
}
/*
The userContinueProgram() Method will check for user Y/N inputs and repeat question if not Y/N or y/n.
*/
static char userContinueProgram(char input) {
Scanner input2 = new Scanner(System.in); // input for the user continue method.
while ( input != 'y' && input != 'Y' && input != 'n' && input != 'N' ) {
System.out.println(""); // keeps console looking uniform and easy to read if doing multiple opperations.
System.out.print( input + " is not a recognized input. Please try again.\nEnter Y/y to try another number Enter N/n to quit: "); // if input is not y/n then retry.
input = input2.next().charAt(0); // collect another char from the user.
}
// if statment adds a space to the console if they are doing another number so it looks uniform.
if ( input == 'y' || input == 'Y' ) {
System.out.println("");
}
// return the y/n input.
return input;
}
/*
The getNumeric() method will see if an input is numeric and if it is return the input as an int.
*/
static int getNumeric() {
int numberEntered = 0; // create an int variable.
boolean isInt = false; // create a boolean variable for the loop.
// while fales we try to get a numeric input from the user.
while (isInt == false) {
// try to collect a numeric input.
try {
Scanner input3 = new Scanner(System.in);
numberEntered = input3.nextInt();
isInt = true ;
}
// if non numeric input is found the loop the loop repeats.
catch (Exception e) {
System.out.print("Your entry is not an integer/whole number. Please enter a number: ");
}
}
// return the numeric value.
return numberEntered;
}
/*
Main Method will use the above methods to check user inputs for validity as well as loop and create a persistent program.
*/
public static void main (String[] args) {
Scanner input = new Scanner(System.in); // Scanner obj.
//set a while true loop to get the user input and create a persistent program.
char continueProgram = 'Y';
while ( continueProgram != 'n' && continueProgram != 'N') {
//ask for and store the number the user entered using the getNumeric as it has error catching.
System.out.print("Enter number for squaring: ");
int numberEntered = getNumeric();
//check if the number is zero. If it is tell the user.
if (numberEntered == 0) {
System.out.println("Zero Squared is Zero.\n");
// Ask if the user wishes to enter another value or quit.
System.out.print("Do you want to repeat for another number (Y/N): ");
continueProgram = input.next().charAt(0);
// check if its a valid input.
continueProgram = userContinueProgram(continueProgram);
// if the user enters n/N to quit break the loop.
if (continueProgram == 'n' || continueProgram == 'N') {
break;
}
// else reset the loop.
else {
continue;
}
}
//Check if the number entered is negative and if it is make it positive.
numberEntered = checkInput(numberEntered);
// use the squareInput method to return a list of the added values and the total value.
int[] numberList = squareInput(numberEntered);
// Use the Display results method to display the results in the propper formating.
displayResults(numberList);
// Ask if the user wishes to try again or end the loop.
System.out.print("Do you want to repeat for another number (Y/N): ");
// set loop end condition.
continueProgram = input.next().charAt(0);
// check if its a valid input.
continueProgram = userContinueProgram(continueProgram);
}
// if the loop is broken the use has ended the program so we say goodbye.
System.out.println("Bye...");
}
}