TRANSFORMING
OPPORTUNITY
ACROSS SUB-SAHARAN
AFRICA
Introduction to Lecturer: Ms. Onalethata Matlhape
Programming BSc (Hons) Software Engineering
MEng Computer Engineering
Languages
Introduction to STRUCTURED
Programming PROGRAMMING
Languages USING C
LECTURE OUTLINE
• Overview of the C language syntax, structure, and tools
• Data types, variables, operators, and expressions
• Control structures: decision-making (if, switch) and
iteration (for, while, do-while)
• Modular programming with functions and parameter
passing
Lecture Objectives
• Review
• Blocks and Compound Statements
• Control Flow
• Conditional Statements
• Loops
• Functions
Review
• Variable -name/reference to a stored value (usually in
memory)
• Data type -determines the size of a variable in memory,
what values it can take on, what operations are allowed
• Operator -an operation performed using 1-3 variables
• Expression -combination of literal values/variables and
operators/functions
Review: Data Types
• Various sizes (char, short, long, float, 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 and logical) operators, assignment
operators, etc.
• Conditional expressions
• Order of evaluation (precedence, direction)
Blocks and Compound Statements
• A simple statement ends in a semicolon:
z = foo(x+y);
• Consider the multiple statements:
temp = x+y;
z = foo(temp);
• Curly braces – combine into compound statement/block
Blocks
• Block can substitute for simple statement
• Compiled as a single unit Variables can be declared inside
{
int temp = x+y;
z = foo(temp);
}
• Block can be empty {} No semicolon at end
Nested Blocks
• Blocks nested inside each other
{
int temp = x+y;
z = foo(temp);
{
float temp2 = x∗y;
z += bar(temp2);
}
}
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)
e.g. n<3 or x<y || z<y
• Expression is non-zero condition true ⇒
• Expression must be numeric (or a pointer)
const char str[] = "some text" ;
if (str) / ∗ string is not null ∗ /
return 0;
Conditional Statements
• The if statement
• The switch statement
The if statement
if (x% 2)
y += x/2;
• Evaluate condition
if (x %2==0)
• If true, evaluate inner statement
y += x/2;
• Otherwise, do nothing
The else keyword
if (x%2 == 0)
y += x/2;
else
y += (x+1)/2;
• Optional
• Execute statement if condition is false
y += (x+1)/2;
• Either inner statement may be block
The else if keyword
if (x%2 == 0)
y += x/2;
else if (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 statement then
executed
• If multiple conditions true, only first executed
• Equivalent to nested if statements
Nesting if Statements
if (x%4 == 0)
if (x%2 == 0)
y = 2;
else
y = 1;
To which if statement does the else keyword belong?
Nesting if Statements
To associate else with outer if statement: use braces
if (x%4 == 0) {
if (x%2 == 0)
y = 2;
} else
y = 1;
The switch Statement
• 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 else ∗ /
break ;
default :/ ∗ otherwise ∗ /
/ ∗ do a third thing ∗ /
break ;
}
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 (ch) {
switch (ch) { case ’Y’ :
case ’Y’ : / ∗ do something if
case ’y’ : ch == ’Y’
/ ∗ do something if
ch == ’Y’ or case ’N’ :
ch == ’y’ ∗ / / ∗ do something if
break ; ch == ’Y’ or
} ch == ’N’ ∗ /
break ;
}
The switch statement
• Contents of switch statement a block
• Case labels: different entry points into block
• Similar to labels used with goto keyword
Loop statement
• The while loop
• The for loop
• The do-while loop
• The break and continue keywords
The while loop
while (/ ∗ condition ∗ /)
/ ∗ loop body ∗ /
• Simplest loop structure – evaluate body as long as condition is true
• Condition evaluated first, so body may never be executed
The for loop
int factorial ( int n) {
int i, j=1;
for (i =1; i<= n; i++)
j ∗= i;
return j; }
• The “counting” loop
• Inside parentheses, three expressions, separated by semicolons:
o Initialization: i=1
o Condition: i <= n
o Increment: i++
• Expressions can be empty (condition assumed to be “true”)
The for loop
Equivalent to while loop:
int factorial ( int n) {
int j =1;
int i=1; / ∗ initialization ∗ /
while (i <= n/ ∗ condition ∗ /) {
j ∗= i;
i ++; / ∗ increment ∗ /
}
return j;
}
The for loop
• Compound expressions separated by commas
int factorial ( int n) {
int i, j;
for (i =1, j=1; i<= n; j ∗= i , i++) ;
return j;
}
• Comma: operator with lowest precedence, evaluated left-to-right; not same as
between function arguments
The do-while loop
char c;
do {
/ ∗ loop body ∗ /
puts ( "Keep going? (y/n) " );
c = getchar ( ) ;
/ ∗ other processing ∗ /
} while (c == ’y’ && / ∗ other conditions ∗ / );
• Differs from while loop – condition evaluated after each iteration
• Body executed at least once
• Note semicolon at end
The break keyword
• Sometimes you want to terminate a loop early
• break; exits innermost loop or switch statement to exit early
• Consider the modification of the do-while example:
char c;
do {
/ ∗ loop body ∗ /
puts ( "Keep going? (y/n) " );
c = getchar ( ) ;
if (c != ’y’)
break ;
/ ∗ other processing ∗ /}
while (/ ∗ other conditions ∗ / );
The continue keyword
• 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))
int gcd ( int a, int b) {
int i , ret = 1, minval = min(a,b);
for (i = 2; i <= minval; i++) {
if (a%i) / ∗ i not divisor of a ∗ /
continue ;
if (b%i == 0) / ∗ i is divisor of both a and b ∗ /
ret = i;
}
return ret;
}
Summary
Today’s lesson we learnt about:
• Blocks and Compound Statements
• Control Flow
• Conditional Statements
• Loops
Thank you Onalethata
Matlhape
www.bothouniversity.com