Results 1 to 7 of 7

Thread: Convert from If/If Else to Switch Statement - how is it done?

  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Convert from If/If Else to Switch Statement - how is it done?

    I've searched the world over and have not been able to find an example that addresses using a switch statement vs. if/if else statements.

    This is for a homework assignment and the assigned text has one example that is not even close to assisting me with completing this assignment. Last week's assignment was to create a grade calculator using if statements. I completed that, but I can't figure out how to convert it. Please help!

    Each grade should post to a textarea on a form. I've got the form created, but I can't get the switch statement to write to that area. I'm a novice, so I need detailed instructions please. Thanks for your response!

    Code:
     if (gradeOut >= 90)
        gradeOut+= "My letter grade is: A";
      else if (totalGrade < 90 && totalGrade >= 80)
        gradeOut+= "My letter grade is: B";
      else if (totalGrade < 80 && totalGrade >= 70)
        gradeOut+= "My letter grade is: C";
      else if (totalGrade < 70 && totalGrade >= 60)
        gradeOut+= "My letter grade is: D";
      else if (totalGrade < 60)
        gradeOut+= "My letter grade is: F";

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    a switch statement is like this:
    Code:
    switch(variable) {
    case value:
      //execute if variable is value
      break;    
    case value2:
      //execute if variable is value2
      break;
    default:
     // code to be executed if variable is none
    }
    a switch statement is not meant to compare with other than ==. it can only check if something equals something else, but you can do it. you can check if boolean true equals an expression. It will return the first true expression, so there is no need for 2 expressions (totalGrade < 90 && totalGrade >= 80):
    Code:
    switch(true) {
    case gradeOut<60:
        gradeOut= "My letter grade is: F";
        break;
    case gradeOut<70:
        gradeOut= "My letter grade is: D";
        break;
    case gradeOut<80:
        gradeOut="My letter grade is: C";
        break;
    ...
    }
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. The Following User Says Thank You to Master_script_maker For This Useful Post:

    lilrhino (05-01-2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Master_script_maker View Post
    a switch statement is like this:
    Code:
    switch(variable) {
    case value:
      //execute if variable is value
      break;    
    case value2:
      //execute if variable is value2
      break;
    default:
     // code to be executed if variable is none
    }
    a switch statement is not meant to compare with other than ==. it can only check if something equals something else, but you can do it. you can check if boolean true equals an expression. It will return the first true expression, so there is no need for 2 expressions (totalGrade < 90 && totalGrade >= 80):
    Code:
    switch(true) {
    case gradeOut<60:
        gradeOut+= "My letter grade is: F";
        break;
    case gradeOut<70:
        gradeOut+= "My letter grade is: D";
        break;
    ...
    }
    Thanks for your response. Switch statements don't seem to be used extensively. I have several JavaScript books that I got from the library, but it's vaguely addressed.

    I'll try your example, and see if it will get me what I need. I tried some similar syntax but I enclosed the (gradeOut <60) in parentheses, so that's why it didn't work. It just kept returning undefined or false.

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Even what you had could be greatly simplified:

    Code:
    var gradeLetter="F"
     if (totalGrade > 89)
        gradeLetter = "A";
      else if (totalGrade > 79)
        gradeLetter = "B";
      else if (totalGrade > 69)
        gradeLetter = "C";
      else if (totalGrade > 59)
        gradeLetter = "D";
    gradeOut += "My letter grade is: " + gradeLetter;
    Note: I changed the first test to totalGrade to match the others, as otherwise the output for 90 and above wouldn't get picked up or there would be an error.

    You could even do it like so:

    Code:
    gradeOut += "My letter grade is: " + (
    totalGrade > 89? "A" :
    totalGrade > 79? "B" :
    totalGrade > 69? "C" :
    totalGrade > 59? "D" : "F" );
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #5
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default convert if to switch in c++

    while(infile>>character)
    {
    if(character=='A')
    {
    infile>>value;
    append(arr,i,value);
    }
    else if(character=='I')
    {
    infile>>find>>value;
    if(i>98)
    cout<<"insert cannot be performed as the array is full"<<endl;
    else
    insert(arr,i,find,value);
    }
    else if(character=='D')
    {
    infile>>value;
    if(i==1)
    cout<<"there are no elements to delete"<<endl;
    else
    del(arr,i,value);
    }
    else
    display(arr);
    }
    system("pause");
    }

  7. #6
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    while(infile>>character)
    {
    if(character=='A')
    {
    infile>>value;
    append(arr,i,value);
    }
    else if(character=='I')
    {
    infile>>find>>value;
    if(i>98)
    cout<<"insert cannot be performed as the array is full"<<endl;
    else
    insert(arr,i,find,value);
    }
    else if(character=='D')
    {
    infile>>value;
    if(i==1)
    cout<<"there are no elements to delete"<<endl;
    else
    del(arr,i,value);
    }
    else
    display(arr);
    }
    system("pause");
    }

  8. #7
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up hope it solve your problem

    Quote Originally Posted by lilrhino View Post
    I've searched the world over and have not been able to find an example that addresses using a switch statement vs. if/if else statements.

    This is for a homework assignment and the assigned text has one example that is not even close to assisting me with completing this assignment. Last week's assignment was to create a grade calculator using if statements. I completed that, but I can't figure out how to convert it. Please help!

    Each grade should post to a textarea on a form. I've got the form created, but I can't get the switch statement to write to that area. I'm a novice, so I need detailed instructions please. Thanks for your response!

    Code:
     if (gradeOut >= 90)
        gradeOut+= "My letter grade is: A";
      else if (totalGrade < 90 && totalGrade >= 80)
        gradeOut+= "My letter grade is: B";
      else if (totalGrade < 80 && totalGrade >= 70)
        gradeOut+= "My letter grade is: C";
      else if (totalGrade < 70 && totalGrade >= 60)
        gradeOut+= "My letter grade is: D";
      else if (totalGrade < 60)
        gradeOut+= "My letter grade is: F";





    Code:
    //By King Lord...
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        double studentGrade = 0, sumOfGrades = 0, average = 0;
        double  highestGrade = 0, lowestGrade = 100;
        int numberOfGrades = 0;
        string answer;
        
        
        do {
        
            //Step 1: Gather the input of the studentGrade
            cout << "Please enter in the student grade as a percentage: "<<endl;
            cin >> studentGrade;
         
            cout <<"====================================================="<<endl;
            cout << "You entered the grade as "<<studentGrade<<endl;
         
            //Step 2: Add the grade to the sumOfGrades
            sumOfGrades += studentGrade;
            cout << "The sum of grade is "<<sumOfGrades<<endl;
         
            //Step 3: Add one to numberOfGrades
            numberOfGrades++;
         
            //Step 4: Calculate the average = sumOfGrades / numberOfGrades
            //average = sumOfGrades / numberOfGrades;
            //cout << "The average is "<<average<<endl;
         
            // Step 5: check if studentGrade > highestGrade
            //         then highestGrade = studentGrade   
            if (studentGrade > highestGrade)
            highestGrade = studentGrade;
         
            //Step 6: check if studentGrade < lowestGrade
            //        then lowestGrade = studentGrade
            if (studentGrade < lowestGrade)
            lowestGrade = studentGrade;
             
            cout <<"=====================================================\n"<<endl;   
            cout << "Do you like to enter another grade? (y/n)"<<endl;  
            cin >> answer;
            cout <<"====================================================="<<endl;
            cout <<"=====================================================\n"<<endl;
                
         }while(answer == "y" || answer == "yes" ||  answer == "Yes" || answer == "YES");
          
                 
        //Step 7: Output the average, highestGrade, lowestGrage
        cout <<"======================================================"<<endl;
        
        
        //Step 6.5: Calculate the average = sumOfGrades / numberOfGrades
        average = sumOfGrades / numberOfGrades;
        cout << "The average is "<<average<<endl;
        cout << "The sum of grade is "<<sumOfGrades<<endl;
        cout << "The highest grade is "<<highestGrade<<endl;
        cout << "The lowest grade is "<<lowestGrade<<endl;
        cout <<"====================================================="<<endl; 
        
        int avg = average/10;
        //Step 8: Output a character grade for the average
        switch(avg)
        {
           case 9:
                cout << "The grade is an A "<<endl;
                break;
                
           case 8:
                cout << "The grade is an B "<<endl;
                break;
                
           case 7:
                cout << "The grade is an C "<<endl;
                break;   
                
           case 6:
                cout << "The grade is an D "<<endl;
                break;
                 
           default:
                cout << "The grade is an F "<<endl;
                break;   
        }
    
    return 0;
    }

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
  •