3 Replies - 265 Views - Last Post: 23 August 2009 - 05:18 PM Rate Topic: -----

#1 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Making the condition shorter

Post icon  Posted 23 August 2009 - 04:58 PM

How can i make this condition shorter or in a different way:

if(cl==1&&(y==300||y==301||y==302||y==303))


There has to be another way.
Is This A Good Question/Topic? 0
  • +

Replies To: Making the condition shorter

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Making the condition shorter

Posted 23 August 2009 - 05:01 PM

if(cl==1&&(y>=300 && y<=303))


Assuming the numbers are ints (no decimals). But I agree, there has to be another way.

Depending on what you want your code to do, I guess, you could do a while loop and a switch case. Not really less code, but fun anyway.

int cl=1, y;
while(cl==1){
    cout<<"Enter y value: ";
    cin>>y;
    switch(y){
        case 300:
        case 301:
        case 302:
        case 303:cout<<"thank you.\n""Enter 1 to continue or any other number to quit.\n";
             cin>>cl;
             break;
        default: cout<<"Incorrect value, try again."<<endl;
    cout<<"enter 1 to continue or any other number to quit.\n";
    cin>>cl;
    }
}

This post has been edited by OliveOyl3471: 23 August 2009 - 05:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Making the condition shorter

Posted 23 August 2009 - 05:05 PM

Wow how did i not saw that, thank you olive i would take this one. Atleast is alot better than what i was doing.

*Need to get away from computer for a sec now*
Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Making the condition shorter

Posted 23 August 2009 - 05:18 PM

You're welcome. Glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1