PDA

View Full Version : Convert from If/If Else to Switch Statement - how is it done?

lilrhino
04-30-2008, 10:36 PM
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!

if (gradeOut >= 90)
else if (totalGrade < 90 && totalGrade >= 80)
else if (totalGrade < 80 && totalGrade >= 70)
else if (totalGrade < 70 && totalGrade >= 60)
else if (totalGrade < 60)

Master_script_maker
05-01-2008, 02:18 AM
a switch statement is like this:

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):

switch(true) {
break;
break;
break;
...
}

lilrhino
05-01-2008, 02:24 AM
a switch statement is like this:

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):

switch(true) {
break;
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.

jscheuer1
05-01-2008, 07:10 AM
Even what you had could be greatly simplified:

if (totalGrade > 89)
else if (totalGrade > 79)
else if (totalGrade > 69)
else if (totalGrade > 59)

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:

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

victory
09-25-2010, 01:13 AM
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");
}

victory
09-25-2010, 01:14 AM
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");
}

kinglord
09-08-2013, 01:02 AM
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!

if (gradeOut >= 90)
else if (totalGrade < 90 && totalGrade >= 80)
else if (totalGrade < 80 && totalGrade >= 70)
else if (totalGrade < 70 && totalGrade >= 60)
else if (totalGrade < 60)

//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;

do {

//Step 1: Gather the input of the studentGrade
cout << "Please enter in the student grade as a percentage: "<<endl;

cout <<"====================================================="<<endl;
cout << "You entered the grade as "<<studentGrade<<endl;

cout << "The sum of grade is "<<sumOfGrades<<endl;

//Step 4: Calculate the average = sumOfGrades / numberOfGrades
//cout << "The average is "<<average<<endl;

// Step 5: check if studentGrade > highestGrade

//Step 6: check if studentGrade < lowestGrade

cout <<"=====================================================\n"<<endl;
cout << "Do you like to enter another grade? (y/n)"<<endl;
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
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;
}