Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ set(SOURCES
src/AST/IfStatementNode.cpp
src/AST/WhileStatementNode.cpp
src/AST/BreakNode.cpp
src/AST/ContinueNode.cpp
src/AST/ComparisonNode.cpp
src/AST/LogicalNode.cpp
src/AST/UnaryNode.cpp
Expand Down Expand Up @@ -161,6 +162,7 @@ set(HEADERS
src/AST/IfStatementNode.hpp
src/AST/WhileStatementNode.hpp
src/AST/BreakNode.hpp
src/AST/ContinueNode.hpp
src/AST/ComparisonNode.hpp
src/AST/LogicalNode.hpp
src/AST/UnaryNode.hpp
Expand Down
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- **🔄 Regular Expressions**: Complete `regexp` library with 12 methods for pattern matching, text searching, and string manipulation
- **🔀 Control Flow Enhancement**: `else if` syntax support for complex conditional logic with proper AST chaining
- **🛑 Break Statements**: `break` statement for early exit from while loops, enabling search patterns and safety limits
- **⏭️ Continue Statements**: `continue` statement for skipping iterations in while loops, perfect for filtering and validation
- **📏 Text Length Method**: `Text.length()` method for string length calculation with full type integration
- **📐 Math Library**: Comprehensive mathematical functions library with 40+ methods including trigonometry, logarithms, statistics, and constants (`import math`)
- **✨ Comprehensive Text Methods**: 48 string manipulation methods including case conversion, search, validation, formatting, and advanced utilities
Expand Down Expand Up @@ -801,6 +802,79 @@ Object BreakDemo {
- ✅ **Nested Loops**: Only exits the immediate while loop (not outer loops)
- ✅ **Iterator Compatible**: Works seamlessly with all iterator types

### **Continue Statements in While Loops**

The `continue` statement skips the rest of the current iteration and jumps to the next iteration of the while loop, enabling efficient filtering, validation, and conditional processing patterns.

```obq
import system.io

Object ContinueDemo {
@external method basicContinueExample(): Int {
# Skip even numbers, process only odd numbers
sum: Int = 0
i: Int = 0

while (i < 10) {
i = i + 1
remainder: Int = i % 2
if (remainder == 0) {
continue # Skip even numbers
}
sum = sum + i
}
return sum # Returns 25 (1+3+5+7+9)
}

@external method filteringExample(): Int {
# Filter list, skip negative numbers
numbers: List<Int> = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]
positive_sum: Int = 0

iter: ListIterator = numbers.iterator()
while (iter.hasNext()) {
value: Int = iter.next()

if (value < 0) {
continue # Skip negative numbers
}

positive_sum = positive_sum + value
}
return positive_sum # Returns 25 (1+3+5+7+9)
}

@external method validationExample(): Int {
# Process data with validation, skip invalid entries
data: List<Int> = [0, 15, -3, 8, 25, -1, 12, 30, 7, -5]
valid_count: Int = 0

iter: ListIterator = data.iterator()
while (iter.hasNext()) {
value: Int = iter.next()

# Skip values outside valid range (1-20)
if (value < 1) {
continue
}
if (value > 20) {
continue
}

valid_count = valid_count + 1
}
return valid_count # Returns 4 (15, 8, 12, 7)
}
}
```

**Key Continue Statement Features:**
- ✅ **Skip Current Iteration**: Jumps to next loop iteration immediately
- ✅ **Filtering Patterns**: Perfect for processing subsets of data
- ✅ **Validation Logic**: Skip invalid data while continuing processing
- ✅ **Condition Chains**: Multiple continue conditions for complex filtering
- ✅ **Combined with Break**: Use together for sophisticated control flow

### **Generic Sets (Set<T>) with Iterators**

```obq
Expand Down Expand Up @@ -3390,6 +3464,7 @@ try {
| `if`, `else if`, `else` | Conditional logic with chaining |
| `while` | Control flow loop |
| `break` | Exit from while loop |
| `continue` | Skip to next iteration of while loop |
| `throw` | Throw user-defined errors |
| `try`, `catch`, `finally` | Structured exception handling |
| `Result`, `Error` | Error handling types |
Expand Down Expand Up @@ -3913,6 +3988,7 @@ Immutable objects eliminate entire categories of bugs:
- ✅ **Comprehensive Type System**: Int, Long, Float, Double with automatic promotion
- ✅ **While Loops**: Practical iteration with ListIterator and RepeatIterator support
- ✅ **Break Statements**: Early exit from while loops for search patterns and safety limits
- ✅ **Continue Statements**: Skip iterations in while loops for filtering and validation patterns
- ✅ **Modulo Operator**: Full arithmetic completeness with `%` operator for all numeric types
- ✅ **System Utilities**: `system.utils` module with RepeatIterator for controlled repetition
- ✅ **Error Handling System**: Complete `throw`/`try`/`catch`/`finally` with `Result<T,E>` and `Error` types
Expand Down
226 changes: 226 additions & 0 deletions examples/test_continue_examples.obq
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import system.io

Object ContinueExamples {
@external method basicContinueExample(): Int {
io.print("=== Basic Continue Example ===")
io.print("Processing numbers 1-10, skipping even numbers")

sum: Int = 0
i: Int = 0

while (i < 10) {
i = i + 1

remainder: Int = i % 2
if (remainder == 0) {
io.print("Skipping even number: %d", i)
continue
}

io.print("Processing odd number: %d", i)
sum = sum + i
}

io.print("Sum of odd numbers: %d", sum)
return sum
}

@external method filteringExample(): Int {
io.print("=== Filtering Example ===")
io.print("Processing list, skipping negative numbers")

numbers: List<Int> = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10]
positive_sum: Int = 0
processed_count: Int = 0

io.print("Numbers: %s", numbers)

iter: ListIterator = numbers.iterator()
while (iter.hasNext()) {
value: Int = iter.next()

if (value < 0) {
io.print("Skipping negative number: %d", value)
continue
}

io.print("Processing positive number: %d", value)
positive_sum = positive_sum + value
processed_count = processed_count + 1
}

io.print("Processed %d positive numbers", processed_count)
io.print("Sum of positive numbers: %d", positive_sum)
return positive_sum
}

@external method validationExample(): Int {
io.print("=== Validation Example ===")
io.print("Processing data, skipping invalid entries")

data: List<Int> = [0, 15, -3, 8, 25, -1, 12, 30, 7, -5]
valid_count: Int = 0
valid_sum: Int = 0

io.print("Data: %s", data)
io.print("Valid range: 1-20")

iter: ListIterator = data.iterator()
while (iter.hasNext()) {
value: Int = iter.next()

# Skip values outside valid range (1-20)
if (value < 1) {
io.print("Skipping invalid (too low): %d", value)
continue
}

if (value > 20) {
io.print("Skipping invalid (too high): %d", value)
continue
}

io.print("Processing valid value: %d", value)
valid_sum = valid_sum + value
valid_count = valid_count + 1
}

io.print("Found %d valid entries", valid_count)
io.print("Sum of valid values: %d", valid_sum)
return valid_count
}

@external method searchWithContinueExample(): Int {
io.print("=== Search with Continue Example ===")
io.print("Finding multiples of 3 in range 1-30")

multiples: List<Int> = []
i: Int = 0

while (i < 30) {
i = i + 1

remainder: Int = i % 3
if (remainder != 0) {
continue # Skip numbers that are not multiples of 3
}

# Also skip multiples that are greater than 20
if (i > 20) {
io.print("Skipping large multiple: %d", i)
continue
}

io.print("Found multiple of 3: %d", i)
multiples.add(i)
}

io.print("Multiples of 3 (1-20): %s", multiples)
return multiples.size()
}

@external method batchProcessingExample(): Int {
io.print("=== Batch Processing Example ===")
io.print("Processing batches, skipping empty ones")

batch_sizes: List<Int> = [5, 0, 3, 0, 8, 2, 0, 6, 1, 0]
total_processed: Int = 0
batch_count: Int = 0

io.print("Batch sizes: %s", batch_sizes)

iter: ListIterator = batch_sizes.iterator()
while (iter.hasNext()) {
batch_size: Int = iter.next()
batch_count = batch_count + 1

if (batch_size == 0) {
io.print("Batch %d: Empty, skipping", batch_count)
continue
}

io.print("Batch %d: Processing %d items", batch_count, batch_size)
total_processed = total_processed + batch_size
}

io.print("Total items processed: %d", total_processed)
return total_processed
}

@external method continueWithBreakExample(): Int {
io.print("=== Continue with Break Example ===")
io.print("Processing until error threshold, skipping warnings")

error_codes: List<Int> = [0, 1, 0, 2, 1, 0, 3, 1, 2, 5]
processed: Int = 0
error_count: Int = 0
max_errors: Int = 3

io.print("Error codes: %s", error_codes)
io.print("0=Success, 1=Warning, 2=Error, 3=Error, 5=Critical")
io.print("Max errors allowed: %d", max_errors)

iter: ListIterator = error_codes.iterator()
while (iter.hasNext()) {
code: Int = iter.next()
processed = processed + 1

if (code == 0) {
io.print("Item %d: Success", processed)
continue # Skip successful items
}

if (code == 1) {
io.print("Item %d: Warning (ignored)", processed)
continue # Skip warnings
}

# Count errors (code >= 2)
error_count = error_count + 1
io.print("Item %d: Error (code %d), total errors: %d", processed, code, error_count)

if (error_count >= max_errors) {
io.print("Error threshold reached, stopping processing")
break
}
}

io.print("Processed %d items with %d errors", processed, error_count)
return processed
}

@external method runAllExamples(): Int {
io.print("Continue Statement Examples")
io.print("===========================")
io.print("")

this.basicContinueExample()
io.print("")

this.filteringExample()
io.print("")

this.validationExample()
io.print("")

this.searchWithContinueExample()
io.print("")

this.batchProcessingExample()
io.print("")

this.continueWithBreakExample()

io.print("")
io.print("All continue statement examples completed!")

return 0
}
}

Object Main {
method main(): Int {
examples: ContinueExamples = new ContinueExamples()
return examples.runAllExamples()
}
}
31 changes: 31 additions & 0 deletions src/AST/ContinueNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 O²L Programming Language
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ContinueNode.hpp"

#include "../Common/Exceptions.hpp"

namespace o2l {

Value ContinueNode::evaluate(Context& context) {

Check warning on line 23 in src/AST/ContinueNode.cpp

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, Debug)

unused parameter ‘context’ [-Wunused-parameter]
throw ContinueException();
}

std::string ContinueNode::toString() const {
return "Continue()";
}

} // namespace o2l
Loading
Loading