954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

If statement help!

hi
Got 2 questions.
1.How can i simplify 4 variables all with same condition?
if a,b,c,d <1 System.out.println("Invalid input");?
How to put this in a simple code?I know it can be done individually.

2.if the above condition is false i dont want my code to go further and calculate the input.How and i able to do this.Presently everytime i enter an invalid number ,it still processes the output but also come up with the statement"invalid number".

Thank you

Valiantangel
Junior Poster in Training
62 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

write the code as below:

if(a < 1 || b < 1 || c < 1 || d || 1)
{     
     System.out.print("Invalid Input");
     break;
}
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 
.if the above condition is false i dont want my code to go further and calculate the input.How and i able to do this.Presently everytime i enter an invalid number ,it still processes the output but also come up with the statement"invalid number".

If you want to straight away exit your program you will have to use exit() inside if statement. If you are in a loop and simply want to get out of the loop then you will have to usebreak as mentioned by adil bashir

if(a < 1 || b < 1 || c < 1 || d || 1) { System.out.print("Invalid Input"); break; }

You need to decide whether you want to use '&&' or '||' depending on what condition you wan to check. If you are looking for any one of a,b,c,d to be < 1 then above code using or condition is fine . If your condition is true when all a,b,c,d < 1 , then you will have to use and (&&) instead

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You