SYSC2006_L01_Imperative_programming_intro_C
SYSC2006_L01_Imperative_programming_intro_C
Copyright © 2024
Department of Systems and Computer Engineering, Carleton University
➢ Imperative Programming
➢ Declarative Programming
6
Declarative Programming Paradigm
Programmer declares the properties of the desired result, but
!
not how to compute them. What to do instead of how to do it.
s
o u rse
➢ Functional programming: based on mathematical function
(e.g., Scheme, Lisp)
S C c
SY
➢ Logic programming: uses logic circuits to control how facts
r
h e
and rules about the problems within the system are
t
O
represented or expressed (e.g., Prolog)
7
Imperative Programming Paradigm
Computation specified in terms of:
1. Program’s state: contents of its variables at any point in its execution
2. Statements that change the state.
Programmers instruct the computer on how to change its state.
Imperative Programming Language
➢ Has constructs that control the order in which operations are
performed
➢ Allows side effects: State can be read and modified at different points
in the program's execution
8
Imperative Programming Paradigm
➢ Object-oriented Programming: operations are grouped with the
state they operate on; i.e., objects encapsulate data (state) and
operations (behavior) 4 !
2 00
➢ Typically, an object's state is modified only by the code that is
C
part of the object
SYS
➢ Most (but not all) OO languages are imperative languages
➢ Typical Object Programming Languages: C++, Java, Python, …
0 6 !
S C 20
➢ Procedural Programming: statements are grouped into
procedures/subroutines/functions.
SY
9
Procedural Programming
10
Procedural Programming SYSC 2006!
➢ Statements are grouped into procedures/ subroutines/
functions
➢ Structured constructs (if, while, etc.) that control the
execution of blocks of code within procedures
➢ Typical Procedural Programming Languages: C, Fortran,
COBOL, Python, ...
NOTE: In the literature, "procedural programming" and "imperative
programming" are used synonymously.
Check the name
of the course!
11
Characteristics
➢ Program state
➢ Maintained using variables
➢ Changed using assignment statements
➢ By default, statements are executed sequentially from a
known starting point
➢ Loops: executes a sequence of statements multiple times
➢ Early exit from a loop body; e.g., break statement
➢ Skip execution of remainder of loop body, continue with
next iteration; e.g., continue statement
12
Characteristics
➢ Conditional branch
➢ executes a sequence of statements only if a specified
condition is true
➢ Unconditional branching
➢ jumps (infamous goto statement)
➢ subprograms/functions/procedures
➢ Other control-flow constructs - beyond the scope of this
course
➢ Exceptions, generators, coroutines, etc.
13
Why C for SYSC 2006?
ECOR1041/42 – Python
C Tutor
Visualize C program execution, including memory management
URL: https://pythontutor.com/c.html
18
FROM PYTHON TO C
My first C Program
19
"Hello, world!" in Python
if __name__ == '__main__':
print("Hello, world!") Every C program
will have a main
Or, to be more “fair” to C: function that is
the starting point
def main():
print("Hello, world!")
of the program.
if __name__ == '__main__':
main()
20
"Hello, world!" in C Contains declarations that
must be included in order to
#include <stdio.h> call I/O functions (printf)
#include <stdlib.h>
Contains declarations of
int main(void){ EXIT_SUCCESS and
printf("Hello, world!\n"); EXIT_FAILURE macros
return EXIT_SUCCESS; (and much more)
}
Similar to Python function definition.
Mandatory return and parameter types.
Each C program has a main function.
Program starting point
21
"Hello, world!" in C
printf sends formatted
#include <stdio.h> output to the standard
#include <stdlib.h> output stream (often the
console)
int main(void){
printf("Hello, world!\n");
return EXIT_SUCCESS;
} instructions/statements
end with a semicolon (;)
The value returned by main is
the program's exit status
22
Practice Exercise:
Fahrenheit to Celsius
Print a table of Fahrenheit temperatures and their Celsius
equivalents
0 -17.8
20 -6.7 5 Min to solve it in
40 4.4 Python
... ...
280 137.8
300 148.9
23
Practice Exercise:
Python Solution
Fahrenheit to Celsius
def main():
# Set table’s lower & upper limits, and step size.
lower = 0
upper = 300
step = 20
fahr = lower
#Loop to calculate and print all values
while fahr <= upper:
celsius = 5 / 9 * (fahr – 32)
print(fahr, round(celsius, 1))
fahr = fahr + step
if __name__ == '__main__':
main()
24
Practice Exercise:
C Solution
Fahrenheit to Celsius
#include <stdio.h>
#include <stdlib.h>
int main(void){
//Declare the variables we need
lower = 0;
upper = 300;
step = 20;
26
Practice Exercise:
Fahrenheit to Celsius C Solution
#include <stdio.h>
#include <stdlib.h>
int main(void){
int lower, upper, step;
float fahr, celsius;
lower = 0;
upper = 300;
step = 20;