CS105 Study Guide

Unit 3: Input and Flow Control Statements

3a. Explain the differences between programmer-initialized variables and user input variables

  • What is a programmer-initialized variable?
  • What is a user input variable?
  • How are user input variables and programmer-initialized variables applied when programming?

All programs require the use of variables that must be initialized to some value using the assignment operator (=) in order to use them within a program. After a variable has been initialized it can be modified using the assignment operator if desired.

A programmer-initialized variable is initialized within a program; hence, its value is known simply by looking at the code.

The input command can be used to create a user input variable whose value is not known until it is entered by the user.

Both applications have their place within a program. User input variables are used when data must be entered by someone using the program (such as by entering a password). Programmer-initialized variables are set by the programmer to a specific value (such as a=35 or s='hello').

To review, see The Input Command.

 

3b. Write a program that will take string and numerical data from the keyboard

  • What is the basic syntax of the input command?
  • What data type does the input command return after executing?
  • What care should be taken if numerical input data is desired?

Since the input command is used to allow for user input, it is sensible to provide a message to let the user know what is required for the input (such as indata=input('Enter your name: ')). The input command will suspend program execution until the user hits the return key. After this, the destination variable will contain the contents of what was entered by the user.

The input command returns data of type str. If numerical data is required from the user input, it must be converted to the desired data type. For example:

x=float(input('Enter the number of seconds: '))

would convert the input data to a floating point value. If the conversion step is neglected, then instructions following the input statement that assume numerical data as the input would be operating on string data. This would most likely result in some type of exception.

To review, see The Input Command.

 

3c. Write conditional statements using logical operators

  • What is the syntax for an if statement?
  • How are if statements used?
  • How can else and elif clauses be of use?

if statements are extremely important when it comes to computing because they allow decisions to be made. 'if' decisions are made by evaluating Boolean expressions. This is a major reason why expertise with relational and logical expressions is so necessary. If a Boolean condition evaluates to a value of True, then the code contained within an if statement will execute; otherwise, it will not.

Python uses indentation to indicate all instructions to be executed in the event that the if condition is True. Furthermore, if statements look like:

if condition:

Indented Python code to execute if the condition is True

Always remember to put a colon (:) after the if condition.

In addition to the if, Python allows for else and elif clauses to be created if multiple conditions and decision outcomes are required. When putting together elif statements, it must be understood that only one potentiality will execute in the event that some elif condition is True. Therefore, when encountering a complex looking series of elif statements, most of the code will not execute. Only the path containing a True condition will be taken and the others will be ignored.

To review, see if, else, and elif Statements.

 

3d. Write for loops and while loops using logical operators and the range function for flow control

  • How are for loops used?
  • How are while loops used?
  • What is the difference between a for loop and a while loop?

Loops are a helpful programming construct because they efficiently allow for a series of operations to be performed over a desired set of iterations. for loops generally iterate over a set range of values. while loops generally iterate while a specified logical condition evaluates to a value of True. Both types have their place within the Python programming language.

The syntax for for loops and while loops is similar to the if statement in that the colon (:) is used to indicate the loop statement and indentation is used to house instructions contained within the loop. Predicting how a variable changes as a loop iterates must be part of your skill set as it indicates an understanding of how a loop is working. Accurately predicting how a loop is working means that you are able to mentally execute each iteration and, in some sense, visualize or track how the value of a variable changes during or at the end of each iteration.

To review, see Using "while" Loops and Using "for" Loops.

 

3e. Explain how break, continue, and pass statements are used in loops

  • How is the break instruction used?
  • How is the continue instruction used?
  • How is the pass instruction used?

When writing loops, it may be necessary to interrupt the program flow based upon some condition being satisfied. The break statement allows you to exit a loop without having to iterate to its terminating condition. Be careful when applying the break statement within an inner loop of a nested loop, since only the inner loop will terminate and the outer levels will continue to execute.

The continue statement, at the time it executes, will disregard any remaining instructions within a loop and flow to the next iteration of the loop currently being executed. continue statements and break statements are commonly used in conjunction with if statements such that, when a condition is satisfied, the break or the continue statement can be invoked.

The pass instruction effectively does nothing. It is often used to take the place of code not yet integrated into a program. It allows you to construct, for example, the skeleton of the loop with the internal loop details reserved for later development.

To review, see "break", "continue", and "pass" Statements.

 

Unit 3 Vocabulary

Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.

  • programmer-initialized variable
  • user input variable
  • input command
  • iterations
  • for loops
  • while loops
  • break statement 
  • continue statement 
  • pass instruction