Author Topic: IF i could make it less....  (Read 517 times)

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
IF i could make it less....
« on: March 13, 2011, 08:36:17 AM »
C programming:

The user has to put in 7 independant values which are assigned 7 different variables. Is there a way to check they are all >=0 with less code?

This does work, but I'm curious.

Code: [Select]
if (y1>=0 && y2>=0 && y3>=0 && y4>=0 && y5>=0 && y6>=0 && y7>=0)

My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: IF i could make it less....
« Reply #1 on: March 13, 2011, 09:17:53 AM »
C programming:

The user has to put in 7 independant values which are assigned 7 different variables. Is there a way to check they are all >=0 with less code?

This does work, but I'm curious.

Code: [Select]
if (y1>=0 && y2>=0 && y3>=0 && y4>=0 && y5>=0 && y6>=0 && y7>=0)


if !(y1<0 || y2<0 || y3<0 || y4<0 || y5<0 || y6<0 || y7<0)

is this less code for you ?  ;D

AS

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: IF i could make it less....
« Reply #2 on: March 13, 2011, 09:54:07 AM »
C programming:

The user has to put in 7 independant values which are assigned 7 different variables. Is there a way to check they are all >=0 with less code?

This does work, but I'm curious.

Code: [Select]
if (y1>=0 && y2>=0 && y3>=0 && y4>=0 && y5>=0 && y6>=0 && y7>=0)


Good one. ;D

if !(y1<0 || y2<0 || y3<0 || y4<0 || y5<0 || y6<0 || y7<0)

is this less code for you ?  ;D

AS


Good one!
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p