Lesson 2

Variables, expressions, and statements

From CS100 Summer Homework

文档内容

A C program is composed of a series of statements, each one inducing one or several operations to be executed. In its simplest form, a statement is a line of code terminated by a semi-­‐colon. There are a number of different types of statements:

  • Declaration statements: A declaration is simply a statement through which we describe an identifier, such as the name of a variable. Through such declarations, the compiler will know which local variables will have to be allocated on the memory stack, and what identifier is used to refer to those variables within their scope. In its simplest form, a declaration statement takes the form:

    type declarator

    where type denotes the type of the variable and declarator the name of that variable. A few primitive variable types sufficient for the solution of the preparatory summer homeworks are listed as follows:
    char 8-­‐bit (1-­‐byte) variable representing a character (interchangeably used to represent 8-­‐bit integer numbers)
    short, int, long Integer numbers for which the (physical) binary representation has a bit‐width of at least 16, 16, and 32 bits, respectively (depending on computer and implementation)
    float, double Special representations for floating point numbers. Physical bit-­‐width is 32 and 64 bits on most systems.
    You are invited to do some research by yourself to understand how for example the ASCII encoding scheme is used to define correspondences between numbers and characters such as the letters of the alphabet or other special characters. It is also useful to understand the binary number system, or floating-­‐point number representations:
    https://en.wikipedia.org/wiki/C_data_types
    https://en.wikipedia.org/wiki/ASCII
    https://en.wikipedia.org/wiki/Single-­‐precision_floating-­‐point_format
    https://en.wikipedia.org/wiki/Double-­‐precision_floating-­‐point_format
    https://en.wikipedia.org/wiki/Binary_number
    * 你可能会发现国内无法访问维基百科!所以我提供了其在国内的镜像:
    C data types
    ASCII
    Single-­‐precision_floating-­‐point_format
    Double-­‐precision_floating-­‐point_format
    Binary number
  • Expression statements: Expression statements can be either just a single entity such as a variable or a combination of entities interconnected by operators. The following are simple examples of expressions:
    a
    a + b
    x = y
    t = u + v x <= y
    ++j
    
    It is useful to read up operator preference to understand in which order the different operations will be executed in case there is more than one. Also note that declaration and expression statements can be combined, as in
    int a = b + c;
  • Compound statements: Compound statements are in fact just a combination of several statements grouped by braces. The sub-­‐statements can again be declaration statements, expression statements, or compound statements. An example would be:
    {
    pi = 3.141593;
    circumference = 2. * pi * radius;
    area = pi * radius * radius;
    }
    
  • Control statements: Control statements are a bit different, as they use special keywords reserved for the compiler and will dynamically influence the program flow based on some conditions. The most basic control statement is the if-­‐statement. It consists of an "if" keyword followed by a condition in parentheses. The condition is an expression that evaluates to either 0 or not (representing an unfulfilled or fulfilled condition, respectively). The closing parenthesis is followed by either a single expression or a compound statement to be conditionally executed. The if-­‐statement may be followed by an else-­‐statement, thus resulting in an if-­‐else-­‐statement. The else-­‐statement simply consists of the keyword "else", again followed by either a single expression or a compound statement, and defines alternative operations to be executed in case the condition is unfulfilled. The following indicates an example of an if-­‐else-­‐statement:
    if( condition ) 
      { statements
    }
    else {
      statements
    }
    

有话想说?