I get an error at the 'Else' on a TI-84 Plus.
I can't figure out why this doesn't work.
I'm writing a GCD program just as an exercise in programming a TI calculator.
It's recursive (or as recursive as TI-BASIC gets).
If B=0
Disp A
Else
C->B
B->remainder(A,B)
A->B
prgmGCD2
TI-Basic is often rather picky about the syntax of if statements.
There are three general formats for an If statement.
Single Statement If
:If <boolean>
:<expression>
Note that <expression> consists of exactly one line of code.
Multi Statement If
:If <boolean>
:Then
:<expresion>
:<expresion>
:End
As opposed to the first option, this option can contain any number of lines of code after the If.
If Else
:If <boolean>
:Then
:<expresion>
:<expresion>
:Else
:<expresion>
:<expresion>
:End
As with the previous option, any number of statements can be put after the If and after the Else.
You are obviously trying to use an if else statement. The correct syntax for this is:
:If B
:Then
:C->B
:B->remainder(A,B)
:A->B
:prgmGCD2
:Else
:Disp A
:End
Related
I've got a small code in TI BASIC on my TI-84 Plus C Silver Edition calculator that will determine correct dosage of drugs based on the patient's weight. For example, if aspirin is given at 5 mg per kg of patient weight (it isn't), then the code should tell me to give a 100kg patient 500mg of aspirin.
However, the code is solving for every possible drug. Here it is:
PROGRAM:DRUG1
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",A
:If A=IPPI
:Disp "DOSAGE",W*2
:If A=NEVO
:Disp "DOSAGE", W*0.5
So in this case, the two drugs are IPPI and NEVO. If I give a patient weight of 100kg, and choose IPPI, then I would expect to see
DOSAGE 200
However, what I do see is
DOSAGE 200
DOSAGE 50
so apparently both "if" statements are running, even though I've given a only one value (IPPI). [The same error occurs when I set A as NEVO].
I've tried enclosing both If statements within Then...End as well, so the code would look like:
PROGRAM:DRUG1
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",A
:If A=IPPI
:Then
:Disp "DOSAGE",W*2
:End
:If A=NEVO
:Then
:Disp "DOSAGE", W*0.5
:End
but that changes nothing.
I'm pretty new to BASIC, so I'm sure there's a simple error that I can't see, but I'm stumped at the moment.
You need to change the second Input command so the information is stored to a string instead of the numeric variable A. TI-84 series calculators have ten string variables in the [VARS][7] menu for this purpose.
Note also that you must compare the string against the string "IPPI" rather than the sequence of letters (numeric variables) IPPI. So your code could be:
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",Str1
:If Str1="IPPI"
:Disp "DOSAGE: ",W*2
:If Str1="NEVO"
:Disp "DOSAGE: ",W*0.5
or more concisely:
:Input "PATIENT WEIGHT: ",W
:Input "AGENT NAME: ",Str1
:Disp "DOSAGE:"
:If Str1="IPPI"
:Disp 2W
:If Str1="NEVO"
:Disp .5W
You're trying to use variable names as strings.
:If A=IPPI
This isn't comparing a string to "IPPI", it's comparing a numeric variable A to the numeric value I*P*P*I, which I'm guessing results in 0 in your case.
Similarly, when you take input, if you enter IPPI, it's going to multiply those variables and assign A to be that product.
You'll need to use a string variable and quotes.
The main problem with your program is that you aware assigning a string to a variable that only supports numbers. That leaves the new value of the variable the Boolean value of the string, True, which in the case of TI-BASIC is the value, 1. To fix this you need to assign it to a variable which supports characters in a string, in this case you can use STR1.
Ok, here's my problem. I wrote an advanced Pythagorean Theorem program, but it apparently is having exceptions. Here's an instance of my problem. When I input A? √(3) and B? 2, I get 0 back. Here's the code:
:Prompt A,C
:(C^2-A^2)->B
:If B<0
:Then
:Disp "THAT IS N
OT A VALID TRIA
ANGLE
:Else
:If iPart(√(B))≠
√(B)
:Then
:Disp "B = √(",B
:Else
:Disp "B = ",√(B)
:End
:End
Therefore, if B = 1, then hypothetically it should output B = 1 but instead it outputs:
A=? √(3)
C=? 2
B = √(
1
Done
What am I doing wrong and how can I fix it?
When I quickly evaluate your program, it seems to work correctly when you get B≠1. For example if I want to calculate the famous 3,4,5 - triangle it shows:
A=?4
C=?5
B=
3
Done
Apparently the iPart( doesn't work correctly with √(1). You could include an extra statement to the If iPart( ... statement to rule this out. Like this.
:...
:If iPart(√(B))≠√(B) and B≠1
:...
Besides that I think the program looks cleaner and nicer if you use the Input, ClrHome and Output( commands.
:ClrHome
:Input "A: ",A
:Input "C: ",C
:(C^2-A^2)->B
:If B<0
:Then
:Output(4,1,"THA
T IS NOT A")
:Output(5,1,"VAL
ID TRIANGLE")
:Else
:If iPart(√(B))≠
√(B) and B≠1
:Then
:Output(3,1,"B:
√( )")
:Output(3,5,B)
:Else
:Output(3,1,"B:")
:Output(3,5,√(B))
:End
:End
:Pause
:ClrHome
Now the results screen looks something like this:
A: √(3)
C: 2
B: 1
I think this is cleaner, with the 3 aligned istead of in the bottom right corner. When you press ENTER everything will remove itself from the screen (due to the Pause command).
My Program, just learning how to code calculators today, is not giving me any response but
"DONE"
PROGRAM:FACTORS
:ClrHome
:Input "A=", A
:Input "B=", B
:Input "C=", C
:For(D,1,100,1)
:For(E,1,100,1)
:If (D*E)=C and (D+E)=B
:Stop
:End:End:End
:Disp D
:Disp E
Two problems:
1: All of the ":end"s are on the same line. Do a different one for each
2: This is probably the biggest problem: The "stop" command. "Stop" is used to end the program altogether, and go back to regular function. I'm assuming what you want to do is make it stop looping once D*E=C and once D+E=B. In that case, you can do one of two things: write the breakout code into a repeat loop; for instance
:ClrHome
:Input "A=", A
:Input "B=", B
:Input "C=", C
:For(D,1,100,1)
:For(E,1,100,1)
:Repeat (D*E)=C and (D+E)=B
:End
:End
:End
:Disp D
:Disp E
Or, you can use a Goto command
:If (D*E)=C and (D+E)=B
:Goto Lbl A
And further down in your code, you would put the "Lbl A" above where you wanted it to display your variables
The problem you have is that stop ends the program entirely instead of just breaking the loops. To fix this, instead or using For loops, you could use Repeat loops:
:1→D
:Repeat (D*E=C and D+E=B) or D=100
::1→E
::Repeat (D*E=C and D+E=B) or E=100
:::E+1→E
::End
::1+D→D
:End
You can ignore the extra colons, they are just there for clarity, but if you leave them the code will still work because they function identically to newlines.
The Repeat loops will break by themselves when the condition D*E=C and D+E=B is met, but you have to handle the initialization and incrementing of the variables E and D yourself.
Also note that your factoring algorithm can fail if A does not equal one. Consider dividing both B and C by A, and then outputting A as a constant factor.
Another error with your code is that you have too many End statements, but fixing this would not fix the program, and it would still exit at the Stop. An If without a Then does not need an End, but only one line will be run if the condition is true. For example:
:If <condition>
:<one statement>
or
:If <condition>
:Then
:<statement 1>
:<statement 2>
:<statement ...>
:<statement n>
:End
I want the code for the following case using Ruby (RoR):
line no: 09
line no: 10 if(#yyyyy == nil)
line no: 11 do some operation here
line no: 12 then goto line no 10
line no: 13 end
l
I tried with next, break, & goto, but nothing is working.
Is there any keyword/statement to fulfill my scenario?
Ruby doesn't support it by default, and, if you were to submit the code to http://codereview.stackoverflow.com, I'm sure they'd help you to rewrite/refactor the code.
You can probably use the redo command to simulate what you want. "Programming Ruby" says:
redo repeats the current iteration of the loop from the start but without reevaluating the condition or fetching the next element (in an iterator).
That said, "The Joke Is On Us: How Ruby 1.9 Supports the Goto Statement" will give you insight into actually using a "goto" with Ruby, however, at that point, your code will not be portable or usable in a "stock" Ruby.
Please do check the following URL:
http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement
As per many expert programmers, using the goto statement is a bad practice.
catch :foo do
for (aaa in #xxxxx)
if(#xxxxx == nil)
do some operation here
throw :foo
end
for (bbb in #yyyyy)
if(#yyyyy == nil)
do some operation here
throw :foo
end
end
for (ccc in #zzzzz)
if(#zzzzz == nil)
do some operation here
throw :foo
end
end
end
end
I've found three ways to write the same condition in Ruby:
#1
if 1==1
puts "true"
end
#2
puts "true" if 1==1
#3
if 1==1 then puts "true" end
Why can't I do this?
#4
if 1==1 puts "true"
I don't understand:
Why then and end are needed in #3, and,
Why I need to change the order to get #2 to work.
Statement #4 seems like the most natural way to write this. I don't understand why it's not possible.
The "if x then y end" syntax is meant for multiline conditionals while the "y if x" form is meant for concise single-line conditional statements. The then is necessary in the first case to tell Ruby that the condition is over (since Ruby doesn't require parens like C), and the end is necessary to tell Ruby that the whole if block is over (since it can be multiple lines).
You can replace the then with a semicolon, because an end-of-line also signals the end of the condition. You can't get rid of the end with a multiline if. Either use the second form or the ternary operator if you want a concise one-liner.
For example, suppose
x = true
the following will evaluate true, and return y
x ? y :
=> y
likewise, this will evaluate false and return nothing
!x ? y :
=>
add a term after the ':' for the else case
!x ? y : z
=> z
The thing is that both ways actually are a natural way to think:
if this is true then do something
do something if this is true
See? Ruby tries to get close to English syntax this way. The end is just necessary to end the block, while in the second version, the block is already closed with the if.
To actually answer your question, I think there is no chance to get the then and end removed. Remember Pascal / Delphi? You have a then there as well. It's typical only for C-style languages to not have it.
What about using a colon instead of then like this? http://www.java2s.com/Code/Ruby/Statement/layoutanifstatementisbyreplacingthethenwithacolon.htm
There are various ways you could short circuit if you wanted to do so.
The conditional statement is just part of the Ruby syntax to make it more English like.