# Comp 110 Control Flow Statements

## Control Flow Statements

Problem: You are given a scenario where you need to sum up the contents of an array. In addition, if one of the numbers in the array is greater than 100, give an output of "0". Let's say the array is "{3,5,2}"  The answer should be: "10". If the input is "{6,101,4}" the answer should be "0" Let's attempt to create a method to do that.

``````public static int sumArr(int[] a){
int sum = 0;
sum += a[0];  //sum += a[0] is equivalent to "sum = sum + a[0];"
sum+= a[1];
sum+=a[2];
return sum;
}``````

Take a second to read this code. Our current method adds the first three elements together. What is the problem with this code?

First of all, if one of the numbers is greater than100, it does not return 0. Also, what if the array has a length of 4? What if it has a length of 32?  Since we do not know the length, we need another tool in our tool box. This is where looping comes in. Looping will allow us to sum arrays with an unknown length! There are a number of ways to create loops. Secondly, if and if-else statements will allow us to change the output depending on a specific value. We call all of the above our control flow statements. In this section you will learn about the following::