# 2-A: Selection and Iteration

## Conditions

A fundamental element of flow control is whether or not you want to execute a block of code. We use a conditional or **if statement** to test something. That test will result in a `true` or `false` value. If it's true, we'll execute the block of code. If it's false, we'll skip to the end of the block of code.&#x20;

<figure><img src="https://1916862645-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHmXRQJbjMi37frjOn8%2Fuploads%2Fzn0qxv4IWJWVPtIc0B88%2Fimage.png?alt=media&#x26;token=db913250-668c-4da9-8502-189eaccb4ee4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1916862645-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LHmXRQJbjMi37frjOn8%2Fuploads%2FsmlIVYk8GVKCLmiMaea1%2Fimage.png?alt=media&#x26;token=6ab4205c-2613-4327-9992-f191821d8ca9" alt=""><figcaption></figcaption></figure>

## Loops

Loops are how we repeat commands or loop through items in a collection.&#x20;

### for loops

#### standard for loop

Most for loops look something like this:

```java
for(int x = 0; x < 20; x++) {
    // code goes in here
}
```

The for statement has a **declaration, condition** and **increment**. But you can skip the declaration if you'd like. Let's say you want the counter to have a wider scope (so it can be accessed outside of the loop) like this:

```java
int x = 0;  // declared outside the loop so it can remain afterwards
for(; x < 20; x++){
    // code goes here for the loop
}
System.out.println(x);  // will print 20
```

{% embed url="<http://youtu.be/Q4j9EscKkto?hd=1>" %}

#### foreach loop

Let's say I have an array or collection of numbers like this: `int[] numberArray = [5, 10, 15, 20, 25];`&#x20;

Each number in that group has an address or **index** (that starts at 0). If I wanted to print the number 10 for example, I could write `System.out.println(numberArray[1]);`

So if I wanted to loop through (called iterating or traversing) all the items in this array, I could use a normal for loop like this:

```java
for (int x = 0; x < numberArray.length; x++){
    System.out.println(numberArray[x]);
}
```

But there's an easier form of the for loop that's meant to easily traverse through a collection. We'll go over some of the pros and cons of this method later.&#x20;

```java
for (int x : numberArray){
    // x isn't looping through the addresses this time
    // rather, it's pretending to be each number in the collection
    System.out.println(x); 
}
```

### while loops

#### Infinite loop

```java
while(true){
    // will never stop looping unless I use the break command
}
```

#### do-while loop

The do-while is rarely used but it's handy if you want to make sure the steps run the first time before the loop condition is tested. [Read more.](https://www.tutorialspoint.com/java/java_do_while_loop.htm)&#x20;

```java
do {
    // code will run at least one
} while(x > 10);
```

![This is a silly joke, not an actual way to resolve problems with your loops.](https://1916862645-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHmXRQJbjMi37frjOn8%2F-Lc1mUE344rGQnTD-j39%2F-Lc1msLVvJ2Tr6aBZost%2FAdapt_loop.jpg?alt=media\&token=9b0536d2-d0c4-4274-a3fa-3dc6649a3b28)

This comes up *all* the time in problems. It's a useful trick to avoid crashing your apps, too.&#x20;

{% embed url="<https://www.youtube.com/watch?v=puBq5ULWwBM>" %}

##
