-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBeanShell02.bsh
More file actions
85 lines (75 loc) · 3.01 KB
/
Copy pathBeanShell02.bsh
File metadata and controls
85 lines (75 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* BeanShell02.bsh
* IJ BAR: https://github.com/tferr/Scripts#scripts
* *************************************************
* 2. If statements, for loops and switch statements
* *************************************************
*/
a = 10; b = 20; // Semicolon separating in-line statements
// An if statement:
if (a == b) {
print("Line 13: " + "a is b!");
} else if (a < b) {
print("Line 15: " + "a is smaller than b!");
} else if (a<b) {
print("Line 17: " + "a is greater than b!");
} else {
print("Line 19: "
+ "This is never evaluated because previous conditions"
+ "cover all posiibilities");
}
// A for loop is used to iterate over collections (lists) and arrays.
// So we'll have a look at arrays before looking into for loops.
// Arrays are fixed-size collections of elements of the same type
// (e.g., an array holding strings can not hold integers). So we need
// to specify the array data type when declaring one. This is how we
// define arrays with pre-filled values:
int[] intArray = { 1, 2, 3 };
String[] stringArray = {"One", "Two", "Three"};
// We can also declare an empty array and populate it later on
intArray = new int[3]; // Integer in square brackets specifies array size
// To populate it, we use the assignment operator (=) to fill one item
// at a time. As mentioned before, arrays are indexed sequences of
// items. Java (and thus, BeanShell) uses zero-based arrays: The 1st
// index (item position) is zero, the 2nd index is one, etc. This is
// how we would fill the array "manually":
intArray[0] = 1; // Assign "1" to indexed position 0 (1st position)
intArray[1] = 2; // Assign "2" to indexed position 1 (2nd position)
intArray[2] = 3; // etc.
// This works fine unless the array is large. In such situations, we
// can use a "for loop" that "fills" the array by iterating through
// the array positions (indices). We use <length> (a property of
// array objects) to determine the size of the array (how many
// positions it has):
intArray = new int[3];
print("Line 51: "+ "Contents of intArray:");
for (i=0; i<intArray.length; i++) {
intArray[i] = i + 1;
print(" Line 54 (for loop): intArray["+ i + "] = " + intArray[i]);
}
// We can also loop through arrays using what is called an "enhanced
// for loop". Here is an example:
print("Line 59: "+ "Contents of intArray:");
for (item : intArray) {
print(" Line 61 (enhanced for loop): "+ item);
}
// Switch statements are a special kind of if-else statements with
// multiple execution paths. Here is an example:
cocktail = "cuba libre";
switch (cocktail) {
case "margarita":
print("Line 70: cocktail has tequilla");
break;
case "martini":
case "gin and tonic":
print("Line 74: cocktail has gin");
break;
case "cuba libre":
print("Line 77: cocktail has rum");
break;
default: // Any value not caught_above
print("Line 80: I'm not aware of the specified cocktail");
}
// BTW, While Java switch statements are restricted to certain data
// types (an integer, a character, or a string) in BeanShell switch
// statements apply to any object. Neat hm?