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)
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";

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) {
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;
...
}

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) {
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.

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


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:


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)
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";








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