ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello! I've just started programming at my university and I'm finding it a bit hard to get started. I've been given this for homework.
Quote:
Given 2 integers, a and b, print their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just print 20
"
The problem I'm having is that i don't know how to tell java to print 20 when the value is in that range. I'm guessing it's an if else statement? Any help would be greatly appreciated. Thanks!
Yes, just use an if and an else . There really isn't much more to it. If a condition is true, you do one thing and if it isn't, you do something else. You're even given the condition to test.
Edit: You do know how to form the test, right? I mean, do you know the operators you need to use in the test?
You need to use == to test equality. Think about the range in two parts: n1 + n2 is greater than 10 and less than 19. Does that help now? Also, in Java, you don't use "then". The syntax is
I'm not sure which book you're using (if any), but you might want to look at this. For this problem, you'll want to look at operators and control flow.
But on a serious note, look up the different comparisons you can use, specifically less than (<), greater than (>), not equals (!=) etc etc... As a hint, this is how a java comparison of variables a and b would look:
Code:
if (a>b)
System.out.println("a is greater than b");
else
System.out.println("a is not greater than b");
If you have more than one statement in the if block, youwill need to surround with parentheses:
I wouldn't just post code like this without explaining how it works, as it will probably confuse a beginner.
FWIW, phoenixfire, this operator is called the ternary operator (at least in C; I assume it's the same here). It's a shorthand for if-else and the general form is
condition ? value if true : value if false
You test the condition and if it's true, the value before the colon is returned. If false, the value following the colon is returned.
Thanks Nylex, i'm understanding if else a lot better now. I didn't realize you had to put the statements in {}'s. It can compile now, yay! I'll have a look through those Java tutorials. Looks really helpful.
Devnull10, could you explain that line?
Also, there must be way to enter a range instead of putting each number in individually?
I apologies for the basic questions and thank you for your patients.
Thanks Nylex, i'm understanding if else a lot better now. I didn't realize you had to put the statements in {}'s. It can compile now, yay! I'll have a look through those Java tutorials. Looks really helpful.
You have to use curly braces if you have multiple statements. If it's just a single statement, you can leave them out.
Quote:
Devnull10, could you explain that line?
Have a look at my last post. I explained the construct there.
Quote:
I apologies for the basic questions and thank you for your patients.
Again, you need to break the condition into two parts: n1 + n2 is greater than 10 and n1 + n2 is less than 19. So, you need two tests in your if statement. Look at this.
I wouldn't just post code like this without explaining how it works, as it will probably confuse a beginner.
It was a tongue in cheek reply which is why I explained fully the "other" way below.
You don't have to put statements in braces unless you have more than one statement in the if block, however I tend to do so anyway out of habit.
Quote:
Also, there must be way to enter a range instead of putting each number in individually?
Well, in this case you don't need to put a range in as all you are doing is checking whether the value is GREATER THAN 20.
So: "if (a>20)..." works fine.
If you wanted to check say a variable is in the range 10 - 20 inclusive then just use the AND operator (&&):
Code:
if (a>=10 && a<=20) {
System.out.println("a is between 10 and 20 inclusive.");
}
Well, when you say "a range of numbers" then strictly speaking, no, in the sense that you can't say:
Code:
if (a in (1 to 10)) ...
as you can in some languages. But... a range has an upper and lower bound, so you just check those. Ie, if you want to be in the range 1 to 10, you just ensure the number is not less than 1 and not greater than 10. So technically that is a range, you are just typing it differently!
Code:
if (a>=1&&a<=10)
/*Number is in the range 1 - 10*/
The above is checking a number is in the range 1-10, you can of course do the opposite using an "OR" (||):
Code:
if (a<1||a>10)
/*number is outside the range 1 - 10*/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.