Escolar Documentos
Profissional Documentos
Cultura Documentos
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
1
Review: Denitions
memory)
Data type - determines the size of a variable in memory,
operators/functions
Various sizes (char, short, long, oat , double) Numeric types - signed/unsigned Implementation - little or big endian Careful mixing and converting (casting) types
Review: Operators
Unary, binary, ternary (1-3 arguments) Arithmetic operators, relational operators, binary (bitwise
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
4
Blocks
Block can substitute for simple statement Compiled as a single unit Variables can be declared inside
{
i n t temp = x+y ;
z = f o o ( temp ) ;
}
Nested blocks
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
7
Control conditions
Unlike C++ or Java, no boolean type (in C89/C90) in C99, bool type available (use stdbool.h) Condition is an expression (or series of expressions)
Conditional statements
The if statement
i f ( x % 2) y += x / 2 ;
Evaluate condition
if (x % 2 == 0)
Otherwise, do nothing
i f ( x % 2 == 0 ) y += x / 2 ; else y += ( x + 1 ) / 2 ;
10
i f ( x % 2 == 0 ) y += x / 2 ; else i f ( x % 4 == 1 ) y += 2 ( ( x + 3 ) / 4 ) ; else y += ( x + 1 ) / 2 ;
Additional alternative control paths Conditions evaluated in order until one is met; inner
11
Nesting if statements
i f ( x % 4 == 0 ) i f ( x % 2 == 0 ) y = 2; else y = 1;
12
Nesting if statements
13
Alternative conditional statement Integer (or character) variable as input Considers cases for value of variable
switch ( ch ) {
case Y : / ch == Y /
/ do something /
break ; case N : / ch == N / / do something e l s e / break ;
d e f a u l t : / o t h e r w i s e /
/ do a t h i r d t h i n g /
break ;
}
14
Multiple cases
Compares variable to each case in order When match found, starts executing inner code until
break; reached
Execution falls through if break; not included
switch case case / ( ch ) { Y : y : do something i f ch == Y o r ch == y / break ; switch ( ch ) { case Y : / do something i f ch == Y / case N : / do something i f ch == Y o r ch == N / break ; }
15
Contents of switch statement a block Case labels: different entry points into block Similar to labels used with goto keyword (next lecture. . . )
16
Loop statements
The while loop The for loop The do-while loop The break and continue keywords
17
while ( / c o n d i t i o n / ) / l o o p body /
condition is true
18
semicolons:
Initialization: i = 1
Condition: i <= n Increment: i++
19
20
21
iteration
Body executed at least once
Note semicolon at end
22
Sometimes want to terminate a loop early break; exits innermost loop or switch statement to exit
early
Consider the modication of the do-while example:
char c ;
do {
/ l o o p body / p u t s ( "Keep going? (y/n) " ) ;
c = g e t c h a r ( ) ;
i f ( c ! = y )
break ; / o t h e r p r o c e s s i n g /
} while ( / o t h e r c o n d i t i o n s / ) ;
23
Use to skip an iteration continue; skips rest of innermost loop body, jumping to loop
condition
Example:
# define min ( a , b ) ( ( a ) < ( b ) ? ( a ) : ( b ) ) i n t gcd ( i n t a , i n t b ) {
i n t i , r e t = 1 , m i n v a l = min ( a , b ) ;
f o r ( i = 2 ; i <= m i n v a l ; i ++) {
i f ( a % i ) / i not d i v i s o r of a / continue ; i f ( b % i == 0 ) / i i s d i v i s o r o f both a and b / r e t = i ;
}
r e t u r n r e t ;
}
24
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
25
Functions
Basic syntax of functions explained in Lecture 1 How to write a program using functions?
25
parts
Lets design a program to solve linear Diophantine
ax + by = gcd(a, b).
26
numbers a > b.
[Knuth, D. E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. 3rd ed. Addison-Wesley, 1997.]
Addison Wesley. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.
27
28
29
Break down problem into simpler sub-problems Consider iteration and recursion How can we implement gcd(a,b) recursively? Minimize transfer of state between functions Writing pseudocode rst can help
30
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
31
Programming modules in C
C programs do not need to be monolithic Module: interface and implementation interface: header les implementation: auxilliary source/object les Same concept carries over to external libraries (next
week. . . )
31
Euclids algorithms useful in many contexts Would like to include functionality in many programs Solution: make a module for Euclids algorithms Need to write header le (.h) and source le (.c)
32
33
variables in euclid.c
For functions: put function prototypes in a header le For variables: re-declare the global variable using the
else
Enables access/modifying of global variable from other
source les
34
35
main le diophant.c
#include "euclid.h"
Just compiling diophant.c is insufcient The functions gcd() and ext_euclid() are dened in
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
37
Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables
38
Variable scope
scope the region in which a variable is valid Many cases, corresponds to block with variables
declaration
Variables declared outside of a function have global scope Function denitions also have scope
38
An example
39
40
Static variables
within that le, not globally (cannot be externed) Inside a function, static variables:
41
Register variables
During execution, data processed in registers Explicitly store commonly used data in registers minimize
load/store overhead Can explicitly declare certain variables as registers using register keyword
must be a simple type (implementation-dependent) only local variables and function arguments eligible excess/unallowed register declarations ignored, compiled
42
Example
43
Summary
Topics covered:
Controlling program ow using conditional statements and
loops
Dividing a complex program into many simpler
register variables
44
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.