MYF

[RA] Ch2 Reading Code

Reading Assignment: All of Programming Chapter 2 Reading Code

Learning to read code means that you can execute it by hand in a step-by-step fashion.

2 important parts to executing code by hand:

  • understanding what each statement does
  • keeping track of the state of the program in a correct fashion

Suggestions on the 1st part: make a “quick reference” sheet.

Variables

Programs track most of their state in variables.

Declaration

Definition of identifier: the name of a variable.

Assignment

Definition of assignment statements: change the value of variables.

Expressions

Definition: a combination of values and operations which evaluates to a value.

Modulus operator ‘%’ is an uncommon operator, which evaluates to the remainder when dividing the first operand by the second.

Variables are taken part in expressions by reading the current value of its box.

The way to solve an algebraic equation:

  • write a meaningful assignment statement
  • take current value of variables
  • update variables

Functions

A function gives a name to a parameterized computation–it is the implementation in code of a specific algorithm. One should declare a function first, which provides the definition for how a function behaves, and calling a function, which executes the definition of the function on specific values of the parameters.

Scope

Definition: The scope of a variable is the region of code in which it is visible. Within a variable’s scope, its name may refer to it. Outside of a variable’s scope, nothing can refer to it directly. In C, the scope of a local variable begins with its declaration and ends at the closing curly-brace(}).

-w416

The colored block means the scopes of variables.

If no variables in the scope, the reference is illegal. If exact one variable is mentioned, that is it. If there are multiple variables with the same name, we select the one whose declaration is in the innermost enclosing block. That is, if you went backwards out of blocks, through open curly braces, the variables which would go out of scope first is the one to use.

Try not using global variables for any problems.

Note: The rest of this section talks about the scope of variables by giving an exact example.

Printing

printf function. The last letter f stands for “formatted”

Escape sequences are two (or more) characters, the first of which is a backslash(\), which gives the remaining characters special meaning.

Conditional Statements

In C, an if/else statement specifies the block of code to be executed. Logic Operators and Boolean Operators are of great help to give true or false value.

if/else

switch/case

Shorthand

also called syntactic sugar

2 types:

  • a shorter way to write common patterns of existing things, like x += y
  • omit the curly braces around single statement block of code, like if if statement is one single statement, the curly braces are not required. But it is hight inadvisable to do so!!

Loops

Loops is a important way to repeat the same block of code multiple times. Another way to repeat is recursion, which will being discussed in Chapter 7.

while Loops

do-while Loops

A while loop may execute the body zero times, skipping the entire loop. By contrast, a do-while loop is guaranteed to executes its body at least once because it executes the loop body before ever checking the condition.

for Loops

Nesting

continue and break

Higher-level Meaning

Except for reading code in a step-by-step fashion, understanding of the meaning of a piece of code is another useful skill. That is the ability to translate the code into the algorithmic step it represents, then figure out what the purpose of that general algorithm is.