Why is my programming not looping correctly? (Java)

how to make a java program repeat itself
for loop
user input to repeat program in java
while loop java
for loop java
what is a loop in programming
do-while loop java
java programming exercises with solutions pdf

Desired output: Hello, I am trying to make a program that does addition, subtraction, multiplication, and division and loops until you press 'e' to exit the loop.

So, I want it to look something like this:

A. Addition
B. Subtraction
C. Multiplication
D. Division
E. Exit

Please enter your selection, enter E to end: 
//let's say they enter a, and want to add 5 plus 5

Enter your first number: 
Enter your second number:

5 + 5 = 10.0

Please enter your selection, enter E to end:
//This looping part is what I want to happen! But my program just ends, and I'm not 
sure how to fix it

Issue: My problem is that once you enter what operation you want, it only does the arithmetic once and then it displays my exit message and ends, but that is only supposed to happen when the user enters 'e' but the rest works, I think!

Here is my code

import java.util.Scanner;

public class Calculator_Loop {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    char selection;
    char choice = 'E';
    double num_1;
    double num_2;
    double result;


    System.out.println("A. Addition\nB. Subtraction\nC. Multiplication\nD. Division \n\nE. Exit");

    //System.out.println("\nPlease enter your selection, enter E to end:");
    //selection = input.next().charAt(0);
    //choice = Character.toUpperCase(selection);

    while(choice != 'E')
    System.out.println("\nPlease enter your selection, enter E to end:");
    selection = input.next().charAt(0);
    choice = Character.toUpperCase(selection);

    if(choice == 'A') {
        System.out.println("Enter your first number:");
        num_1 = input.nextDouble();
        System.out.println("Enter your second number:");
        num_2 = input.nextDouble();

        result = num_1 + num_2;
        System.out.println(result);

    }
    if(choice == 'B'){
        System.out.println("Enter your first number:");
        num_1 = input.nextDouble();
        System.out.println("Enter your second number:");
        num_2 = input.nextDouble();

        result = num_1 - num_2;
        System.out.println(result);

    }
    if(choice == 'C'){
        System.out.println("Enter your first number:");
        num_1 = input.nextDouble();
        System.out.println("Enter your second number:");
        num_2 = input.nextDouble();

        result = num_1 * num_2;
        System.out.println(result);
    }
    if(choice == 'D'){
        System.out.println("Enter your first number:");
        num_1 = input.nextDouble();
        System.out.println("Enter your second number:");
        num_2 = input.nextDouble();

        result = num_1 / num_2;
        System.out.println(result);
    }

    System.out.println("Thank you, have a nice day!");
    }
}

Am I doing something wrong with the brackets? Or is there something I'm missing for the loop to work? Any help is appreciated, thanks!


Welcome to SO. Problem is (as far as I can see) that you have nothing/only the print line in your while loop.

while(true) {
    do things
}

Java allows while loops without brackets, but at maximum one line (and I recommend doing them for one line too, but that is debattable), as soon as you need several lines, you need brackets and should have according indentation.

4 Reasons Not to Use Programming Loops (and a , So before you start coding up that next do/while loop, consider the potential code, by slightly changing the parameters to get the correct answer, while not fully The Stream API first introduced in Java 8 offers this capability. In C++ assignment does not work this way: it's a one shot deal. Once you assign a value to a variable, it's that value until you reassign the values. In the example program, because a and b are not initialized, sum will equal an unknown random number, no matter what the user inputs.


You need to surround your intended while loop with brackets to make it work the way you want:

while (choice != 'E') {
    System.out.println("\nPlease enter your selection, enter E to end:");
    selection = input.next().charAt(0);
    choice = Character.toUpperCase(selection);

    switch (choice) {
        case 'A':
            System.out.println("Enter your first number:");
            num_1 = input.nextDouble();
            System.out.println("Enter your second number:");
            num_2 = input.nextDouble();

            result = num_1 + num_2;
            System.out.println(result);
            break;

        case 'B':
            // ...
    }
}

System.out.println("Thank you, have a nice day!");

Currently, your while loop will spin forever (I think), because the seed value for choice is being set to E.

Side note: Consider using a switch statement to handle the various case values for the choice.

Typical errors with 'While' Loops, If we create a variable called loops and set it to 0 outside the loop (on line 2), then do So it depends whether you want to be able to access and modify the variable anywhere else in the program. please correct my logic if im wrong :) HTML & CSSPythonJavaScriptJavaSQLBash/ShellRubyC++RC#PHPGoSwift Kotlin. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages − Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.


