If else statement wont work for do while loop

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2184 Views - Last Post: 09 June 2013 - 09:58 PM Rate Topic: -----

#1 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

If else statement wont work for do while loop

Posted 09 June 2013 - 07:05 PM

//I am getting an error on line 29, stating "else without if"

//this program will ask you enter a number, it its too high or low, re-enter it

import java.util.Scanner;

class doWhile2{
	
	public static void main(String[] args){
		
		Scanner input = new Scanner(System.in);
		
		int i = (int)(Math.random() * 100);
		int number;
		
		// create do while 
		do{
			System.out.println("Enter an integer: ");
			number = input.nextInt();
			
		}
		while(true);
			
			if (i == number){
			System.out.println("The number you entered matches our number " + i);
			}
			break;
			
			else if (i > number){
			System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");
			}
			
			else {
			System.out.println("The number you entered is too high, the differnce is " (number - i)" try again: ");
			}
		
		
	}
}






Is This A Good Question/Topic? 0
  • +

Replies To: If else statement wont work for do while loop

#2 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:07 PM

Remove the break or put it inside the if statement.

This post has been edited by schutzzz: 09 June 2013 - 07:08 PM

Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,916
  • Joined: 19-March 11

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:08 PM

The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.
Was This Post Helpful? 0
  • +
  • -

#4 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:35 PM

View Postjon.kiparsky, on 09 June 2013 - 07:08 PM, said:

The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.

The reason why I put the break is so that if the user input a matching number, then the program should end. Am I thinking correctly? Also, I just removed the break, but now I am getting an error stating: "unexpected type" for the minus symbol in both my ELSE IF and ELSE statement. :(
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,916
  • Joined: 19-March 11

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:37 PM

View Postjon.kiparsky, on 09 June 2013 - 09:08 PM, said:

If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.

Was This Post Helpful? 0
  • +
  • -

#6 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:39 PM

View Postschutzzz, on 09 June 2013 - 07:07 PM, said:

Remove the break or put it inside the if statement.

But isnt the break so if my IF statement is true, then it will ignore the following statements? I've taken off the break and I added a + to my ELSE statement, but now I am getting the error: "unexpected type" for both my ELSE IF and ELSE statement, which is pointing to the subtraction sign...so now I have the following code:

while(true);
			
			if (i == number){
			System.out.println("The number you entered matches our number " + i);
			}
			
			
			else if (i > number){
			System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: ");
			}
			
			else {
			System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: ");
			}



Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,916
  • Joined: 19-March 11

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:40 PM

View Poststreek405, on 09 June 2013 - 09:35 PM, said:

Also, I just removed the break, but now I am getting an error stating: "unexpected type" for the minus symbol in both my ELSE IF and ELSE statement. :(/>

System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");


You're missing a plus sign.

 System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");

Was This Post Helpful? 1
  • +
  • -

#8 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:45 PM

View Postjon.kiparsky, on 09 June 2013 - 07:37 PM, said:

View Postjon.kiparsky, on 09 June 2013 - 09:08 PM, said:

If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.

Oh ok. I think I understand it now. But when I put the break inside the curly braces, I get an error stating "break outside switch or loop". So now I just omitted the break entirely.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,916
  • Joined: 19-March 11

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:46 PM

View Poststreek405, on 09 June 2013 - 09:39 PM, said:

But isnt the break so if my IF statement is true, then it will ignore the following statements?



The syntax of the if statement is like this:

if (CONDITION)
STATEMENT BLOCK
else STATEMENT BLOCK

a STATEMENT BLOCK is eitehr a single statement:
if (true) System.out.println("It's twue! It's twue!");


which can be an empty statement:
if (true) ;



or some number of statements in curly braces:

if (true) {
  System.out.println("It's twue! It's twue!");
  truthIsTrue = true;
  int one = 1;
  // etc.
}



The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.

Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.
Was This Post Helpful? 1
  • +
  • -

#10 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:49 PM

View Postjon.kiparsky, on 09 June 2013 - 07:40 PM, said:

View Poststreek405, on 09 June 2013 - 09:35 PM, said:

Also, I just removed the break, but now I am getting an error stating: "unexpected type" for the minus symbol in both my ELSE IF and ELSE statement. :(/>/>

System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");


You're missing a plus sign.

 System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");

Thank you, I too noticed that, but now I am getting the error "unexpected type" for both the ELSE IF and ELSE statement, pointing at the minus sign

View Postjon.kiparsky, on 09 June 2013 - 07:46 PM, said:

View Poststreek405, on 09 June 2013 - 09:39 PM, said:

But isnt the break so if my IF statement is true, then it will ignore the following statements?



The syntax of the if statement is like this:

if (CONDITION)
STATEMENT BLOCK
else STATEMENT BLOCK

a STATEMENT BLOCK is eitehr a single statement:
if (true) System.out.println("It's twue! It's twue!");


which can be an empty statement:
if (true) ;



or some number of statements in curly braces:

if (true) {
  System.out.println("It's twue! It's twue!");
  truthIsTrue = true;
  int one = 1;
  // etc.
}



The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.

Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.

Ok thank you, that actually makes sense!
Was This Post Helpful? 0
  • +
  • -

#11 Bountyhunter1234   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 12-May 09

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:50 PM

you start your while statement and immediately end it when you do this

while(true);



it should be

while(true){


Was This Post Helpful? 1
  • +
  • -

#12 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:52 PM

View PostBountyhunter1234, on 09 June 2013 - 07:50 PM, said:

you start your while statement and immediately end it when you do this

while(true);



it should be

while(true){


I originally made it like that, but then I got the error " ; expected", point that the curly brace
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,916
  • Joined: 19-March 11

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:54 PM

If you fix that missing plus sign, that will go away.

Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.

See the following sample code. Try compiling it in a throwaway file:

public class Foo
{
   public static void main(String[] args)
   {
      int i = 0;
      int number = 10;
      System.out.println("The number you entered is too low, the difference is"
            + (i - number) +" try again: ");

      while (true) {
         if (true)  { System.out.println ("true"); break;}
      }
   }
}


This post has been edited by jon.kiparsky: 09 June 2013 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#14 Bountyhunter1234   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 76
  • Joined: 12-May 09

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 07:57 PM

my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.
Was This Post Helpful? 0
  • +
  • -

#15 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: If else statement wont work for do while loop

Posted 09 June 2013 - 08:03 PM

View Postjon.kiparsky, on 09 June 2013 - 07:54 PM, said:

If you fix that missing plus sign, that will go away.

Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.

See the following sample code. Try compiling it in a throwaway file:

public class Foo
{
   public static void main(String[] args)
   {
      int i = 0;
      int number = 10;
      System.out.println("The number you entered is too low, the difference is"
            + (i - number) +" try again: ");

      while (true) {
         if (true)  { System.out.println ("true"); break;}
      }
   }
}



View PostBountyhunter1234, on 09 June 2013 - 09:50 PM, said:

you start your while statement and immediately end it when you do this

while(true);



it should be

while(true){



I've tried that but I am still getting the same error, for the break. This the the code
while(true);
			
			if (true){
			System.out.println("The number you entered matches our number " + i);
			break;
			}
			
			
			else if (i > number){
			System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: ");
			}
			
			else {
			System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: ");
			}
		



Good catch! That is in fact the problem.


LOL That was my very first attempt that this code, but it stated that I need an ";" in place of the curly brace for the WHILE. Also, in my book, for the do while example, there is an ";" not a curly brace for the while part, although that does not make any sense to me...

View PostBountyhunter1234, on 09 June 2013 - 07:57 PM, said:

my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.

It's all good, man.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2