You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1303,7 +1386,47 @@ Multiple commands can be separated by a colon `:`. If instead of a command a
1303
1386
number is specified, it is equivalent to a GOTO command with the specified
1304
1387
numeric-label.
1305
1388
1389
+
### Inline Version of IF {#InlineVersionOfIf}
1390
+
1391
+
```smallbasic
1392
+
result = IFF (condition, return_value_true, return_value_false)
1393
+
```
1394
+
1395
+
The command `IFF` will test the condition `condition`. If `condition` resolves to
1396
+
`true` then `return_value_true` will be returned otherwise `return_value_false`.
1397
+
1398
+
```smallbasic
1399
+
x = 4
1400
+
ans = IFF(x <= 5, 0, 10)
1401
+
PRINT ans ' Output: 0
1402
+
```
1403
+
1404
+
See function reference [IFF](https://smallbasic.github.io/reference/638.html) for
1405
+
more information.
1406
+
1407
+
### SELECT CASE {#SelectCase}
1408
+
1409
+
```smallbasic
1410
+
SELECT CASE expr
1411
+
CASE result1
1412
+
' do thinks
1413
+
CASE result2
1414
+
' do thinks
1415
+
CASE ELSE
1416
+
' do thinks
1417
+
END SELECT
1418
+
```
1419
+
1420
+
`SELECT CASE` offers a more concise syntax to writing successive IF tests. `SELECT CASE`
1421
+
performs multiple tests on the expression `expr`. If the value of `expr` is equal to
1422
+
`result1`, the case statement `result1` will be entered and the commands executed. An
1423
+
unlimited amount of case statements can be used. Once a case statement is fulfilled
1424
+
the select-case structure will be exited and all following case statements will not be
1425
+
tested anymore. If non of the case statements were entered the optional 'CASE ELSE'
1426
+
statements will be entered.
1306
1427
1428
+
See function reference [SELECT CASE](https://smallbasic.github.io/reference/655.html) for
1429
+
detailed information.
1307
1430
1308
1431
## Units {#Units}
1309
1432
@@ -1425,66 +1548,64 @@ SmallBASIC uses the following meta commands:
1425
1548
#unit-path: C:\sbasic\units;C:\temp
1426
1549
```
1427
1550
1551
+
### The USE Keyword {#TheUseKeyword}
1428
1552
1553
+
The `USE` keyword is used on specific commands for passing a user-defined expression.
1429
1554
1430
-
### The USE keyword {#use}
1431
-
1432
-
This keyword is used on specific commands to passing a user-defined expression.
1433
-
1434
-
```
1555
+
```smallbasic
1435
1556
SPLIT s," ",v USE TRIM(x)
1436
1557
```
1437
1558
1438
-
In that example, every element of V() will be 'trimmed'.
1439
-
Use the x variable to specify the parameter of the expression. If the expression needs more parameter, you can use also the names y and z
1559
+
In this example, every element of `v` will be trimmed. Use the `x` variable to
1560
+
specify the parameter of the expression. If the expression needs more parameter,
1561
+
you can use also the names `y` and `z`.
1440
1562
1441
-
### The DO keyword {#do}
1563
+
##Exception Handling {#ExceptionHandling}
1442
1564
1443
-
This keyword is used to declare single-line commands. It can be used with WHILE and FOR-family commands.
1565
+
Exception handling is supported for file handling, and accessing serial ports and
1566
+
network sockets. Exception handling is typically used with errors raised when
1567
+
calling a file system command that cannot be completed, for example attempting to
1568
+
open a non-existent file.
1444
1569
1570
+
```smallbasic
1571
+
TRY
1572
+
' do something
1573
+
CATCH err
1574
+
print err
1575
+
' do something
1576
+
END TRY
1445
1577
```
1446
-
FOR f IN files("*.txt") DO PRINT f
1447
-
...
1448
-
WHILE i < 4 DO i ++
1449
-
```
1450
-
1451
-
Also, it can be used by IF command (instead of THEN), but is not suggested.
1452
1578
1453
-
1454
-
1455
-
### Loops and variables {#Loops}
1456
-
1457
-
When we write loops it is much better to initialize the counters on the top of the loop instead of the top of the program or nowhere.
1458
-
1459
-
```
1460
-
i = 0
1461
-
REPEAT
1462
-
...
1463
-
i = i + 1
1464
-
UNTIL i > 10
1579
+
```smallbasic
1580
+
TRY
1581
+
' do something
1582
+
CATCH "Error 1"
1583
+
' do something
1584
+
CATCH "Error 2"
1585
+
' do something
1586
+
END TRY
1465
1587
```
1466
1588
1467
-
p.. Initializing variables at the top of the loop can make code more readable.
1589
+
The `TRY` statement introduces a try/catch block. A try/catch block consist of the
1590
+
following structure:
1468
1591
1469
-
## Loops and expressions
1592
+
`TRY`
1470
1593
1471
-
```
1472
-
FOR-like (loops) commands evaluate both the "destination" and the exit-expression every time.
1473
-
FOR i=0 TO LEN(FILES("*.txt"))-1
1474
-
PRINT i
1475
-
NEXT
1476
-
```
1594
+
The `TRY` statement starts a block of commands which might create a run-time error.
1477
1595
1478
-
In that example the 'destination' is the LEN(FILES("*.txt"))-1 For each value of i the destination will be evaluated. That is WRONG but it is supported by BASIC and many other languages.
1479
-
So, it is much better to be rewritten as
1596
+
`CATCH [var | expr]`
1480
1597
1481
-
```
1482
-
idest=LEN(FILES("*.txt"))-1
1483
-
FOR i=0 TO idest
1484
-
PRINT i
1485
-
NEXT
1486
-
```
1598
+
The `CATCH` statement is used to catch a run-time error of one of the commands in
1599
+
the try-block.
1600
+
1601
+
The `CATCH` statement has two modes. You can supply a variable argument to store the
1602
+
error string. Alternatively you can supply a string expression. When the raised error
1603
+
matches the string expression, the error will be caught. When using the expression
1604
+
mode, you can supply a succession of CATCH statements to handle various error messages
1605
+
separately.
1487
1606
1488
-
Of course, it is much faster too.
1607
+
`END TRY`
1489
1608
1609
+
The `END TRY` statement marks the end of a try/catch block.
1490
1610
1611
+
For examples see the language reference [TRY](https://smallbasic.github.io/reference/1425.html).
0 commit comments