📄 language-full.html
字号:
@[] can be used with strings</td>
</tr>
</table>
<b>
<p>Notes:</b> No shortcut logic is performed on the operands of || and &&<br>
The compound assignment operators (+=, *=, etc.) are not supported.<br>
The comma and conditional operators (?:) are not supported.<br>
</p>
<h3>String Character Accessor</h3>
<p>To get or set an individual character within a string variable, use <i>stringVariable</i>@[<i>index</i>].
The index of the first character is 0. You will produce a runtime error if you
attempt to access a character that is past the end of the string. Example:</p>
<pre>string str = "bob";
...
puts(str@[1]); // Prints the second letter of str
str@[1] = 'X'; // changes str from "bob" to "bXb"</pre>
<p><b>Note:</b> the string character accessor cannot be used with pointers, nor
can the address of the resulting string be take. In other words, the following
expressions are not valid: &str@[i], *pstr@[i], (*pstr)@[i]</p>
<h3>Increment / Decrement</h3>
<p>The ++ and -- operators are special in that they must be placed before or after a
variable and modify the value of the variable. The ++ increments the value of a variable
by one, while the -- decrements by one. The caveat is that if the ++/-- is placed in front
of the variable, the expression evaluates to the value of the variable after it is
incremented/decremented. If it is placed after the variable, the expression evaluates to
the variable's previous value. Example: </p>
<pre>int myInt;
...
myInt = 8;
puts(++myInt); // Prints "9" to the output form
myInt = 8;
puts(myInt++); // Prints "8" to the output form, but myInt is now 9</pre>
<h3>Automatic Conversion and Casting</h3>
<p>Just like in assignments statements, automatic conversion takes place in every part of
an expression. If the two arguments to an operator are of different types, one of
arguments will be promoted to the less strict type. The promotion order is char to int to
float to string. So in the expression: </p>
<pre>"Result is: " + 5;</pre>
<p>The constant 5 is first promoted to a string, and the two strings are concatenated.
This may have some undesirable side effects. For example, if you want to write an
expression and result to the output form, you might do something like this: </p>
<pre>puts("5 + 7 = " + 5 + 7); // Prints "5 + 7 = 57"</pre>
<p>This probably wasn't the desired outcome. Instead, you would want the expression
evaluated first, then concatenated to the string. The parentheses can be used to
accomplish this: </p>
<pre>puts("5 + 7 = " + (5 + 7)); // Prints "5 + 7 = 12"</pre>
<p>One problem remains. Suppose you want to find the floating point value of a fraction of
two integer. </p>
<pre>puts("7 / 5 = " + (7 / 5)); // Prints "7 / 5 = 1"</pre>
<p>This output is because both arguments are integers, so the result is also an integer.
To solve this, we can cast one of them to a float: </p>
<pre>puts("7 / 5 = " + ((float)7 / 5)); // Prints " 7 / 5 = 1.4"</pre>
<p>This forces the integer 7 to a floating point number before dividing it by 5.</p>
<h3>Statements</h3>
<p>Statements are the individual parts that make up the body of a function. The following
are the available statements: </p>
<table border="1">
<tr>
<th>Statement</th>
<th>Description</th>
</tr>
<tr>
<td><code>return;</code></td>
<td>Returns immediately from the current function (with a default return value of integer
0)</td>
</tr>
<tr>
<td><code>return <i>expr</i>;</code></td>
<td>Returns immediately from the current function, returning the value of the expression <i>expr</i></td>
</tr>
<tr>
<td><code>if (<i>expr</i>) <i>stmt</i></code></td>
<td>Evaluates the expression <i>expr</i>, if its result is true (non-zero or non-empty
string), the statement <i>stmt</i> is executed, otherwise <i>stmt</i> is skipped, and
execution continues</td>
</tr>
<tr>
<td><code>if (<i>expr</i>) <i>stmtA</i><br>
else <i>stmtB</i></code></td>
<td>Evaluates the expression <i>expr</i>, if its result is true (non-zero or non-empty
string), the statement <i>stmtA</i> is executed, otherwise <i>stmtB</i> is executed</td>
</tr>
<tr>
<td><code>while (<i>expr</i>) <i>stmt</i></code></td>
<td>The expression <i>expr</i> is evaluated. If it is true (non-zero or non-empty string),
<i>stmt</i> is executed. The loop then begin again, evaluating <i>expr</i> and executing <i>stmt</i>
until <i>expr</i> is no longer true. This means that <i>stmt</i> will never execute if <i>expr</i>
is initially false</td>
</tr>
<tr>
<td><code>do <i>stmt</i><br>
while (<i>expr</i>)</code></td>
<td>The same as <code>while</code> except that the statement <i>stmt</i> is executed
before <i>expr</i> is evaluated. This guarantees that <i>stmt</i> will execute at least
once</td>
</tr>
<tr>
<td><code>for (<i>init</i>;<i>cond</i>;<i>iter</i>)<br>
<i>stmt</i></code></td>
<td>The initializer expression <i>init</i> is first evaluated. The condition expression <i>cond</i>
is evaluated. If it is true, <i>stmt</i> is executed and the iterator expression <i>iter</i>
is evaluated continuing the loop, otherwise the the <code>for</code> loop ends. Note: <i>init</i>
is evaluated only once.</td>
</tr>
<tr>
<td><code>break;</code></td>
<td>Immediately exits from the directly enclosing <code>while/do/for</code>
loop or <code>switch</code> statement..</td>
</tr>
<tr>
<td><code>continue;</code></td>
<td>Immediately restarts the directly enclosing <code>while/do/for</code> loop. In a <code>for</code>
loop, the <i>iter</i> expression is evaluated, followed by the <i>cond</i> expression and
possibly the <i>stmt</i></td>
</tr>
<tr>
<td><code>switch (<i>expr</i>)<br>
{ stmts }</code></td>
<td>Evaluates the expression <i>expr</i>. If <i>stmts</i> contains a <code>case</code>
statement with a matching value, the code immediately following the <code>case</code>
statement is executed until either a <code>break</code> statement or the end of the
<code>switch</code> statement is reached. If no matching <code>case</code> statement is found and
a <code>default</code> statement exists in the <code>switch</code>, the code immediately following the
<code>default</code> statement is executed until either a <code>break</code> statement or the end of
the <code>switch</code> statement is reached. If no case statement matches the <i>expr</i>,
and no <code>default</code> statement is present, everything in the <code>switch</code> statement is
skipped, and execution continues after the final closing brace. <i>expr</i>
must not evaluate to a float value.</td>
</tr>
<tr>
<td><code>case <i>constant</i>:</code></td>
<td>
<p align="left">A marker within a <code>switch</code> statement. The <i>constant</i>
must be either a <code>char</code> (<code>case 'a':</code>), an <code>int</code>
(<code>case 3:</code>), or a <code>string</code> (<code>case "apple":</code>)
If the <i>constant</i> matches the <i>expr</i> in the
<code>switch</code> statement, then the code immediately following this marker is run,
until a <code>break</code> statement or the end of the <code>switch</code> statement is reached.</p>
</td>
</tr>
<tr>
<td><code>default:</code></td>
<td>An optional marker within a <code>switch</code> statement. If none of the <code>case</code>s in
the <code>switch</code> statement match the <code>switch</code> <i>expr</i>, the code immediately
following this marker is executed, until a <code>break</code> statement or the end of
the <code>switch</code> statement is reached.</td>
</tr>
<tr>
<td><code>{ <i>statements</i> }</code></td>
<td>A brace followed by a list of statements, followed by another brace is considered a
single statement</td>
</tr>
<tr>
<td><code><i>expression</i>;</code></td>
<td>An expression followed by a semicolon is also considered to be a statement</td>
</tr>
</table>
<h3><br>
Statement Examples</h3>
<b>
<p>return</b><br>
Let's visit a previous example function to see how return works. </p>
<pre>five() {
return 5;
}</pre>
<p>Since the return value of the function <i>five</i> is always 5, we can use the function
any place we would normal put the constant 5. </p>
<pre>puts("Five is " + five()); // Prints "Five is 5"</pre>
<p>Also, since <code>return</code> causes the function to exit immediately, we could do
this: </p>
<pre>five() {
return 5;
puts("This won't print");
}</pre>
<p>and we would have the same effect.</p>
<p><b>if</b><br>
</p>
<pre>lessThan5(int x) {
if (x < 5)
puts("Less than five");
puts("Hello");
}</pre>
<p>If this function is called with a number less than 5, "Less than five" will
be printed followed by the word "Hello", otherwise, only the word
"Hello" is printed.</p>
<p><b>if ... else</b><br>
</p>
<pre>lessThan5(int x) {
if (x < 5)
puts("Less than five");
else
puts("Greater than or equal to five");
}</pre>
<p>If this function is called with a number less than 5, "Less than five" is
printed, otherwise "Greater than or equal to five" is printed.</p>
<p><b>while</b> </p>
<pre>count() {
int x;
x = 5;
while (x > 0) {
puts(x);
x = x - 1;
}
}</pre>
<p>This bit of code will print the numbers from 5 to 1 counting backwards. Notice that
braces were placed around the two lines of code in the <code>while</code> loop to make
them act as a single statement.</p>
<p><b>do ... while</b> </p>
<pre>count() {
int x;
x = 6;
do {
x = x - 1; // could also be x--
puts(x);
} while (x > 0);
}</pre>
<p>This bit of code (similar to the previous example) will print the numbers from 5 to <b>0</b>
counting backwards. The zero is printed in this case because the expression <code>x < 0</code>
is not evaluated until after the loop</p>
<p><b>for</b> </p>
<pre>output() {
string list[4];
int index;
list[0] = "Zero";
list[1] = "One";
list[2] = "Two";
list[3] = "Three";
for (index = 0 ; index < 4 ; index++)
puts(list[index]);
}</pre>
<p>This example will print out "ZeroOneTwoThree". When we disect it we see that
the array <i>list</i> is initialized first. We then reach the <code>for</code> loop.
First, the initializer is evaluated, setting index to 0. Next, the condition is evaluated <code>index
< 4</code>, which is true, so the body of the loop executes, printing "Zero".
The iterator expression is then evaluated, increasing <i>index</i> by one. This continues
until <i>index</i> is equal to 4, at which point the loop exits without executing the body
again.</p>
<p><b>break</b> </p>
<pre>count() {
int x;
x = 5;
while (x > 0) {
if (x == 1)
break;
puts(x);
x = x - 1;
}
}</pre>
<p>In this slightly more complex piece of code, the counting goes on as it normally would,
printing out "5432". However, when <i>x</i> reaches 1, <code>break</code> is
executed, breaking out of the <code>while</code> loop early, before the 1 gets printed.</p>
<p><b>continue</b> </p>
<pre>count() {
int x;
x = 6;
while (x > 1) {
x--; // Do the subtraction first
if (x == 3)
continue;
puts(x);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -