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

Topic outline

  • Unit 2: Structuring Program Code

    This unit focuses on implementing simple control structures. First, we will learn how to use conditional and iteration structures to make decisions and to repeat code. We will then discuss how to use debugging tools to test and troubleshoot these structures. We will also explore how to break our code into smaller, more manageable pieces by putting certain common pieces into functions. We will also discus scope, as well as passing variables by value and by reference. Finally, we explore a special type of class, the string, which has some special functions that allow us to manipulate text. By introducing classes and how they are used here, we will be ready to tackle object-oriented programming in the next unit.

    Completing this unit should take you approximately 8 hours.

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

      • use conditional and iteration structures in C++;
      • explain the purpose of different features of the debugging tool;
      • define test cases and coverage analysis;
      • use simple functions; and
      • use character arrays and the functions of the string class.
    • 2.1: Conditional and Iteration Structures

      • Read these lecture notes to learn about control structures in C++ programming. Compile the examples from the notes, and make sure you understand the code in each line. After reading these notes, you should be able to define conditional structures, iteration structures, and jump statements.

      • Computers are thought to be very intelligent machines, but they are only as intelligent as the humans that program them. If-else gives programs the capability to make decisions and provide an illusion of intelligence. Watch this video to learn how to add this intelligence capability to your programs.

      • Simple decisions are useful on many occasions, but sometimes decisions are more complex and have multiple layers. Here you can learn how to combine if statements with AND and OR to address many different scenarios.

      • While decisions give computers their "intellectual" abilities, loops give them their usefulness. The ability to repeat something over and over for dozens, hundreds, or even millions of times ensures that computer programs can perform tasks that are quite complex in a fraction of the time it takes us humans. The while loop is the fundamental approach to repetitive sequences.

      • The do-while loop is a version of the while loop. Though it is possible that a program will never enter the loop with a while loop (since it is possible that the condition is already met before entering the loop), do-while loops guarantee that the loop gets performed at least once. Watch this video to learn how do-while loops differ from the standard while loop.

      • Although while loops are the basic loop structure, for loops are the enhanced version of while loops that allow for more flexibility, and let programmers simplify and improve the accuracy of their code. Watch this video to learn how to use for loops. 

    • 2.2: Testing and Debugging

      • Read this section to learn more about the basics of software testing.

      • Learning to debug is an essential skill for any programming language. Knowing how to search your code for bugs, walk through the program's interpretation, and examine the variable manipulation is an essential part of debugging. Learn how to use the tools built into the Eclipse environment to debug your program. Though this video refers to Java, debugging C++ in Eclipse works similarly.

    • 2.3: The Scope of Variables in a Simple Function

      • Functions are small segments of code that are removed from the normal code flow, and they are called to perform specific actions. After the function executes, control returns back to the normal flow. Within a function, data may be sent and returned to the rest of the program.

    • 2.4: Arguments Passed by Value and by Reference in a Simple Function

      • When data is passed by value, the actual contents of the variable is passed. However, with some data, you may instead simply want the program to access the data directly from the current location where it is stored. To do this, you pass the value by reference, which tells the function to access the address of the variable.

    • 2.5: Functions of the String Class

      • Now we'll explore the use of string arrays, which are part of C programming. C did not allow for the use of strings initially, so string arrays are simply a string of characters that are stored in an array. We haven't discussed arrays yet, but this is a great introduction to the use of character strings.

      • Review this page for some more information on character strings, or string arrays.

      • The string class is an extension that was added to C++ that deals with character strings as strings, not as arrays. Read this article to learn how to use classes to call methods and how to manipulate strings to access text data.

      • This chapter highlights the many different functions that are part of the string class in C++.

    • Unit 2 Exercises

      • Complete these exercises to test your understanding of iterations. 

      • Complete these exercises to test your understanding of functions.

      • Answer the six questions in problem set 1. After you finish, check your answers.

      • Complete these exercises to test your understanding of string arrays.