|
| 1 | +# Wave Programming Language - Code Examples and Explanation |
| 2 | + |
| 3 | +This document provides a series of code examples written in the Wave programming language. Each example demonstrates a specific feature or concept in the language, accompanied by a detailed explanation. |
| 4 | + |
| 5 | +## Input/Output |
| 6 | +``` |
| 7 | +fun main() { |
| 8 | + print("Hello World?\n"); |
| 9 | + println("Hello World!"); |
| 10 | +} |
| 11 | +``` |
| 12 | +### Explanation: |
| 13 | +* `fun main()`: This defines the main function, which serves as the entry point for the program. |
| 14 | +In Wave, the `main` function is where the execution of the program begins, just like in C or Rust. |
| 15 | + |
| 16 | +* `print("Hello World?\n");`: The `print` function outputs the string `"Hello World?"` without appending a newline character at the end. |
| 17 | +The `\n` ensures the output is followed by a newline. |
| 18 | + |
| 19 | +* `println("Hello World!");`: The `println` function is used to print the string `"Hello World!"` to the console, followed by a newline character. |
| 20 | + |
| 21 | +* Output: |
| 22 | +``` |
| 23 | +Hello World? |
| 24 | +Hello World! |
| 25 | +``` |
| 26 | + |
| 27 | +This example demonstrates how to use `print` for output without a newline and `println` for output with a newline. |
| 28 | + |
| 29 | +## Variables and Conditionals |
| 30 | +``` |
| 31 | +fun main() { |
| 32 | + var x: i32 = 5; |
| 33 | + var y: i32 = 10; |
| 34 | + |
| 35 | + if (x < y) { |
| 36 | + println("x is less than y"); |
| 37 | + } else { |
| 38 | + println("x is greater than or equal to y"); |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | +### Explanation: |
| 43 | +* `var x: i32 = 5;` and `var y: i32 = 10;`: These lines declare two variables, `x` and `y`, of type `i32` (32-bit integer), |
| 44 | +and assign them initial values of 5 and 10, respectively. |
| 45 | + |
| 46 | +* `if (x < y) { ... } else { ... }`: This is and `if-else` conditional statement. |
| 47 | +It checks if the value of `x` is less than `y`. If the condition is true, it prints `x is less than y`. |
| 48 | +Otherwise, it prints `x is greater than or equal to y`. |
| 49 | + |
| 50 | +* Output: |
| 51 | +``` |
| 52 | +x is less than y |
| 53 | +``` |
| 54 | + |
| 55 | +This example demonstrates how to declare variables, compare them, and use conditional logic. |
| 56 | + |
| 57 | +## Loops |
| 58 | +### `while` Loop |
| 59 | +``` |
| 60 | +fun main() { |
| 61 | + var i: i32 = 0; |
| 62 | + |
| 63 | + while (i < 5) { |
| 64 | + println(i); |
| 65 | + i = i + 1; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +#### Explanation: |
| 71 | +* `var i: i32 = 0;`: This line initializes a variable `i` with the value 0. |
| 72 | + |
| 73 | +* `while (i < 5) { ... }`: The `while` loop continues executing the code block as long as the condition `i < 5` holds true. |
| 74 | +Inside the loop, the current value of `i` is printed, and `i` is incremented by 1. |
| 75 | + |
| 76 | +* Output: |
| 77 | +``` |
| 78 | +0 |
| 79 | +1 |
| 80 | +2 |
| 81 | +3 |
| 82 | +4 |
| 83 | +``` |
| 84 | + |
| 85 | +### `for` Loop |
| 86 | +``` |
| 87 | +fun main() { |
| 88 | + for (i in 0..4) { |
| 89 | + println(i); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +#### Explanation: |
| 95 | +* `for (i in 0..4) { ... }`: The `for` loop iterates over the range `0..4`, effectively printing the values of `i` from 0 to 4. |
| 96 | + |
| 97 | +* Output: |
| 98 | +``` |
| 99 | +0 |
| 100 | +1 |
| 101 | +2 |
| 102 | +3 |
| 103 | +4 |
| 104 | +``` |
| 105 | + |
| 106 | +This example demonstrates how the same task can be accomplished using a `for` loop, iterating through a specified range instead of manually managing the loop condition. |
| 107 | + |
| 108 | +## Functions |
| 109 | +``` |
| 110 | +fun greet(name: str) { |
| 111 | + println("Hello, {} !", name); |
| 112 | +} |
| 113 | +
|
| 114 | +fun main() { |
| 115 | + greet("Wave"); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Explanation: |
| 120 | +* `fun greet(name: str)`: This defines a function called `greet` that takes a single parameter `name` of type `str` (String). |
| 121 | + |
| 122 | +* `"println("Hello, {} !"), name"`: inside the function, we use a formatted string to print a greeting message that includes the value of `name`. |
| 123 | + |
| 124 | +* `greet("Wave")`: In the `main` function, we call the `greet` function with the argument `"Wave"`, which outputs the greeting message. |
| 125 | + |
| 126 | +* Output: |
| 127 | +``` |
| 128 | +Hello, Wave ! |
| 129 | +``` |
| 130 | + |
| 131 | +This example demonstrate how to define and call a function with parameters in Wave, along with string formatting. |
| 132 | + |
| 133 | +## Error Handling |
| 134 | +``` |
| 135 | +fun divide(a: i32, b: i32) -> i32 { |
| 136 | + if (b == 0) { |
| 137 | + println("Error: Divison by zero"); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + return a / b; |
| 141 | +} |
| 142 | +
|
| 143 | +fun main() { |
| 144 | + var result: i32 = divide(10, 2); |
| 145 | + |
| 146 | + if result != -1 { |
| 147 | + println("Result: {}", result); |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Explanation |
| 153 | + |
| 154 | +* `fun divide(a: i32, b: i32) -> i32`: This defines a function called `divide` that takes two integers as parameters and returns an integer result. |
| 155 | +* `if (b == 0) { ... }`: Inside the function, we check if `b` is zero. If it is, we print an error message and return `-1` to indicate an error. Otherwise, the function performs the division and returns the result. |
| 156 | +* `var result: i32 = divide(10, 2)`: In the `main` function, we call `divide` with the arguments `10` and `2`, storing the result in the variable `result`. |
| 157 | +* `if result != -1 { ... }`: We then check if the result is not equal to `-1` (indicating an error) and, if valid, print the result. |
| 158 | + |
| 159 | +* Output: |
| 160 | +``` |
| 161 | +Result: 5 |
| 162 | +``` |
| 163 | + |
| 164 | +This example demonstrates how to handle errors, such as division by zero, and return an error value. |
| 165 | + |
| 166 | +## Arrays |
| 167 | +``` |
| 168 | +fun main() { |
| 169 | + var arr = [1, 2, 3, 4, 5]; |
| 170 | + |
| 171 | + for (num in arr) { |
| 172 | + println(num); |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### Explanation: |
| 178 | + |
| 179 | +* `var arr = [1, 2, 3, 4, 5];`: This creates an array named `arr` containing the integers from 1 to 5. |
| 180 | + |
| 181 | +* `for (num in arr) { ... }`: The `for` loop iterates over each element in the array `arr`. In each iteration, the current element is stored in the variable `num`. |
| 182 | + |
| 183 | +* `println(num);`: Inside the loop, the current value of `num` is printed. |
| 184 | + |
| 185 | +* Output: |
| 186 | +``` |
| 187 | +1 |
| 188 | +2 |
| 189 | +3 |
| 190 | +4 |
| 191 | +5 |
| 192 | +``` |
| 193 | + |
| 194 | +This example demonstrates how to declare an array and iterate through its elements using a `for` loop. |
0 commit comments