freak wrote:Beware though that the results are different if you change the order:
Code: Select all
System.out.println( i + 2 + " Result" ); // 9 Result
System.out.println( (i+2) + " Result" ); // 9 Result
That is because "a + b + c" is interpreted as "(a + b) + c" and thus the evaluation kicks in in both cases.
Imho it is clearer to just concatenate all operands if strings and numbers are mixed and not try to be smart and evaluate some of the expressions before. You can always use Str() if that is actually the result you want and you have full control.
Please allow me to add a multiplication to the examples in Java and C# (to be more clear what we are talking about).
Java:
Code: Select all
package app;
public class Main {
public static void main(String[] args) {
int i = 7;
System.out.println( "Result: " + i + 2 ); // Result: 72
System.out.println( "Result: " + (i+2) ); // Result: 9
System.out.println( "Result: " + i + 2 * 6 ); // Result: 712
System.out.println( "Result: " + (i+2) * 6 ); // Result: 54
System.out.println( "Result: " + (i+ 2 * 6)); // Result: 19
System.out.println( i + 2 + " is the result" ); // 9 is the result
System.out.println( (i+2) + " is the result" ); // 9 is the result
System.out.println( i + 2 * 6 + " is the result" ); // 19 is the result
System.out.println( (i+2) * 6 + " is the result" ); // 54 is the result
}
}
C#:
Code: Select all
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
int i = 7;
Console.Out.WriteLine( "Result: " + i + 2 ); // Result: 72
Console.Out.WriteLine( "Result: " + (i+2) ); // Result: 9
Console.Out.WriteLine("Result: " + i + 2 * 6 ); // Result: 712
Console.Out.WriteLine("Result: " + (i + 2) * 6 ); // Result: 54
Console.Out.WriteLine("Result: " + (i + 2 * 6)); // Result: 19
Console.Out.WriteLine( i + 2 + " is the result" ); // 9 is the result
Console.Out.WriteLine( (i+2) + " is the result" ); // 9 is the result
Console.Out.WriteLine( i + 2 * 6 + " is the result" ); // 19 is the result
Console.Out.WriteLine((i + 2) * 6 + " is the result"); // 54 is the result
}
}
}
Both languages give the same and correct results. It is the opposite of magic,
it is just following the operator precedence rules.
What we call operator precedence as programmers is what we begin to learn
in kindergarten (1+2 = 3) and we add new stuff later in school:
1 + 2 * 3 = 7
(1+2) * 3 = 9
We learned that parenthesis have higher priority than plus, minus, multiplication, division.
We also learned that operators of the same priority are evaluated from left to right.
(Side note 1: Parenthesis are missing from the operator precedence table in the PB manual)
(Side note 2: Parenthesis are called "Brackets" in PB manual, whereas "Brackets" is used for [] most of the time)
PB reference manual -> Variables, Types and Operators:
() Brackets. You can use sets of brackets to force part of an expression to be evaluated first, or in a certain order.
Example:
a = (5 + 6) * 3 ; Result will be 33 since the 5+6 is evaluated first
b = 4 * (2 - (3 - 4)) ; Result will be 12 since the 3-4 is evaluated first, then the 2-result, then the multiplication
PB supports this common rule generally, but not in:
It is the same rule. Expressions in Parenthesis/Brackets are evaluated first, then the result is added to the string.
It may be confusing at first, even for some of the experienced PB programmers, because it is a new PB feature.
You learn about operator precedence in every beginner's programming course, and you always learn it in the same way,
used world wide in every-days life, in mathematics and used by every programming language.
If you remember what you learned at school, you understand why the results in Java and C# are like they are:
Code: Select all
int i = 7;
System.out.println( i + 2 + " Result" ); // 9 Result
With the simple string concatenation system freak suggests, the PureBasic result would be:
Code: Select all
i = 7
PrintN( i + 2 + " Result" ) ; 72 Result
It is wrong, even every non-programmer should understand that. It is against all common sense
and against everything we learned.
Operators of same precedence are evaluated from left to right. 7 + 2 = 9.
Going a non-standard way for PureBasic in this case could be even more confusing... in my opinion.