# loops

```scala
for( var x <- Range ){
   statement(s);
}

 for( var a <- 1 to 10){
     println( "Value of a: " + a );
 }
```

## Ranges

```scala
i to j // includes j
i until j //excludes j
```

Can use multiple ranges separate by `;` and will iterator over al combos of both

```scala
for( a <- 1 to 3; b <- 1 to 3){
    println( "Value of a: " + a );
    println( "Value of b: " + b );
}
```

(1, 1) -> (1, 2) -> (1, 3) -> (2, 1) -> ...

## Containers

Can also be containers

```scala
val numList = List(1,2,3,4,5,6);

// for loop execution with a collection
for( a <- numList ){
    println( "Value of a: " + a );
}
```

## With Filters/Guards

can filter out with if in for loop

```scala
for( a <- numList
    if a != 3; if a < 8 ){
    println( "Value of a: " + a );
}
```

## With Yield

Creates a new data structue from an existing one

```scala
val names2 = for (e <- names) yield e.capitalize
val lengths = for (e <- names) yield {
    // imagine that this required multiple lines of code
    e.length
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/all/scala/loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
