GAINING CONTROL - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

GAINING CONTROL

Description:

the expression 2*x 5 is also rvalue. C Expressions- Classifications. Expression Lvalue Rvalue ... x = (I ) 3; y = I-4; Block - Declaration ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 43
Provided by: engineeri67
Category:
Tags: control | gaining

less

Transcript and Presenter's Notes

Title: GAINING CONTROL


1
GAINING CONTROL
  • CHAPTER 2

2
2.1 Expressions and Statements
  • Y 4 x 5
  • does_nothing ()

3
EXPRESSIONS
  • Expressions are sequences of tokens that can be
    evaluated to numerical quantity.
  • A TOKEN is a group of characters that belong
    together.
  • ( ) -

4
TOKENS
  • They range from a single number or identifier to
    a more complicated sequence of tokens.
  • X
  • x 5
  • 2 Y - 7 (-3/X 7Z)

5
EXPRESSIONS
  • Expressions can contain any of the operators in
    C.
  • We first discuss arithmetic and assignment
    operators.
  • Others are Bit Operators and Cast Operators.

6
Operators
  • assignment
  • addition
  • - unary minus and subtraction
  • multiplication
  • / division
  • remainder

7
Arithmetic Operations
  • addition and unary plus
  • - unary minus and subtraction
  • multiplication
  • / division
  • remainder
  • a (a/b) b (ab)

8
Compound Assignment
  • The first syntax shortcut is
    compound assignment
  • The assignment statement
  • a a 4
  • can be shortened to
  • a 4

9
STATEMENTS
  • A statement is a sequence of tokens terminated
    with a semicolon that can be recognized by the
    compiler.
  • A statement differs from an expression in that
    statements do not have values.

10
STATEMENTS
  • The purpose of some statements like assignment
    statements is to change the values of variables.

11
CONTROL STATEMENTS
  • The purpose of control statements is to select
    which set of statements to execute in a given
    circumstance or to cause a sequence of statements
    to be executed more than once.

12
CONTROL STATEMENTS
  • if ( expression )
  • statement
  • if (expression)
  • statement 1
  • else
  • statement 2
  • while
  • statement
  • for (initialization test processing)
  • statement

13
Lvalues (lvalues) -LEFT
  • The term lvalues refers to an expression that has
    a Location in memory.
  • The name of an variable is an lvalue.

14
Modifiable lvalues
  • Modifiable lvalues are expressions whose values
    can either be changed or evaluated.
  • Modifiable lvalues can be used on the left-hand
    side of an assignment statement.
  • Left

15
rvalue -RIGHT
  • A rvalue can be evaluated but cannot be changed.
  • An rvalue cannot be used on the left-hand side of
    an assignment statement.

  • Right

16
rvalue
  • Example single character token 5 is an rvalue
  • the expression 2x 5 is also rvalue

17
C Expressions- Classifications
  • Expression Lvalue Rvalue
  • x yes yes
  • x 3 no yes
  • y yes yes
  • 2y - 7 no yes
  • (-2/y 7 y) no yes

18
(No Transcript)
19
2.2 BLOCKS AND COMPOUND STATEMENTS
  • Can contain both declarations and executable
    code.
  • Treated as a single statement.
  • Delimited by a pair of braces.
  • Variables declared in a block are unknown outside
    the block

20
Block Examples
  • x 4
  • y x 3

21
Block Examples
  • int I
  • I 5
  • x (I) 3
  • y I-4

22
Compound Statements
  • A compound statement is a sequence of statements
    that can be used anyplace in the syntax that a
    simple statement can be used.
  • It is a common construct in most programming
    languages.

23
Blocks
  • In C the construct that implements a compound
    statement is called a block.
  • The syntax of the C language allows more than
    just a sequence of statements in a block
  • Variable declarations can be included with the
    executable statements

24
Block
  • A block must begin with an opening brace and
    terminate with a closing brace.
  • .
  • The contents of the block may consist of
    declarations, statements, both or neither.

25
Block
  • The contents of the block may consist of
    declarations, statements, both or neither.
  • int I
  • x (I) 3
  • y I-4

26
Block - Declaration
  • A declarations in a block must appear before the
    statements.
  • int f ( int)

27
Block - Placement
  • A block can be placed anywhere in a program that
    a simple statement can be placed.

28
(No Transcript)
29
Local Variable
  • When a variable is declared inside of a block, it
    is called a local variable, since the compiler
    will only recognize its name from its point of
    declaration to the end of the block.

30
Local Variable
  • The statements in an inner block can access any
    variable that the surrounding function can access.

31
Varaiables
  • Surrounding Block
  • int 5
  • Inner Block
  • int i 3

32
Style
  • A C program or function will be more readable
    when all variables used within a single function
    are given different names.
  • The compiler can keep variables declared inside
    and outside of a block straight, but human beings
    may have problems.
  • Declarations should appear in one place.
  • Auxiliary declarations in a block are possible.

33
(No Transcript)
34
2.3 IF and if-else Statements
  • This conditional statement allows a program to
    test a condition and then choose which code to
    execute next.
  • The choice depends on the outcome of that test.
  • If (expression)
  • statement

35
If-else Statement
  • If (expression)
  • statement 1
  • else
  • statement 2
  • If expression evaluates to true (nonzero),
    statement 1 is executed,
  • If expression evaluates to false (zero),
    statement 2 is executed.

36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
Nested or Compound if-else
  • If (expression)
  • statement
  • else if (expression)
  • statement
  • else if (expression)
  • statement
  • else
  • statement

40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com