loader image
Skip to main content
If you continue browsing this website, you agree to our policies:
x

Topic outline

  • Unit 5: Control Structures

    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.

    • Upon successful completion of this unit, you will be able to:

      • use control structures;
      • write an 'if' statement;
      • write a 'switch' statement; and
      • explain various looping structures such as for, while, and do loops.
    • 5.1: Introduction to Control Structures

      • This article gives a general introduction to control structures.

    • 5.2: The 'if' statement

      • 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.

      • Attempt this ungraded quiz.

    • 5.3: The 'switch' Statement

      • 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.

      • Attempt this ungraded quiz.

    • 5.4: The 'while' and 'do-while' Loops

      • 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.

      • Attempt this ungraded quiz.

    • 5.5: The 'for' Loop

      • The 'for' loop is more compact than the 'while' and 'do' loops and automatically updates the loop counter at the end of each iteration. Both 'for' and 'while' loops are designed for different situations. You'll learn more about when to use each later.
      • This chapter discusses the 'for' loop in greater detail, as well as the scope of variables in the 'for' loop.

      • Attempt this ungraded quiz.

      • Attempt this ungraded quiz.

    • 5.6: Advanced Topics

      • 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.

    • Unit 5 Assessment

      • Take this assessment to see how well you understood this unit.

        • This assessment does not count towards your grade. It is just for practice!
        • You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
        • You can take this assessment as many times as you want, whenever you want.