CS101 Study Guide

Unit 5: Control Structures

5a. Use control structures

  • How can a program adapt to evolving circumstances?
  • How do we control the flow of a computer program?
  • How do we cycle through a block of code until a certain condition is reached?

There is, as we have seen, a certain logic in the writing of computer programs. One step flows naturally into another so that a particular outcome is reached. Such sequential programming is a good way to start. However, especially in this modern age, programs must adapt to evolving circumstances. These circumstances arise because of the data put into a program and the increasing complexity of the steps to process that data. As in all things, there are effective and ineffective ways to achieve this.

In this course, we are studying the ways of formal logic, "if this then that and nothing else". There are other ways, not least of which is fuzzy logic from Lofti Zadeh, which admits to multiple-set membership but also which admits that, eventually, we have to make a crisp decision on how to react (Fuzzy Logic). There are also gaussian networks and other means of breaking up a decision space so that you can be more selective than formal logic allows. Many of these can be likened to statistics, a formal mathematics for making the best guess possible given the data at hand. We do not dig into these in this course but you should be aware they exist.

Constructs common to many languages: if-then, if-then-else, while, do-while, for, conditional operator, and switch statement, are an important part of C++. These allow us to select which block of code to execute, under which circumstances, and how long to continue the execution of that code block. We specify a condition and then decide whether or not that condition exists. Thus, our algorithms (problem-solving steps, steps to achieving a certain outcome) can grow in complexity and adapt to the circumstances and associated data.

Review: Control Structures: A relatively short reading that introduces the concepts and constructs to be discussed in detail in this unit.


5b. Write an 'if' statement

  • What are the two types of 'if' statements?
  • Which type of 'if' statement could be paraphrased, "If the specified condition exists, execute these steps. In any case, execute these other steps"?
  • What is the maximum number of code blocks within an 'if' statement?

'If' statements control access to a maximum of two blocks of code as shown in this table:

If (condition) then < control block 1>

.... < code executed regardless of condition >

If (condition) then < control block 1>

else < control block 2>

..... < code block executed regardless of condition >


Depending on the condition, a boolean true/false result, <control block 1> or <control block 2> will be executed. Regardless of the condition being true or false, the code after the 'if' statement will be executed. This allows us to run unique code on the basis of the situation at hand within the computer program. For instance, a variable called 'temperature' might cause a certain block of code to be executed if the value is beyond a certain threshold. Or, we might want to issue an overdraft notice if dollars_in_account falls below zero. The 'if' structure allows us to carry out these ideas.

Review: Decision Making: See these sections for a review of the basics: 2, 8, 13, 16, 17


5c. Write a 'switch' statement

  • What is the essential difference between switch-case and the conditional operator?
  • Which component of the switch-case is often forgotten by programmers but will compile and execute anyway? Why is it a bad idea to not employ this component?
  • Why would we want to use the conditional operator instead of if-then-else?
  • What can if-then-else and the conditional operator deal with that switch-case cannot?

if-then-else can be compared to the conditional operator in the following way:

if-then-else

conditional operator

if(condition)

{

< code block if true >

}

else

{

< code block if false >

}

variableName = (condition) ? <value if true> : <value if false >;


Notice that if-then-else can execute an entire code block, whereas the conditional operator simply assigns a value to a variable. So, the conditional operator is a convenient shorthand for simple cases. It can be used in more complex cases by calling a function that returns a value appropriate to the left-hand side of equals.

Switch-case concentrates on integer values only. The "switch" is an integer variable of some type. The "case" is what to do when the value is equal to a particular value. This table compares switch-case to if-then-else:

if-then-else

switch-case

if(result == v1) { <code block if true>}

else if(result == v2) { < code block if true>}

...

...

...

else { <code block if none are true> }

switch(result)

 {

case v1:

<code block if result == v1>

break;

case v2:

<code block if result == v2>

break;

...

...

...

default:

<code block if none are true>

}


As you can see, switch-case is more readable and understandable than lengthy if-then-else. On the other hand, if-then-else and conditional operators can deal with more than just integer types. You should pick the approach that is most appropriate to the program being written.

When using a series of if-then-else or switch-case, always have a default result, what should happen if none of the conditions hold. It is never good programming practice to leave out the critical condition, "none of the conditions hold". This could well be indicative of a logic or programming error earlier in the code. Even when nothing needs to be done in such a situation, at least put the default in with a dummy operation and a comment that nothing needs to be done. Make that conscious decision. Also, with switch-case, do not forget the "break" component. Otherwise, the code will continue to the next code block. 

Review: The Conditional Operator and the 'switch' Statement: See these sections for review: 2, 5, 6, 8, 9, 12


5d. Explain various looping structures such as for, while, and do loops

  • What is the difference between a while loop and a do-while loop?
  • What is the difference between a for loop and a while loop?
  • Why do we need conditional looping structures?
  • Can conditional looping structures be combined with non-looping conditional structures?

We have previously examined non-looping conditional structures. These ask a true/false question and execute a block of code once, depending on the answer to the question. Now we turn to looping conditional structures. These ask a question and continue to execute a block of code until the answer to the question is false. At variance with this idea is do-while. It will execute its block of code at least once, regardless of the answer to the question.

while loop syntax: while(condition) { <execute this block of code> }

As long as the condition is true, the block of code will be executed over and over again.

do-while loop syntax: do { <execute this block of code> } while(condition)

The block of code will execute at least once and then over and over until the condition is true.

for loop syntax: for(<operation 1>; <condition>; <operation 2>) { <execute this block of code> }

When this structure starts, <operation 1> is carried out. Then the condition is checked. If the condition is true, the block of code is executed. Then <operation 2> is carried out, following which the condition is checked. If the condition is true the block of code is executed again. Then <operation 2> followed by checking the condition. This process continues until the condition is false.

Clearly, looping conditionals keep us from having to duplicate blocks of code. Note that blocks of code can be anything legal in the language. So, non-looping and looping conditionals can be combined. Note also that if the condition never becomes false then the loop will continue until the program is manually halted.

Review:


Unit 5 Vocabulary

This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.

  • algorithms
  • break
  • conditional operator
  • default
  • do-while loop 
  • for loop
  • if-then
  • if-then-else
  • looping
  • switch-case
  • while loop