Control structures dictate how a program will behave under certain circumstances. Control structures belong to one of two families: those that test values and determine what code will be executed based on those values, and those that loop, performing identical operations multiple times. Control structures like if-then-else and switch the program to behave differently based on the data that they are fed. The while and for loops allow you to repeat a block of code as often as it is needed. As you will see, that functionality can be very useful when designing complex programs.
This unit will introduce you to control structures and how they are used before moving on to discuss if, switch, while/do-while, and for loops. We will also discuss some advanced topics, such as nesting and scope. By the end of this unit, you should be able to draw from the information you learned in the previous unit to create a control structure, which will allow you to create more involved and useful programs.
Completing this unit should take you approximately 10 hours.
Read this chapter, which reviews how computers make decisions using if statements. As you read this tutorial, you will understand that sometimes it is important to evaluate the value of an expression and perform a task if the value comes out to be true and another task if it is false. In particular, try the simulated program under the heading "Simulated Program" to see how a different response is presented to the user based on if a number is positive or negative.
Pay special attention to the "More Than One Statement per Branch" header to learn how the 'else' statement is used when there is more than one choice.
Read this chapter, which discusses the switch and '?' operators to write conditional statements. As you read this tutorial, you will learn that sometimes it is better to use a 'switch' statement when there are multiple choices to choose from. Under such conditions, an if/else structure can become very long, obscure, and difficult to comprehend. Once you have read the tutorial, you will understand the similarity of the logic used for the 'if/else' statement, '?', and 'switch' statements and how they can be used alternately.
Read this chapter, which explains while loops. This is in contrast to how a do-while loop works, which we will discuss later. In a do-while loop, the body of the loop is executed at least one time, whereas in a while loop, the loop may never execute if the loop condition is false.
Complete the review exercise. Think of the correct response to fill in the blank for each question, and then click on the blank to reveal the correct answer.
The 'do-while' loop is a variation of the while loop. 'do-while' loops always execute at least once, whereas while loops may never execute.
This chapter discusses the 'for' loop in greater detail, as well as the scope of variables in the 'for' loop.
This chapter discusses how control structures such as loops and if statements can be combined together to implement program logic.
This article explains variable scope within different control structures such as a block, while loop, and branching statement such as the if statement.
Complete this review exercise. Think of the correct response to fill in the blank, and then click on the blank to reveal the correct answer.
Take this assessment to see how well you understood this unit.