Results 1 to 8 of 8

Thread: switch statement.....easy

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    how to do u use greater than or less than signs with switch and case statements?

    case > 5
    case < 5

    i'm using that but that obviously doesn't work
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I think it's easier with if statements, but try this:

    Code:
    switch(a)
    case a > 5:
    //do something
    break;
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  3. #3
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Use this instead:

    Code:
    #include <iostream.h>
    
    void main()
    {
    	int a;
    	int b;
    	cout <<"Value for a: ";
    	cin>>a;
    	cout<<"Value for b: ";
    	cin>>b;
    	
    	if(a > b)
    		cout<<"a is greater than b"<<endl;;
    	if (a < b)
    		cout<<"a is lesser than b"<<endl;
    	if (a == b)
    		cout<<"a and b are equal"<<endl;
    
    }
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    the if is definitely easier but i have to use a switch
    statement as part of an assignment.
    Bababooey
    Tatatoothy
    Mamamonkey

  5. #5

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    thx harry but i'm very familiar with case statements, what i wanted to accomplish, however, was to compare the switch against another number
    similar to this in VB:
    [CODE
    case is < 5:

    case is > 5:
    [/CODE]
    Bababooey
    Tatatoothy
    Mamamonkey

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I know what you want, but what I'm saying is that you can't use expressions with variable results in a switch statement. No two cases can evaluate to the same result. There is no 'case Is < 5' style solution.

    I know you said that you have to use switch statements in the assignment, but isn't there anywhere else you could us a switch statement? This seems like an inappropriate situation for one.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    case 1:
    case 2:
    case 3:
    case 4: code here
    break
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8
    Addicted Member Active's Avatar
    Join Date
    Jan 2001
    Location
    Lat: 13° 4' 46" N, Long: 80° 15' 20" E
    Posts
    209
    I think of this..

    Code:
    	int y=45;
    
    	switch(y>5)
    	{
    	case 0:
    	  printf("Y is less than or equal to 5");
    	  break;
    	case 1:
    	  printf("Y is Greater than 5");
    	  break;
    	default:
    	}
    If you can't beat your computer at chess, try kickboxing !!!
    [Download Tag Editing Tools.]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width