1. Introduction to Programming concepts LICSTA.pdf

FoghoIvo 17 views 41 slides Jun 04, 2024
Slide 1
Slide 1 of 41
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41

About This Presentation

Programming concepts both in C programming and html


Slide Content

LICSTA SEMINAR 2022
Introduction to Programming
BY MEH PAULINUSMEH P.
Friday, August 26, 2022
1

Day 1-->Basic Notions and Environments
Friday, August 26, 2022 2
BRIEF CONTENT
Day 2-->Abstraction and Decomposition
Day 3-->Workshop

❑Review of Programming and PL
❑Programming Environments
❑Program Development Life Cycle
❑Core Programming Concepts in C language.
Friday, August 26, 2022 3
Day 1-Basic Notions and Environments

❑Decomposition problem solving approach
✓Subroutines
✓Procedures
✓Functions
✓Recursive function
Friday, August 26, 2022 4
Day 2-Abstraction and Decomposition

Day 3-->Miscellaneous Exercises
❑Problems including use of arrays
❑Problems with arrays as function arguments
❑Problems involving recursion
❑Electricity Bill problem (CSC past question)
❑Past Questions AL Computer Science / ICT
Friday, August 26, 2022 5

LET’s Get Started
Friday, August 26, 2022 6
WELCOME TO DAY 1

❑Review of Programming and PL
❑Programming Environments
❑Program Development Life Cycle
❑Core Programming Concepts in C language.
Friday, August 26, 2022 7
Day 1-Basic Notions and Environments

Review of Programming and Programming languages
❑Informally programming is an activity of writing computer programs.
❑Technically, it is the implementation of logic (algorithm) to facilitate specified
computing operations
❑Simply put, expressing an algorithm in a programming language.
❑Programming language is a collection of primitives, rules governing how they are
combined and the meaning attributed to them.
Friday, August 26, 2022 8

❑Review of Programming and PL
❑Programming Environments
❑Programming Development Life Cycle
❑Core Programming Concepts in C language.
Friday, August 26, 2022 9
Day 1-Basic Notions and Environments

Programming Environments
❑Programming environments combines hardware and software that
allows coding.
❑The environments differ from programming languages. An
environment consists of the following
✓A text editor
✓A compiler
✓A debugger
❑An IDE consists of these three integrated in one. E.g. For C
◦Code blocks
◦Dev C++
◦Pelles C
Friday, August 26, 2022 10

❑Review of Programming and PL
❑Programming Environments
❑Program Development process
❑Core Programming Concepts in C language.
Friday, August 26, 2022 11
Day 1-Basic Notions and Environments

Program Development Process
Friday, August 26, 2022 12
Editor
A real Computer
Compiler
Algorithm
input
Program in machine’s language
Program in programming language (source code)

❑Review of Programming and PL
❑Programming Environments
❑Program Development Life Cycle
❑Core Programming Concepts in C language.
Friday, August 26, 2022 13
Day 1-Basic Notions and Environments

Core Programming Concepts in C language.
Tips to remember!
❑C programming language is an imperative high-level language.
❑C programming language is an explicitly typed language (Data types must
be defined for each variable)
❑C programming language is case-sensitive e.g. Sum is not sum and not SUM
❑A block is enclosed by {}
Friday, August 26, 2022 14

1. Data types
❑Data type refers to a set of values together with a set of
permissible operations on them.
Friday, August 26, 2022 15
S/N Data type Example Format specifier
1 Integer int(9, -5) %d
2 Float float(4.5, -7.3) %f
3 String (cat) %s
4 Character char(M, 3,) %c

2. Variables
❑Avariable is a named memory location. Use to hold values.
❑A variable can hold only ONE value at a time.
Friday, August 26, 2022 16
50 12.5
Score Quantity
score

❖Variable Declaration. Indicating to the compiler the name of a variable and the type
of data it can hold. Has the format <<data_type nameOfvariable; >>
❖e.g. int age;
❖Variable Initialization. Specifying an initial value for a variable so that it can have a
defined value.
❖e.g. age= 0;
❖Variable Assignment. Giving a variable a value.
❖E.g. age = 15
Friday, August 26, 2022 17

Brainstorm
// evaluate this and comment on the output
int count;
count = count+ 5;
printf(“The value of count is: %d”,count);
Friday, August 26, 2022 18

Scope of a variable.
❑It refers to the limit to which a variable’s value is accessible in a
program.
A.Local scope
l and w are local variables
Friday, August 26, 2022 19
int area () {
int l, w,
l = 59;
w = 12;
return l*w;
}

Global scope
Friday, August 26, 2022 20
int tax = 5%;
int t_cost() {
int up, qty;
up = 2500;
qty = 12;
return up*qty*tax;
}

3. Operators in C
Friday, August 26, 2022 21

4.Control structures
1. Conditional structure, can be implemented using
❑Simple if
❑If else (the use of ternary operator)
❑Nested if
2. Loops, implemented using
❑while loop
❑Do while loop
❑For loop
Friday, August 26, 2022 22

Simple if
Executes statement(s) only when the condition is True or False NOT Both.
We wish our program to double any given number provided it is even and
print the square of the doubled number.
if (a % 2 == 0)
{
a = a+a;
printf(“The result is: %d”, a*a);
}
Friday, August 26, 2022 23

If Else statement
Executes statement(s) when the condition is True and also executes
statement(s) when the condition is False
Friday, August 26, 2022 24
If (mark >=10)
{
printf(“Congratulations!, You made it with an average of %.2f”, mark);
}
else
{
printf(“Sorry You didn’t make it, your average is %.2f”, mark);
{

Using Ternary Operator
Using the if else statement
If (a > b)
printf(“%d is max”,a)
else
printf(“%d is max”, b)
Using the Ternary Operator
printf("%d is max", (a > b)? a:b);
Friday, August 26, 2022 25

Nested If
❖An if statement inside another if statement.
❖Often used when multiple conditions are to be tested.
❖The nesting can be in the True or False statements
❖!! Be careful not to get a spaghetti problem.
Friday, August 26, 2022 26

Flowchart for nested if.
Friday, August 26, 2022 27

5. Loops
❑Loops are programming elements that repeat a portion of code a set number
of times until the desired process is complete.
❑A loop consists of:
▪Loop condition (expression to be evaluated before loop body is executed)
▪Loop body (statements to be executed if loop condition is met)
▪Loop terminator (statement leading loop to end. )
▪Increment , decrement, break
Friday, August 26, 2022 28

While Loop
In a while loop, the loop condition is evaluated first, if the condition is met, the loop body is
executed then the expression leading to loop exit.
n = 10;
While (n > 0){
printf(“I executed when n was %d”, n);
n = n-1; //expression leading to loop exit
}
Friday, August 26, 2022 29

Do while loop
❑It executes the loop body at least once before evaluating the
condition.
int a = 22;
do {
printf(“The value of a: %d\n”,a);
a = a +1;
} while ( a < 20);
Friday, August 26, 2022 30

For loop
❑Suitable for use when you know how many times to repeat a block
of code.

❑Frequently used to traverse data structures such as arrays, link list.
Friday, August 26, 2022 31

5. Data structures.
❑A lot learned from previous presenter on DS and Algorithm
❑Here we look at implementation in C language.
Friday, August 26, 2022 32

a. Arrays
❑As a block of memory, it must be declared
❑Several homogenous elements stored in a single block
❑A block can hold several elements, each reference by an index
❑An array is static data structure (size can not be change at run time)
Friday, August 26, 2022 33

Array Declaration
Datatype arrayName[size]
E.g.
int eNum [10];
Friday, August 26, 2022 34

Reading through array elements
for (int i= 0; i< 10; i++){
printf(“Element at index %d is %d “, i, eNum[i]);
{
Friday, August 26, 2022 35

Nested for loop
Friday, August 26, 2022 36

STRUCTURE OF A C PROGRAM
1.Documentation
2.Link Section
3.Definition section
4.Global Declaration section
5.Main function
6.Subprogram section
Friday, August 26, 2022 37

Documentation section: Is part of the program where the details
associated with the program are given, such as the name, author. It
gives an overview of the program. This is usually a comment.
Link Section: Use to declare all header files that will be used in the
program to enable the compiler link the header files to the libraries
Definition section:Use to define different constants, keyword.
Global Declaration section: Consist of global declarations
Friday, August 26, 2022 38

The Main Function Section: Every program in c needs to have the
main function. This is where program execution begins.
The main function has two parts.
◦Declaration part
◦Execution part
Subprogram section: Body or implementation details of
functions/subroutines.
Friday, August 26, 2022 39

Example.
Write a c program to find the area of any circle using a user-defined function with a called
variable defined as PI
Friday, August 26, 2022 40

WORKSHOP
Friday, August 26, 2022 41