473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested if() statements...Grade range...

tiktik
14
Hi...

I am doing this simple Java program which displays a particular grade (A, B, C...) according to the mark entered.

However I cannot arrange it in such a way that it displays "Invalid" if the user eners a grade > 100... take a look...


Expand|Select|Wrap|Line Numbers
  1. System.out.print ("Enter mark: ");
  2.     int mark = Keyboard.readInt();
  3.     char grade =' ';
  4.  
  5.  
  6.     if (mark > 100) (System.out.println ("Invalid Entry"));
  7.       else if (mark >=80 && mark <= 100) grade = 'A';
  8.          else if (mark >= 60) grade = 'B';
  9.               else if (mark >= 55) grade = 'C';
  10.                      else if (mark >= 40) grade = 'D'; 
  11.                             else if (mark < 40) grade = 'F';
  12.  
  13.  
  14.         System.out.println ("Grade = " + grade);
after compiled it keeps telling me that this is not a statement:

if (mark > 100) (System.out.println ("Invalid Entry"));



can't figure why ..


The program however runs, and if the user enters "101" for exmple, it keeps obeying the second 'else if'....

Any ideas pls....?

thanks
Nov 26 '08 #1
9 8304
Nepomuk
3,112 Expert 2GB
@tiktik
It's right, that isn't a statement. Try
Expand|Select|Wrap|Line Numbers
  1. if (mark > 100) System.out.println ("Invalid Entry");
or
Expand|Select|Wrap|Line Numbers
  1. if (mark > 100) {System.out.println ("Invalid Entry");}
instead.

Greetings,
Nepomuk
Nov 26 '08 #2
tiktik
14
Yes that's it thanks a lot...

but now i have another problem...

i would like that if the entry is invalid, it would not display the last line ("Grade =" ), and so i tried to insert the 'break;' but it seems as if it can be used only in loops or switch :(



Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.  
  3.     System.out.print ("Enter mark: ");
  4.     int mark = Keyboard.readInt();
  5.     char grade =' ';
  6.  
  7.  
  8.     if (mark > 100) System.out.println ("Invalid Entry"); 
  9.         break;
  10.        if (mark >=80 && mark <= 100) grade = 'A';
  11.          else if (mark >= 60) grade = 'B';
  12.               else if (mark >= 55) grade = 'C';
  13.                      else if (mark >= 40) grade = 'D'; 
  14.                             else if (mark < 40) grade = 'F';
  15.  
  16.  
  17.         System.out.println ("Grade = " + grade);

when I run this program and input an invalid entry, it still displays "Grade = " at the end...
Nov 27 '08 #3
tiktik
14
Never mind.... i solved the problem by inserting "System.out.println" after every if statement like this

Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.  
  3.     System.out.print ("Enter mark: ");
  4.     int mark = Keyboard.readInt();
  5.     char grade =' ';
  6.  
  7.  
  8.     if (mark > 100) System.out.println ("Invalid Entry"); 
  9.        else if (mark >=80 && mark <= 100) {grade = 'A';
  10.                                            System.out.println ("Grade = " + grade); }
  11.  
  12.           else if (mark >= 60) {grade = 'B';
  13.                                 System.out.println ("Grade = " + grade); }
  14.  
  15.               else if (mark >= 55) {grade = 'C';
  16.                                        System.out.println ("Grade = " + grade); }
  17.  
  18.                      else if (mark >= 40) {grade = 'D'; 
  19.                                               System.out.println ("Grade = " + grade); }
  20.  
  21.                             else if (mark < 40) {grade = 'F';
  22.                                                  System.out.println ("Grade = " + grade);}

Now the program works as it is supposed... but still I have the question question whether or not the 'break;' could be used elsewhere (not just in loops or switch() )?... It would be very useful I think - especially when one needs to terminate the program if a certain value is entered - one needs something which would get him out of the main() block...

Thanks
Nov 27 '08 #4
JosAH
11,448 Expert 8TB
This is just programming: if a valid numbers was entered you want to do something, otherwise you don't want to do anything, so:

Expand|Select|Wrap|Line Numbers
  1. if (valid number entered) {
  2.    do something;
  3. }
  4.  
Let's concentrate on the 'valid number entered'; the boolean expression
'number <= 100 && number >= 0' seems to express what you want. Simply plug it into your pseudo code:

Expand|Select|Wrap|Line Numbers
  1. if (number <= 100 && number >= 0) {
  2.    do something;
  3. }
  4.  
You already have solved the 'do something' part so plug it into the pseudo code.

kind regards,

Jos
Nov 27 '08 #5
tiktik
14
Yes, thanks for your reply...it works very well with it as well

Any idea about the break; question?
Nov 27 '08 #6
JosAH
11,448 Expert 8TB
@tiktik
As you have already noticed a 'break' statement can only occur in a loop or in a case statement.

kind regards,

Jos
Nov 27 '08 #7
Nepomuk
3,112 Expert 2GB
@tiktik
There's always System.exit(int Status) if you really want to leave your program, but that should be used with caution.

Other than that, you can always check if your grade variable has been assigned yet - setting it to a default value (e.g. 'X') would be one way of doing that. Or you can have a boolean value gradeWasSet. Or you can put all of those things in one nested if. Or I'm sure you can think of something else.

There is something similar to what you mean in many languages: the jump command goto. This command doesn't exist in Java (although there is continue, which is similar in some aspects), as it can make code very ugly very easily. That's also why it's seen as bad style by a great part of the programming world.

Here, I'll even give you a comic about goto:


(from http://xkcd.com/292/)

Greetings,
Nepomuk
Nov 27 '08 #8
tiktik
14
Thanks for the detailed explanation.
good comic as well =)

tiktik
Nov 28 '08 #9
Nepomuk
3,112 Expert 2GB
You're welcome of course. :-)

Greetings,
Nepomuk
Nov 28 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Don Low | last post by:
Hi, I'm studying the python tutorial at python.org. In introducing the concept of *break* and *continue* Statements, and *else* Clauses on Loops, there's an example that goes as follows: ...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
2
by: SplaTTer | last post by:
I have a piece of code consisting of k nested for loops, for example: int k = 5; int *p = new int; double x,y; //x is read from file for(p=0;p<=6;p++) for(p=0;p<=3;p++) for(p=0;p<=3;p++)
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
3
by: Jake Emerson | last post by:
I'm attempting to build a process that helps me to evaluate the performance of weather stations. The script below operates on an MS Access database, brings back some data, and then loops through to...
5
by: Jyotirmoy Bhattacharya | last post by:
I'm a newcomer to Python. I have just discovered nested list comprehensions and I need help to understand how the if-clause interacts with the multiple for-clauses. I have this small program: ...
16
by: koutoo | last post by:
I start my code with some constants then a while statement. But I have some For statements towards the end within the While statement where I start getting some errors. I'm hoping I won't have to...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.