Let's continue our quick-start into C Sharp by talking about modifying the execution order of code through the use of conditionals and loops. In general, most programming languages run one line at a time in sequential order. Line one runs first, then line two, then line three and so on. Code executes from top to bottom. You can think of code execution as steps the computer takes from one line to the next. At times, you may want the computer to not follow this step-by-step process. That is, you want the computer to jump forward or backward in the code. This is called branching or looping. Branching allows you to change the execution order based on some condition, such as user input, changes in the computer environment, such as time passing or some other condition. A conditional is a logic expression that evaluates to true or false. Here are the common comparison operators that we use in C sharp, including equals to. Notice that it has two equals signs. A single equal is used for assignment like setting a variable but the double equals is for comparing if the left side of the operator is equivalent to the right side. If so, the conditional evaluates to true. If not, it evaluates to false. We also have not equals, greater than, greater than equal to, less than, and less than equal to. We can use these operators in our conditional statements. The most common form of branching can be done with an if statement. That is, if some condition evaluates to true, then instructions are run between the curly braces that proceed the if statement. If not, these instructions are skipped over. In this example, the first if statement is checking if the temp variable is less than equal to 32. If so, it outputs, it is freezing to the console. But in this case the temp variable is not less than 32 since it's set to 40 above. Therefore, the first if condition evaluates to false and we skip over it. The second if condition however, evaluates to true. Wear a jacket is outputted to the console. Then the computer moves on to the third if statement. This condition evaluates to false. The where suntan lotion message is not outputted into the console. Else allows us to have an alternate section of code to run when the if condition evaluates to false. When we combine the if with the else statement, we can create more interesting branching logic. For example, here we set up several conditions to check for the temp variable. The logic is setup to evaluate the temp several times. If it evaluates to true, then the message is outputted to the console and we're done. If however it evaluates to false, then the execution continues to the else statement. In the case of this example, where temp equals 100, all the if and else if statements will evaluate to false. The final else statement will run and wear a bathing suit will be outputted to the console. We can do more complex logic when we combine the AND and OR operators. The AND operator is represented as two ampersands in C Sharp and the OR operator is represented as two vertical lines or shift backslash on the keyboard two times. In this example, the first if statement checks to see if the temp is greater than, equal to 50 and less than 65. Temp is set to 55. Therefore, both parts evaluate to true and true and true is true. The wear a sweater message is outputted to the console. The second if statement checks to see if the temp is greater than 90 or the weather equals sunny. The temp part evaluates to false, but the weather part evaluates to true. Since the OR operator was used, the overall condition is true and where suntan lotion is outputted to the console. We can use parentheses to build even more sophisticated logic as demonstrated here. The parentheses and logical evaluation work just like they do in math operations to explicitly define the order of evaluation. In addition to if statements, the switch statement is another way to modify the order of execution. Here is an example of a switch statement. After the switch, you specify a variable that you want to check to see if it equals to any number of cases. Each case is defined within the body of a switch statements, curly braces. If a case evaluates to true, then the code that is after the colon and before the break is run. If none of the cases evaluate to true, then the code after the default is run. The example here where the weather variable is set to cloudy, the default will run and dress for success will be outputted to the council. Another way to modify the order of execution of code is with loops. Loops are portions of code that repeat while a condition is met. There are three common types of loops. While loops continue while a condition is met. For loops which typically loop for a certain number of times and for each loops. Here is an example of a while loop. You can see we set up an integer variable called lives and set it equal to three. Then we create a while loop. The code between the curly brackets will run through each time the condition is true. That is, while lives is greater than zero, the two lines below will continue to run or loop. The first time through the loop, three greater than zero evaluates to true. Lives three is outputted to the console. Then lives is decremented by one. That is lives equal lives minus 1. The second time through the loop, two is greater than zero, also evaluates to true. Lives: 2 is outputted to the console. Then lives is decremented by one once again. The third time through the loop, one is greater than zero, also evaluates to true. Lives: 1 is outputted to the console. Then lives is decremented by one, once again. The fourth time through the loop lives is now zero. Zero is not greater than zero, so the condition evaluates to false and execution will continue after the while loop. Then you are now dead, is outputted to the console. The for loop is a bit more complex to set up. It contains an initialization, a condition, and an incrementor. Here's an example for a loop. The initialization sets up the integer variable name power and sets the initial value to one. The condition checks if the power is less than three, which of course it is the first time because we just set it to one. If the condition evaluates to true, then the code between the curly brackets is run. The first time through, power is one, is outputted to the console. Then the incrementor happens. In this case, power is incremented by one, so it goes from 1-2. Then we repeat the condition check again and since power is still less than three, we run the loop again, and output power is two to the console. Now power is incremented again, so it goes from 2-3. Then we check the condition again. This time three is not less than three, so the loop is done, and line four would then be executed. Outputting power up complete to the console. For loops are used often with arrays and lists. In this code example in line one, I create the family array that we created previously, [inaudible] it up with the five Simpson family names. The for loop is initialized with i equals 0. The condition is setup to continue to loop while i is less than the length of the family member array and at the end of each loop, i will be incremented by one. Therefore, this code would output each of the family members names in the array, starting at index zero, then one, then two, then three, and finally index four. Then i would equal 5, which is not less than the length of the family and the code would continue to execute after the for loop. The for each loop is another loop type that is also helpful when you need to loop over arrays or lists. In this code example in line one, I create the family list that we created previously, setting it up with the five Simpson family names. The for each loop is set up in line three. It basically says that I want to loop over each element in the family List, storing the element into the variable family name each time through the loop. Just like the previous example, this loop will output Homer, Marge, Bart, Lisa, and Maggie as it iterates over each element of the family list. In summary, the if and switch statements allow you to run certain code based on a logic condition, and the while for and for each statements allow you to loop over code based on logic conditions. Overall, these techniques allow you to modify the order of execution.