Closed Thread
Results 1 to 4 of 4

Thread: Switch Statement

  1. #1
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Switch Statement

    I wanted to know that how can you use switch statement like you do in if
    for e.g
    if(x<80 && c>=90)
    cout<<"\n Grade is A- "


    how can i do this with switch statement?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Switch Statement

    switch statements evaluate integral or char values (doest use '<' or '>')
    you will have to make a char variable to hold the grade, the code can look like this
    Code:
    char grade;
    if(x<80 && c>=90) grade='A'
    if(x<70 && c>=80) grade='B'
    
    switch (grade)
    {
         case 'A':
                 cout<<"\n Grade is A- ";
                 break;
        case 'B':
                 cout<<"\n Grade is B- ";
                 break;
    
        default:
           cout << "grade wasnt assigned";
           break;
    }
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  4. #3
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Switch Statement

    Like amrosama told, you can't use <, >, and other operators. It must be a constant value, what you want can be done with select case statements in visual basic, but i don't recomend you to learn visual basic

  5. #4
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Switch Statement

    Quote Originally Posted by ahmed View Post
    how can i do this with switch statement?
    This is uglier than it looks, and it looks ugly:
    Code:
    char *foo(int grade)
    {
       char *letter = "F";
       switch ( grade )
       {
       case 100: case 99: case 98: case 97: letter = "A+"; break;
       case  96: case 95: case 94: case 93: letter = "A";  break;
       case  92: case 91: case 90:          letter = "A-"; break;
       case  89: case 88: case 87:          letter = "B+"; break;
       case  86: case 85: case 84: case 83: letter = "B";  break;
       case  82: case 81: case 80:          letter = "B-"; break;
       case  79: case 78: case 77:          letter = "C+"; break;
       case  76: case 75: case 74: case 73: letter = "C";  break;
       case  72: case 71: case 70:          letter = "C-"; break;
       case  69: case 68: case 67:          letter = "D+"; break;
       case  66: case 65: case 64: case 63: letter = "D";  break;
       case  62: case 61: case 60:          letter = "D-"; break;
       default:  break;
       }
       return letter;
    }
    But it's on the subject of the question asked.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Switch Statement
    By docmonkey in forum PHP Development
    Replies: 4
    Last Post: 06-17-2011, 03:46 PM
  2. Replies: 6
    Last Post: 11-28-2010, 11:00 PM
  3. Declaring an array and using within switch statement
    By thintheherd in forum C and C++
    Replies: 3
    Last Post: 06-02-2010, 03:12 AM
  4. Replies: 3
    Last Post: 10-18-2008, 10:05 AM
  5. what's wrong with this switch statement...
    By digerati in forum C and C++
    Replies: 13
    Last Post: 10-04-2008, 06:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

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