Skip to content

Expressions

Positron edited this page Mar 4, 2015 · 7 revisions

The assignment operation now produces a result. The result is the value being assigned. This way, you can chain together multiple assignments or use an assignment in a condition.

script 1 open {
   int a, b, c;
   a = b = c = 123; // a, b, and c now have the value 123.
   // First a random number is generated. Then the random number is assigned to
   // variable `a`. The result of the assignment, which is the random number,
   // is then checked if it's not 3.
   while ( ( a = random( 0, 10 ) ) != 3 ) {
      Print( s: "Bad number: ", i: a );
   }
}

--

There are two functions that are associated with the str type: at() and length(). These functions can only be called on a value or a variable of str type. at() returns the character found at the specified index, and length() returns the length of the string.

script 1 open {
   Print( c: "Hello, World!".at( 7 ) );  // Output: W
   Print( i: "Hello, World!".length() ); // Output: 13
}

Logical AND/OR

Note: This section needs redoing.

In bcc, the logical AND (&&) and OR (||) operators exhibit short-circuit evaluation.

When using the logical AND operator, the left side is evaluated first. If the result is 0, the right side is SKIPPED, and the result of the operation is 0. Otherwise, the right side is then evaluated, and, like the left side, if the result is 0, the result of the operation is 0. Otherwise, the result of the operation is 1.

When using the logical OR operator, the left side is evaluated first. If the result is NOT 0, the right side is SKIPPED, and the result of the operation is 1. Otherwise, the right side is evaluated, and if the result is not 0, the result of the operation is 1. Otherwise, the result of the operation is 0.

Code:
``` function int get_0( void ) { print( s: "called get_0()" ); return 0; }

function int get_1( void ) { print( s: "called get_1()" ); return 1; }

script 1 open { print( s: "get_0() && get_1() == ", i: get_0() && get_1() ); print( s: "get_1() && get_0() == ", i: get_1() && get_0() ); print( s: "get_0() || get_1() == ", i: get_0() || get_1() ); print( s: "get_1() || get_0() == ", i: get_1() || get_0() ); }


<h6>Output:</h6>
<pre>
  called get_0()  
get_0() && get_1() == 0  
  called get_1()  
  called get_0()  
get_1() && get_0() == 0  
  called get_0()  
  called get_1()  
get_0() || get_1() == 1  
  called get_1()  
get_1() || get_0() == 1  
</pre>

Notice in the first expression, when the left side is 0, get_1() is not called. Similarly, in the final expression, when the left side of the expression is 1, get_0() is not called.

In simpler words: In the following discussion, _false_ is the value 0 and _true_ is any other value. When using the logical AND operator, you'll get 1 only if both sides are true. If the left side is false, the right side is skipped because the condition to get 1 won't be met. When using the logical OR operator, you'll get 1 as long as one of the sides is true. If the left side is true, there is no need to evaluate the right side, because the condition is already met.
Clone this wiki locally