Skip to content

Expressions

Positron edited this page Jun 27, 2016 · 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
}

--

The [] operator can be used on a string. It returns the specified character of the string. It is just syntactic sugar for the GetChar() function.

script "test" open {
   Print( c: "abc"[ 0 ] ); // Output: a
   Print( c: "abc"[ 2 ] ); // Output: c
}

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.

## Conditional Operator

##### Syntax
<pre>
<i>left-operand</i> <b>?</b> <i>middle-operand</i> <b>:</b> <i>right-operand</i>
<i>left-operand</i> <b>?:</b> <i>right-operand</i>
</pre>

When using the first form of the conditional operator, the left operand is evaluated first. If the result is non-zero, then the middle operand is evaluated and returned. Otherwise, the right operand is evaluated and returned.

script 1 open { Print( s: 1 ? "True!" : "False!" ); // Output: True! Print( s: 0 ? "True!" : "False!" ); // Output: False! }


The middle operand of the conditional operator is optional. In this second form, the left operand is evaluated first. If the result is non-zero, it is returned. Otherwise, the right operand is evaluated and returned.

script 1 open { Print( i: 123 ?: 321 ); // Output: 123 Print( i: 0 ?: 321 ); // Output: 321 }


__Note:__ At this time, the left operand cannot be of `str` type.
Clone this wiki locally