HWZ Forums

Login Register FAQ Mark Forums Read

C program experts lip lai!

Reply
 
LinkBack Thread Tools
Old 07-07-2017, 12:54 AM   #1
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
C program experts lip lai!

How can I put 2 conditions in an if-else statement?

E.g.
if(a >= 20 && <= 40) is this correct?

Sent from 1000char using GAGT
rollanddie is offline   Reply With Quote
Old 07-07-2017, 01:30 AM   #2
Senior Member
 
kuma-mon's Avatar
 
Join Date: Apr 2017
Posts: 828
How can I put 2 conditions in an if-else statement?

E.g.
if(a >= 20 && <= 40) is this correct?

Sent from 1000char using GAGT
hi,

your two conditions are correct but you have to individualize each condition, even though both belong to the same "a".

correct ans:

//if a is between 20 to 40
if(a >= 20 && a <= 40){
//do something here
}
kuma-mon is offline   Reply With Quote
Old 07-07-2017, 08:05 AM   #3
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
hi,

your two conditions are correct but you have to individualize each condition, even though both belong to the same "a".

correct ans:

//if a is between 20 to 40
if(a >= 20 && a <= 40){
//do something here
}
Thanks kuma.

How about 4 conditions all belonging to A?

if(a >= 20 && a<=40 && a>=60 && a <= 80)

Sent from 1000char using GAGT
rollanddie is offline   Reply With Quote
Old 07-07-2017, 01:06 PM   #4
Senior Member
 
kuma-mon's Avatar
 
Join Date: Apr 2017
Posts: 828
Thanks kuma.

How about 4 conditions all belonging to A?

if(a >= 20 && a<=40 && a>=60 && a <= 80)

Sent from 1000char using GAGT
hi,

yes it would be like that

edit: forget its &&, so it wont work

Last edited by kuma-mon; 08-07-2017 at 11:37 PM..
kuma-mon is offline   Reply With Quote
Old 07-07-2017, 03:40 PM   #5
Moderator
 
tangent314's Avatar
 
Join Date: Jul 2002
Posts: 3,035
How about 4 conditions all belonging to A?

if(a >= 20 && a<=40 && a>=60 && a <= 80)
^ this will always return false...
tangent314 is offline   Reply With Quote
Old 07-07-2017, 04:42 PM   #6
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
^ this will always return false...
What's the correct way to put it?

Sent from 1000char using GAGT
rollanddie is offline   Reply With Quote
Old 07-07-2017, 07:17 PM   #7
Master Member
 
Join Date: Apr 2003
Posts: 3,931
What's the correct way to put it?

Sent from 1000char using GAGT
Because there is no number that can exist between 20 and 40, and 60 and 80 at the same time. Can only have number that is between 20 and 40 or 60 and 80.
peterchan75 is offline   Reply With Quote
Old 07-07-2017, 08:55 PM   #8
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
So will it be
1) if(a >= 20 && a <= 40 || a >= 60 && a <= 80)

OR

2) if((a >= 20 && a <= 40) || (a >= 60 && a <= 80))

Sent from 1000char using GAGT
rollanddie is offline   Reply With Quote
Old 07-07-2017, 09:05 PM   #9
Great Supremacy Member
 
BlackCube's Avatar
 
Join Date: Jul 2003
Posts: 64,779
2nd might be better, but IIRC both works.
__________________
oyb'in hy gnki py ln. r krep dyeek mrn keg r krep dyeek und r enng oyb lyin pske r psybdsp nznio gko
BlackCube is offline   Reply With Quote
Old 07-07-2017, 09:24 PM   #10
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 8,588
So will it be
1) if(a >= 20 && a <= 40 || a >= 60 && a <= 80)

OR

2) if((a >= 20 && a <= 40) || (a >= 60 && a <= 80))

Sent from 1000char using GAGT
Both are the same, go understand the operators precedence in C.
davidktw is offline   Reply With Quote
Old 07-07-2017, 09:51 PM   #11
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
Alright, thanks for all the input guys!

Appreciate it

Sent from 1000char using GAGT
rollanddie is offline   Reply With Quote
Old 08-07-2017, 11:34 PM   #12
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
i'm facing a problem.

if(time >= 600 && time <= 900 || time >= 1800 && <= 2100)
{
}
else
{
}

but it is always returning as false. any idea why?
__________________
This space intentionally left blank.
rollanddie is offline   Reply With Quote
Old 08-07-2017, 11:42 PM   #13
Senior Member
 
kuma-mon's Avatar
 
Join Date: Apr 2017
Posts: 828
i'm facing a problem.

if(time >= 600 && time <= 900 || time >= 1800 && <= 2100)
{
}
else
{
}

but it is always returning as false. any idea why?
hi

im assuming you are comparing the time, as in the system time value? it wont work because it is not even an integer. it is a set of values, eg: DDMMYYYYHHMMSS

system.out.println your time value first and check if its the value you want. you have to get the seconds/milliseconds of the time and convert it to integer.
kuma-mon is offline   Reply With Quote
Old 08-07-2017, 11:48 PM   #14
Master Member
 
rollanddie's Avatar
 
Join Date: Aug 2012
Posts: 4,332
hi

im assuming you are comparing the time, as in the system time value? it wont work because it is not even an integer. it is a set of values, eg: DDMMYYYYHHMMSS

system.out.println your time value first and check if its the value you want. you have to get the seconds/milliseconds of the time and convert it to integer.
not so complicated

i'm getting the user to input the time in 24hr format e.g. 0600 0700 1859 1959. doesn't matter if the user keys above 59 for the minute hand.
__________________
This space intentionally left blank.
rollanddie is offline   Reply With Quote
Old 09-07-2017, 12:04 AM   #15
Senior Member
 
kuma-mon's Avatar
 
Join Date: Apr 2017
Posts: 828
not so complicated

i'm getting the user to input the time in 24hr format e.g. 0600 0700 1859 1959. doesn't matter if the user keys above 59 for the minute hand.
icic,

for this case, i think you can just simply convert the input to int, then compare the values.

for eg: 0600 input is not a valid int, because got 0 infront, so u have to remove the 0. go google on how to remove 0 infront.

you have to also consider input like 00001, and 0000. so this would be converted to 1 and 0 in int.

then you can compare 1 > 600.
kuma-mon is offline   Reply With Quote
Reply
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Terms of Service for more information.


Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On