kehmoto1
kehmoto1

Reputation: 51

Can if statements be nested in a while loop?

I've been working with code that uses a do-while loop, and I wanted to add an if else statement into that loop. The do-while loop checks to see what text the user enters and will finish if the word 'exit' is entered.

public static void main(String[] args) {

    String endProgram = "exit";
    String userInput;
    java.util.Scanner input = new java.util.Scanner(System.in);

    do {

        userInput = input.nextLine();
        if ( !userInput.equalsIgnoreCase(endProgram) ) {
            System.out.printf("You entered the following: %s", userInput);
        } else

    } while ( !userInput.equalsIgnoreCase(endProgram) );

}

When I try to compile this code, I get an error from the command prompt saying:

SentinelExample.java:20: error: illegal start of expression
            } while ( !userInput.equalsIgnoreCase(endProgram) );
            ^
SentinelExample.java:22: error: while expected
      }
       ^
SentinelExample.java:24: error: reached end of file while parsing
}
 ^

When I remove the if else statement in the loop, the program compiles fine. Is there something wrong with the syntax in my program? Or is it not possible to put an if else statement in a while loop?

Upvotes: 0

Views: 67900

Answers (5)

usama iftikhar
usama iftikhar

Reputation: 1

package second;

public class Even {

    public static void main(String[] args) {

        int a=0;
        while (a<=100)
        {
            if (a%2==0)
            {
                System.out.println("The no is EVEN : " + a);
            }
            else 
            {
            System.out.println("The num is ODD : " + a);    
            }

        } } }

why this isn't working rightly, it only display the 0, with even !

Upvotes: 0

Abhishek
Abhishek

Reputation: 882

Its quite possible:

public static void main(String[] args) {

    String endProgram = "exit";
    String userInput;
    java.util.Scanner input = new java.util.Scanner(System.in);

    do {

        userInput = input.nextLine();
        if ( !userInput.equalsIgnoreCase(endProgram) ) {
            System.out.printf("You entered the following:"+userInput);// use plus ssign instead of %s
        }

    } while ( !userInput.equalsIgnoreCase(endProgram) );

}

Upvotes: 0

Asaf Nevo
Asaf Nevo

Reputation: 11688

That because of your else statment that does nothing but make the complier think that the while condition is under the else condition and that wrong. Just remove it

Upvotes: 0

rocketboy
rocketboy

Reputation: 9741

You are missing {} to close the else block. Instead of

do{
  if(){
  }else
}while()

use

do{
  if(){
  }else{
  }
}while()

Upvotes: 0

vee
vee

Reputation: 38645

Check your code here you are missing else block:

    userInput = input.nextLine();
    if ( !userInput.equalsIgnoreCase(endProgram) ) {
        System.out.printf("You entered the following: %s", userInput);
    } else 

This is the reason for the compilation error. You can fix this by either removing the else altogether or if you want to add something in it then do the following:

    userInput = input.nextLine();
    if ( !userInput.equalsIgnoreCase(endProgram) ) {
        System.out.printf("You entered the following: %s", userInput);
    } else {
        // Do something
    }

And to answer your questions, yes it's perfectly valid to nest if statements in a while loop.

Upvotes: 2

Related Questions