Why is it saying that there is a syntax error for the if/else statement I wrote? - logic

I'm trying to write a program that calculates the sum of a geometric series on a TI-84.
Prompt A
Prompt R
Prompt N
If N=100 and abs(R)<1
Disp (A/1-R)
Else
Disp (A(1-R^N))/(1-r)
It says that there is a syntax error at the Else line.

Else can only be paired with an If .. Then construct, not a plain If. So:
Prompt A,R,N
If N=100 and abs(R)<1
Then
Disp A/(1-R
Else
Disp (A(1-R^N))/(1-R
In general the If.. Then .. Else .. End construct should be closed by End but in this case the program exits anyway so it makes no difference. There is some documentation of this in the official TI-BASIC manual and you can check out a more detailed version here.

Related

VBS won't allow programming

I was doing some programming and I get an error message when I try and run it. my code:
MsgBox ("hi")
sleep (2)
MsgBox ("you ok" vbYesNo, "how are you?")
Select Case result
Case vbYes,
MsgBox ("that's good!")
Case vbNo, MsgBox ("sorry to hear that.")
End Select
Line: 3
Char: 18
Error: Expected ')' Source: Microsoft VBScript compilation error.
The problem is as the error says you have compilation issues but looking at the rest of the example code you also have other issues waiting to be corrected.
Missing argument syntax in MsgBox() Function:
Line: 3
Char: 18
Error: Expected ')' Source: Microsoft VBScript compilation error.
is because the Function MsgBox() expects arguments separated by a comma but the argument separator after "you ok" is missing causing the compiler to throw the exception.
Cannot use parentheses when calling a Sub:
When calling a procedure that doesn't return a value using brackets around multiple arguments passed to the procedure will cause;
Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub
There is a caveat to this which is if it's just one argument it will not fail to compile, but this isn't because it accepts one argument but because it sees the brackets as part of the argument not part of the procedure (Eric Lippert discusses this at length in his blog);
MsgBox("Hi") 'Will work
This is equivalent to writing;
Call MsgBox(("Hi")) 'Note the extra brackets
This can be a little confusing so when it comes to multiple arguments you might think this is ok;
MsgBox("you ok", vbyesno, "how are you?") 'This will error
But as the brackets now denote the argument parentheses it becomes invalid and throws a compilation error. To avoid this there are three things you can do;
Remove the parentheses
MsgBox "you ok", vbyesno, "how are you?"
Return a value (if the procedure can return a value)
result = MsgBox("you ok", vbYesNo, "how are you?")
As you check the value of result on the next line this would be the correct option as it result will contain the outcome of clicking either vbYes or vbNo in the MsgBox() function.
Use the Call statement to prefix the procedure call
Call MsgBox("you ok", vbYesNo, "how are you?")
Syntax Error:
Microsoft VBScript compilation error: Syntax error
This comes down to trailing commas after the Case arguments, just remove the commas and the code will compile.
Select Case result
Case vbYes
Call MsgBox("that's good!")
Case vbNo
Call MsgBox("sorry to hear that.")
End Select
The Sleep duration:
The Sleep() function expects a value expressed in milliseconds not seconds, at the moment the example code is waiting for 0.002 seconds. To wait for 2 seconds (which I'm assuming was the intention) use;
Call Sleep(2000)
Useful links
Answer to Can't Use Parentheses When Calling a Sub - VBScript

Bash Script for loop with nested if statement

I have a script like this:
#!/bin/bash
x=${1:-20}
for ((i=1;i<=x;i++))
{
if ((i%3==0))
{
echo 'Fizz'
}
echo $i
}
I get an error color on the last brace in VIM and when I try to run the script I get a "syntax error near unexpected token" for that same brace. Without the nested if statement, this will print 1 through 20 on a new line for each number, which is the expected outcome. If the number is divisible by 3, it should print Fizz instead of that number. I'm not as worried about how to implement the replacement, that should be easy to figure out, but what I don't understand is why I cannot use a brace to close the for loop. If I take out the brace, I get an error that says end of file expected. So what is the proper syntax for ending a for loop with a nested if statement? I've looked around online and here on stack but haven't found a similar format to what I am trying to do. I don't like the
for f in *
format as it is not as easy to read for someone coming from a different coding language and I like to keep my code looking very similar across different languages (I use comments too, but just the same, I try to keep things as similar as possible which is why I used (( )) with the for loop.)
If I comment out the if statement and leave everything else intact, the error disappears and it will print
1
Fizz
2
Fizz
etc.
Any insight into this would be greatly appreciated. Thanks!
So here is what I was able to figure out thanks to #Cyrus:
x=${1:-20}
for ((i=1;i<=x;i++))
do
if ((i%3==0))
then
echo 'Fizz'
else
echo $i
fi
done
In many ways bash is simpler than most other languages but that makes it harder to work with when you are used to "higher" level languages.
So, to help out anyone else that's like me and just starting to code with bash, here is the full program I made, with comments as to why I coded it the way I did. If there are errors in my explanation or my formatting style, please point them out. Thanks! This was kind of fun to write, call me crazy.
# This will literally just print the string inside the single quotes on the screen
echo 'Try running this again but with something like this: fizzbuzz 25 pot kettle black'
# The $0 is the first index, in other words the file name of the executable,
# this will set the default value of x to 20 but will allow the user to input
# something else if they want.
x=${1:-20}
# This is the same but with string variables
f=${2:-FizzBuzz}
g=${3:-Fizz}
b=${4:-Buzz}
# We start the index variable at 1 because it's based on the input,
# otherwise it would echo 0 thru 19
for ((i=1;i<=x;1++))
do
# I recommend using (( )) for if statement arithmetic operations
# since the syntax is similar to other programming languages
if ((i%3==0 && i%5==0)); then echo $f
# you need to use a semicolon to separate if and then if they are
# on the same line, otherwise you can just go to the next line for
# your then statement
else if ((i%3==0)); then echo $g
else if ((i%5==0)); then echo $b
else echo $1
# You need fi in order to finish an if then statement
fi fi fi
done

tcl's subst seems slower than set, when getting a variable value from a variable with a variable's name

See Tcl 8.4 code below and shell output below: (I need meta- coding):
% set k a
% set m k
% puts [set $m ]
a
% puts [subst $$m]
a
So, it appear that set $m and subst $$m have the same functionality. However, the runtime (in the simple testcase) is rather different (see continuation of shell results below:
% time { set $m } 1000000
0.256435 microseconds per iteration
% time { subst $$m } 1000000
0.627714 microseconds per iteration
As can be seen, set is ~2.5 faster than subst. 2 questions are: to be asked:
1. Why?
2. I have seen that it is ~3.6 faster in Tcl 8.5. Can we expect that this will remain the case, in future releases?
Thanks
You can expect things to remain exactly the same. When you use:
puts [set $m]
Tcl will compile that once to a read of the m variable (storing the result on the internal operation stack) a read of the variable whose name is on the operation stack, and then a call of puts with the result.
When you do:
puts [subst $$m]
Tcl compiles that to a concatenation of $ and the results of reading m, a call to the substitution engine (which in turn will parse and bytecode compile that fragment) and only then a puts of the result. Which is entirely more complicated.
You'd see the difference if you did:
set m {k[exit]}
The first would just tell you that you were trying to read from a (strangely-named) variable that didn't exist. The second would quit the process.

why is the syntax for "condition" and "break" different in gdb?

I seem to be having an issue with gdb. Let's say I create a conditional breakpoint in a loop over i with the command
break file.cpp:line_no if i==120
everything good, it creates breakpoint 3 and the code breaks when i==120. Now I'm getting a problem at i==495, and I want to re-use the same breakpoint, so I use:
condition 3 if i==495
Well, there I get a "syntax error in expression near `if i==495' So I try:
condition 3 i==495
No more error, and the info for the breakpoint looks promising:
3 breakpoint keep y <MULTIPLE>
stop only if i==495
But it doesn't stop at the breakpoint when the condition is reached. So I start writing this question, and it occurs to me to try:
condition 3 "if i == 495"
and hooray it works. So I guess my questions are: 1. Why the different syntax between the two commands? and 2. Why is the info so confusing? The new info says 'if "if i==495"', but shouldn't that only evaluate to true if 'i==495' evaluates to true?
Your last try is not doing what you think it is doing. When you write:
cond 3 "if i == 495"
the condition is actually the string constant "if i == 495", which is never false. So, the breakpoint stops.
You can see this either by doing print i (for some hits it won't be 495), or by cond 3 "anything goes here!".
The correct syntax was your second attempt:
cond 3 i == 495
I can't say why this isn't triggering for you.
If you are using gdb to debug fortran it might happen that you get a syntax error when trying the reasonable command:
(gdb) b foo.f:10 if i == 0
A syntax error in expression, near `= 0'.
The reason for this is that you must use the old-style fortran syntax (.eq. rather than == and so on) for logical operators:
(gdb) b foo.f:10 if i .eq. 0
Breakpoint 1 at 0x432738: file foo.f, line 10.

Modelica conditional printing (for debugging)

In Modelica, is it possible to have an if-condition and a command in one line?
(Of course it is possible to write it in three lines, but I would prefer it in one line.)
Something like:
Boolean verbose;
...
if verbose then Modelica.Utilities.Streams.print("iteration steps " + String(iter), "printlog.txt");
What is wrong with the code you wrote? Only thing I find missing is the end if.
if cond then print(str, file); end if;

Resources