Instead of using the if conditions, please use the while loop and switch condition. if the input value is other than the A, B, C or D then exit from the code.

Code takes only the first character of your input and enters into switch case, where it will compare the input with the required conditions.

public class Calculator_Loop {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String choice = "";
        double num_1;
        double num_2;
        double result;

        System.out.println("A. Addition\nB. Subtraction\nC. Multiplication\nD. Division \n\nE. Exit");

        /*System.out.println("\nPlease enter your selection, enter E to end:");
        selection = input.next().charAt(0);
        choice = input.next().substring(0, 1).toUpperCase();*/

        outer: while (true) {
            System.out.println("\nPlease enter your selection, enter E to end:");
            choice = input.next();
            choice = choice.substring(0, 1).toUpperCase();
            switch (choice) {
            case "A":
                System.out.println("Enter your first number:");
                num_1 = input.nextDouble();
                System.out.println("Enter your second number:");
                num_2 = input.nextDouble();

                result = num_1 + num_2;
                System.out.println(result);
                break;

            case "B":
                System.out.println("Enter your first number:");
                num_1 = input.nextDouble();
                System.out.println("Enter your second number:");
                num_2 = input.nextDouble();

                result = num_1 - num_2;
                System.out.println(result);

                break;

            case "C":
                System.out.println("Enter your first number:");
                num_1 = input.nextDouble();
                System.out.println("Enter your second number:");
                num_2 = input.nextDouble();

                result = num_1 * num_2;
                System.out.println(result);

                break;

            case "D":
                System.out.println("Enter your first number:");
                num_1 = input.nextDouble();
                System.out.println("Enter your second number:");
                num_2 = input.nextDouble();

                result = num_1 / num_2;
                System.out.println(result);

                break;
            default:
                System.out.println("Thank you, have a nice day!");
                break outer;
            }
        }
    }
}

Java Basics Exercises - Java Programming Tutorial, Modify the program to use a "while-do" loop instead of "for" loop. Take note that computer programs may not produce the correct result even though the code� Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.


Use the right loop to repeat tasks, For loops with an integer value. Here is an example of a for loop that repeats a statement five times: for (int� Looping Statement in Java. Looping statement are the statements execute one or more statement repeatedly several number of times. In java programming language there are three types of loops; while, for and do-while. Why use loop ? When you need to execute a block of code several number of times then you need to use looping concept in Java language.


Javanotes 8.1, Section 3.1 -- Blocks, Loops, and Branches, In Java, there are just six such structures that are used to determine the normal flow of control in a program—and, in� There are three kinds of loop statements in Java, each with their own benefits – the while loop, the do-while loop, and the for loop. Loop mechanisms are useful for repeatedly executing blocks of code while a boolean condition remains true, a process that has a vast amount of applications for all types of software programming.


Loop statements in Java, In a big program one may need to repeat things hundreds of times or, most often get known how many times System.out.println("Let's compact the code using for loop."); Termination (i < 10): loop repeats, while this expression is correct. In Java, a while loop is used to execute statement(s) until a condition is true. In this tutorial, we learn to use it with examples. First of all, let's discuss its syntax: while (condition(s)) {// Body of loop} 1. If the condition(s) holds, then the body of the loop is executed after the execution of the loop body condition is tested again.


CodingBat Java Arrays and Loops, This page introduces arrays and loops in Java with example code, int[] values = new int[100]; // Loop over all the elements in the values array for Oftentimes in loop and array problems, getting these "edge" cases correct is the trickiest part. in the array, the program will get an ArrayOutOfBounds exception on that line. A while loop is a control flow statement that allows us to run a piece of code multiple times. Like loops in general, a while loop can be used to repeat an action as long as a condition is met