11 Replies - 355 Views - Last Post: 09 January 2013 - 08:58 PM Rate Topic: -----

#1 ShadowScripter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

switch instead of if else

Posted 09 January 2013 - 12:00 PM

First of all I made this in the iPad so it might not be in perfect coding but I was wondering if i could shorten the script with "switch" instead of "if....else" Also I will use Microsoft Visual C++ 2010 for the other errors I know I have. Many thanks in advance.
//run

#include <iostream>
#include <cmath>

label start:
cout << "After any error press q for ERROR n Help"
cout << "enter the sides in small,long,hypt format"
cin >> (small);
cin >> (long);
cin >> (hypt);
func (hypteq)
{
sqrt
{
sqr small + sqr long
}
}
int main()
{
 if {
small > hypt 
cout << "ERROR 1 Not a right triangle";
{
if cin << q
go to E1
}
} else {
goto ( long check)

label long check: {
if {
long > hypt
cout << "ERROR 2 Not a right triangle."
{
if cin << q
go to E2
}
} else {
goto checks0
}

label checks0: {
if small < 1
cout << "ERROR 3 Not a right triangle."
{
if cin << q
go to E3
}
} else {
goto checkl0
}

label checkl0: {
if long < 1
cout << "ERROR 4 Not a right triangle"
{
if cin << q
go to E4
}
} else {
goto (checkh)

label checkh: {
if hypt != hypteq
cout << "ERROR 5 not a right triangle"
{
if cin << q
go to E5
}
} else {
cout << hypeq
}

label E1:
cout << "small side bigger then hypt try again";
go to (start)

label E2: 
cout << "small size less then 1 try again";
goto (start)

label E3:
cout << "long side greater then hypt try again";
goto (start)

label E4:
cout << "long side less then 1 try again";
goto (start)

label E5:
cout: "hypt not equal to equation try again" ;
goto (start)

return = 0
}



Is This A Good Question/Topic? 0
  • +

Replies To: switch instead of if else

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3475
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: switch instead of if else

Posted 09 January 2013 - 12:10 PM

Are you sure that code is C++ ? What happened to all the semicolons?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: switch instead of if else

Posted 09 January 2013 - 12:31 PM

A switch won't make that code less awful. Get rid of the gotos and you'll have a start on it. Since your "E4" and so forth are just printing output to the command line, try just printing the literal strings:



if cin << q
  cout << "small side bigger then hypt try again";


Better still, use named strings for your error message, or put them in an array and reference them by number. (if you #DEFINE a short name for the error number, that's even better)
Was This Post Helpful? 1
  • +
  • -

#4 ShadowScripter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

Re: switch instead of if else

Posted 09 January 2013 - 12:45 PM

View Postjon.kiparsky, on 09 January 2013 - 12:31 PM, said:

A switch won't make that code less awful. Get rid of the gotos and you'll have a start on it. Since your "E4" and so forth are just printing output to the command line, try just printing the literal strings:

i only asked for a awnser not uncalledfor critism. I know goto's are bad, thats why i asked if i could use a switch, so i could get rid of those. like i said, i made it on a notepad, not a scripter so all i wanted was a yes or no and small explination why. nothing about the rest of my script.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: switch instead of if else

Posted 09 January 2013 - 12:50 PM

And I answered: if you use a switch instead of those ifs, your code will still be awful. I also told you how to make it better.

You do want it to be less bad, right? That's what you were asking for? Yes?
So what are you complaining about?

This post has been edited by jon.kiparsky: 09 January 2013 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 ShadowScripter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

Re: switch instead of if else

Posted 09 January 2013 - 12:54 PM

View PostSkydiver, on 09 January 2013 - 12:10 PM, said:

Are you sure that code is C++ ? What happened to all the semicolons?

I'm sorry but i don't understand how semicolons answer my question about switches. please don't just comment about my script that i said i would work on.

View Postjon.kiparsky, on 09 January 2013 - 12:50 PM, said:

And I answered: if you use a switch instead of those ifs, your code will still be awful. I also told you how to make it better.

You do want it to be less bad, right? That's what you were asking for? Yes?
So what are you complaining about?

No, I want it shorter, not someones opinion on "better" if the script works, its good enough for me. and anyone else wont be looking at the script, just the program related to it.
Was This Post Helpful? 0
  • +
  • -

#7 Switters  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 110
  • Joined: 03-June 12

Re: switch instead of if else

Posted 09 January 2013 - 01:03 PM

Your if-else structures can probably be improved and shortened by the use of a switch statement if the "decision" variable in your switch statement has many outcomes that result from the same value of the switch. So yes, try it.

Incidentally, your ability to get good feedback on the forum is also sure to increase if you drop some of the sensitivity. I'm relatively new and I already scratch my head when I see people sign up, post a rather incoherent piece of code on the same day that shows little understanding of the core subject matter, and then get indignant when people bother to take the time to point out other ways to improve. If you can't take your lumps, don't ask for free advice. If you want someone to just smile and do exactly what you ask no matter what they actually think, you'll have better luck with a dog and a tennis ball.
Was This Post Helpful? 2
  • +
  • -

#8 ShadowScripter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

Re: switch instead of if else

Posted 09 January 2013 - 01:25 PM

View PostSwitters, on 09 January 2013 - 01:03 PM, said:

Your if-else structures can probably be improved and shortened by the use of a switch statement if the "decision" variable in your switch statement has many outcomes that result from the same value of the switch. So yes, try it.

Incidentally, your ability to get good feedback on the forum is also sure to increase if you drop some of the sensitivity. I'm relatively new and I already scratch my head when I see people sign up, post a rather incoherent piece of code on the same day that shows little understanding of the core subject matter, and then get indignant when people bother to take the time to point out other ways to improve. If you can't take your lumps, don't ask for free advice. If you want someone to just smile and do exactly what you ask no matter what they actually think, you'll have better luck with a dog and a tennis ball.

I suppose the sensitivity is a little much but i was just saying people should read the question fully. if the first two did they would realize i knew the script could look better all i needed was what you answered perfectly.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: switch instead of if else

Posted 09 January 2013 - 01:31 PM

View PostShadowScripter, on 09 January 2013 - 04:25 PM, said:

I suppose the sensitivity is a little much but i was just saying people should read the question fully. if the first two did they would realize i knew the script could look better all i needed was what you answered perfectly.

No, that was the minimum you needed. You got much better. Be happy, and use the advice.
Was This Post Helpful? 1
  • +
  • -

#10 ShadowScripter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

Re: switch instead of if else

Posted 09 January 2013 - 02:36 PM

View PostCTphpnwb, on 09 January 2013 - 01:31 PM, said:

View PostShadowScripter, on 09 January 2013 - 04:25 PM, said:

I suppose the sensitivity is a little much but i was just saying people should read the question fully. if the first two did they would realize i knew the script could look better all i needed was what you answered perfectly.

No, that was the minimum you needed. You got much better. Be happy, and use the advice.

All i wanted was to know if the switch would work, not how it looked not to get rid of goto's, and not how to make it look better. Just if swapping a switch for the if...else would function.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3475
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: switch instead of if else

Posted 09 January 2013 - 03:17 PM

It depends on the conditions of your if statements. If you are checking for equality or small finite range, then a switch statement would make things more compact and easier to read. If your your if statements as doing simple greater than or less than comparisons with open ranges, then a switch statement won't even work. Additionally if you are doing equality comparisons, but the value you are comparing against are the return value of functions or variables, then a switch statement won't work.

This post has been edited by Skydiver: 09 January 2013 - 03:18 PM

Was This Post Helpful? 1
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: switch instead of if else

Posted 09 January 2013 - 08:58 PM

Well, it is not very good programming practice to use go-to's in your program as it would cause Spaghetti code...so you need to take them out and in the process, you can use looping constructs for the same.

One thing I noted about your code was the fact that you had used '<<' for a cin...well...it should have been '>>' as '<<' is only used for cout.

Another thing I noted was the use of your if conditions...you seem to be using if without any conditons...and then you seem to be giving conditions in the following '{' brackets

It would be very good if you could give the condition along with the if as follows...here's a small example.

32	if {
33	long > hypt
34	cout << "ERROR 2 Not a right triangle."


could be

32	if(long > hypt)
33        {
34     	    cout << "ERROR 2 Not a right triangle.";
35        }


and yes...a switch case can replace your if conditions if you make the if conditions as cases of your switch control...and for default, you can say...invalid.

edit:Small typo

regards,
Raghav

This post has been edited by raghav.naganathan: 09 January 2013 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1