Sorry I know this might sound extremely basic but i've been having a bit of trouble. Basically I need to add an if statement so that when the user enters a number it needs to be between 0-90. Is there any way I can do this straight after the user has input the number or will it have to be done once i've assigned it to a variable?
assign the user number to a variable and then
[cpp]if( userNumber < 90 && userNumber > 0)
{
// do stuff
}[/cpp]
I thnk you could also do:
[cpp]if( 0 < userNumber < 90)
{
// do stuff
}[/cpp]
int x = 5;
if (x == 5){
System.out.println("The value of X is 5");
}else{
System.out.println("The value of x isn't 5");
Sorry if there are any serious mistakes, I typed this on a phone. So anyway, in the parameters of if you type something you want to check is true. In this case it checks if x is equal to 5 (yes, you do need to put ==, = will not work). If it's not true, then it executes the else block.
Ok so I have this
[CODE]if( number > 90 && number < 0)
{
System.out.print("Please enter a number within the range of 0-90");
}
else
{
Code to execute if the number is between 0 and 90
}[/CODE]
However it doesn't seem to be working and just executes the code after the else even if the number is greater than 90 or less than 0. Have I got the syntax wrong?
LOL
your basically saying, if number is greater than 90 and smaller than 0...
swap the < >
Er what? I want the number to be in the range of 0-90 so why would I want to switch them?
Also I did and it prints both the line before the else and the code after the else.
No, he has to replace && with ||.
number > 90 && number < 0 that means if(number bigger than 90 AND number smaller than 0)
A number cant be both over 90 and under 0 therefore it goes straight to the ELSE statement
number < 90 && number > 0 that means if(number smaller than 90 AND number bigger than 0)
this will make the code run if a number is between 0 and 90
[QUOTE=Richy19;25940218]LOL
your basically saying, if number is greater than 90 and smaller than 0...
swap the < >[/QUOTE]
No.
[QUOTE=Gibo990;25940106]Have I got the syntax wrong?[/QUOTE]
Not really if I get what you're trying to achieve.
I believe you want to make the user enter another number.
You would need to use an infinite loop of some kind, then, which "breaks" when the user enters a valid number.
Relatively easy to understand code for this situation could look like this:
[code]
// Repeat the following block of code forever until the user forcefully
// quits the program (via Control-C, for instance) or until "break" gets used.
int number;
while(true) {
// Your code for getting a number from the user would be here in some form.
// I'll just use "get_number_from_user()" as a placeholder.
number = get_number_from_user();
// Check if the number's between 0 and 90.
if(number > 90 || number < 0) {
/* If it isn't, then output an error message. Because there is no "break" statement
* that would get run anywhere after this in the infinite loop (the "else" block
* will not run), the end of the block of code is reached and the block of code runs
* yet again.
*/
System.out.print("Please enter a number within the range of 0 to 90.");
} else {
// But if the number is indeed in the range of 0 to 90, use "break" to get out of this infinite loop.
break;
}
}
System.out.print("Yay, your number is within the range of 0 to 90!");
[/code]
Heh.
[QUOTE=_nonSENSE;25940297]No, he has to replace && with ||.[/QUOTE]
Thank you for this the if statement is working now.
[editline]8th November 2010[/editline]
[QUOTE=Alternative Account;25940451]No.
Not really if I get what you're trying to achieve.
I believe you want to make the user enter another number.
You would need to use an infinite loop of some kind, then, which "breaks" when the user enters a valid number.
Relatively easy to understand code for this situation could look like this:
[code]
// Repeat the following block of code forever until the user forcefully
// quits the program (via Control-C, for instance) or until "break" gets used.
int number;
while(true) {
// Your code for getting a number from the user would be here in some form.
// I'll just use "get_number_from_user()" as a placeholder.
number = get_number_from_user();
// Check if the number's between 0 and 90.
if(number > 90 && number < 0) {
/* If it isn't, then output an error message. Because there is no "break" statement
* that would get run anywhere after this in the infinite loop (the "else" block
* will not run), the end of the block of code is reached and the block of code runs
* yet again.
*/
System.out.print("Please enter a number within the range of 0 to 90.");
} else {
// But if the number is indeed in the range of 0 to 90, use "break" to get out of this infinite loop.
break;
}
}
System.out.print("Yay, your number is within the range of 0 to 90!");
[/code]I see that Siemens is reading the thread right now, so I'd like his ideas as well, heh.[/QUOTE]
Thanks thats exactly what I was looking for, cheers man.
[QUOTE=Richy19;25940344]number > 90 && number < 0 that means if(number bigger than 90 AND number smaller than 0)
A number cant be both over 90 and under 0 therefore it goes straight to the ELSE statement
number < 90 && number > 0 that means if(number smaller than 90 AND number bigger than 0)
this will make the code run if a number is between 0 and 90[/QUOTE]
If you'd just replace the condition in the code he has posted, it'd run what's supposed to be ran if the number is not within the range of 0 to 90 if the number is actually within that range.
What you're basically trying to say would be equivalent to doing this:
[code]
if(everything_is_fine_and_dandy) {
abandon_ship("errors everywhere");
} else {
sing_in_joy();
}
[/code]
What i was trying to do is:
[code]
if(number is in range)
//do stuff with the number
[/code]
i did forget to mention the process of getting a new number if it isnt in that range.
Anyway it would be easier to just use a Do While.
[code]Do
{
// get number from the user
}while(number isnt between 0 and 90);[/code]
[QUOTE=Richy19;25940624]What i was trying to do is:
[code]
if(number is in range)
//do stuff with the number
[/code]
[/quote]
You could do it like that, too, but Gibo990 wanted the error condition to be run before the non-error condition. Aside from aesthetics it doesn't make much of a difference.
[quote=From the same person]
i did forget to mention the process of getting a new number if it isnt in that range.
Anyway it would be easier to just use a Do While.
[code]Do
{
// get number from the user
}while(number isnt between 0 and 90);[/code][/QUOTE]
To output an error message you would have to repeat the loop condition in the if-then construct. That could result in some silly bugs when you need to change the range limits, as you could forget to change one of the limits in either of those conditions.
Holy shit so much drama for a fucking IF statement? Programming is going down the shitters.
Lol I failed ... -snip-
[QUOTE=Alternative Account;25940451]
infinite loop code[/QUOTE]
I usually try not to use infinite loops and break out of them; you could just use this:
[code]
int number;
do {
System.out.print("Please enter a number within the range of 0 to 90.");
// Your code for getting a number from the user would be here in some form.
// I'll just use "get_number_from_user()" as a placeholder.
number = get_number_from_user();
} while(number > 90 || number < 0);
System.out.print("Yay, your number is within the range of 0 to 90!");
[/code]
[editline]9th November 2010[/editline]
Wow I am late, I should read to the end of the thread next time...
[QUOTE=Alternative Account;25940451]
[code]
// Repeat the following block of code forever until the user forcefully
// quits the program (via Control-C, for instance) or until "break" gets used.
int number;
while(true) {
// Your code for getting a number from the user would be here in some form.
// I'll just use "get_number_from_user()" as a placeholder.
number = get_number_from_user();
// Check if the number's between 0 and 90.
if(number > 90 || number < 0) {
/* If it isn't, then output an error message. Because there is no "break" statement
* that would get run anywhere after this in the infinite loop (the "else" block
* will not run), the end of the block of code is reached and the block of code runs
* yet again.
*/
System.out.print("Please enter a number within the range of 0 to 90.");
} else {
// But if the number is indeed in the range of 0 to 90, use "break" to get out of this infinite loop.
break;
}
}
System.out.print("Yay, your number is within the range of 0 to 90!");
[/code]
[/QUOTE]
My current and past programming teachers would've killed me if I used a loop that way.
The do-while loop posted before is the 'correct' way to go.
(Of course, infinite loop with a break works. But it's horribly made.)
[QUOTE=_nonSENSE;25955528]Try to avoid infinite loops.[/QUOTE]
[QUOTE=Fear_Fox;25964255]while(true) is bad for you![/QUOTE]
I agree.
When I wrote that, I actually thought of displaying some kind of error message. [i]("Bad user, don't do that! Bad, bad user! No sweets for you today!")[/i]
A do-while loop would be much better in this case.
An if loop
[code]if(blah)
{
BLOCK O' STATEMENTS
}
elseif(blah)
{
STATEMENT BLOCK
}
else(blah)
{Statement block
}[/code]
[QUOTE=Werem00se;26108561]An if loop
[code]if(blah)
{
BLOCK O' STATEMENTS
}
elseif(blah)
{
STATEMENT BLOCK
}
else(blah)
{Statement block
}[/code][/QUOTE]
That's not a loop and else statements have no parameters, they're the final case, the one that is run when nothing else is true.
[QUOTE=Werem00se;26108561]An if loop
[code]if(blah)
{
BLOCK O' STATEMENTS
}
elseif(blah)
{
STATEMENT BLOCK
}
else(blah)
{Statement block
}[/code][/QUOTE]
Oh wow.
[QUOTE=BlkDucky;26118365]Oh wow.[/QUOTE]
What? Is that wrong?? Oh shit, I should've looked at it before I posted.
Sorry, you need to Log In to post a reply to this thread.