COMPUTER PROGRAMMING
I
CSC 203/ Department of Computer Science
Abayomi-Alli Adebayo (Ph.D)
1
COURSE OUTLINE
• Introduction to problem solving methods and Algorithms
• Programming language elements (Syntax, Semantics)
• Variable Declaration, Data Types, Use of Operators (Arithmetic,
Assignment, Increment/Decrement, Relational, Logic, etc)
• Control structures of Algorithms
• Introduction to C Programming language
• Structural program development in C
• C program control (Control structures in C)
• C functions
• C Arrays
• C Pointers
Other Course Details
✔ A tentative set of lecture slides will be posted, modifications may take
place right up to, or during lecture.
✔ Therefore you should download the class slides after a lecture.
Recommended Books:
Schaum’s Series Programming with C.
Course Lecturer: Dr. Abayomi-Alli A.
• Office: 102, Unity Building A.
• Email: abayomiallia at funaab. edu . ng
• Office Hours: Wednesdays 8:00 am to noon
3
CSC 203
COMPUTER PROGRAMMING I
Week Two Lecture
Variable Declaration, Data Types, Use of
Operators (Arithmetic, Assignment,
Increment/Decrement, Relational,
Logic, etc)
VARIABLE DECLARATION
• Variable: a name associated with a memory cell whose value can change.
Its simply a memory storage.
• Variable Declaration: specifies the data type of a variable
• Example: int num;
• Variable Definition: assigning a value to the declared variable
• Example: int num = 5;
CONSTANTS
• Entities that appear in the program code as fixed values.
• Any attempt to modify a CONSTANT will result in error.
• Major types of constants:
• Integer constants
• Positive or negative whole numbers with no fractional part
• Example:
const int MAX_NUM = 10;
const int MIN_NUM = -90;
CONSTANTS
• Floating-point constants (float or double)
• Positive or negative decimal numbers with an integer part, a decimal point
and a fractional part
• Example:
const double VAL = 0.5877e2; (stands for 0.5877 x 102)
const double pi = 3.412;
• Character constants
• A character enclosed in a single quotation mark
• Example:
• const char letter = ‘n’;
• const char number = ‘1’;
BASIC DATA TYPES
• There are 4 basic data types :
• int
• float
• double
• Char
• int
• used to declare numeric program variables of integer type
• whole numbers, positive and negative
• keyword: int
int number;
number = 12;
BASIC DATA TYPES
• float
• fractional parts, positive and negative
• keyword: float
float height;
height = 1.72;
• double
• used to declare floating point variable of higher precision or higher range of
numbers
• exponential numbers, positive and negative
• keyword: double
double valuebig;
valuebig = 12E-3;
BASIC DATA TYPES
• char
• equivalent to ‘letters’ in English language
• Example of characters:
• Numeric digits: 0 - 9
• Lowercase/uppercase letters: a - z and A - Z
• Space (blank)
• Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
• single character
• keyword: char
char my_letter;
my_letter = 'U';
Qualifiers: short, long, unsigned, signed, const
Arithmetic Operators
The major operators in any programming language are + (Plus), - (Minus), *
(Multiplication), /(Division)
Others include:
• Increment: a++
• Example
• int a=5;
• a++ =6;
• Modulus (remainder): %
• Example:
• 12%5 = 2;
Arithmetic Operators
• Assignment by addition: +=
• Example:
• int a=4;
• a+=1; (means a=a+1) value of a becomes 5
• Comparison Operators: <, > , <=, >= , != (not equal), ==, ! (logical not), &&
(logical and), ||(logical or) .
• Example:
• int a=4, b=5;
• a<b returns a true value.
• Bitwise Operators: << (shift left), >>(shift right), ~(bitwise not), & (bitwise and),
|(bitwise or) ,^ (bitwise xor).
• Example
• int a=8;
• a= a>>1; // value of a becomes 4
Operator Precedence
• Meaning of a + b * c ?
Is it a+(b*c) or (a+b)*c ?
• All operators have precedence over each other
• *, / have more precedence over +, - .
• If both *, / are used, associativity comes into picture. (more on this later)
• Example :
• 5+4*3 = 5+12= 17.
Precedence Table
Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>
< >
&
&&
||
THE COMMA OPERATOR
The operator enables you to put more than one expression on a single line by
separating each one with a comma.
e.g int i = 10; j = 25;
CONTROL STRUCTURES OF
ALGORITHMS
CONTROL STRUCTURES OF ALGORITHMS
• What kinds of control structures are necessary in order to describe algorithms?
(1) Sequence: This structure is the most elementary structure. The sequence
structure is a case where the steps in an algorithm are constructed in such a way
that, no condition step is required.
• For example, suppose you are required to design an algorithm for finding the
average of Six numbers, and the sum of the numbers is given.
Algorithm:
1. Start
2. Input Sum
3. Average = Sum / 6
4. Output Average
5. Stop
CONTROL STRUCTURES OF ALGORITHMS
2) Selection: The selection structure also known as decision structure is a case
in the algorithm where one has to make a choice of two alternatives by
making a decision depending on a given condition.
• A selection structure takes the form:
If condition is true
Then do task A
else
Do TaskB
CONTROL STRUCTURES OF ALGORITHMS
The selection requires the following:
• Choose alternative actions as a result of testing a logical condition
• Produce code to test a sequence of logical tests
• In making choices, IF statement is used together with logical operators to test for
true or false.
The logical operators used are:
• = is equal to <= is less than or equal
• > is greater than <> is not equal to
• < is less than
• >= is greater than or equal
IF STATEMENT
• The If statement appears as follows:
if (condition)
{
statements;
}
-The statement is executed only if Condition is True
-If condition is false, the statement is ignored and the next appropriate statement
is simply executed.
e.g
If (sales > 5000)
{
bonus=500;
}
THE IF-ELSE STATEMENT
The If-else statement never appears in a program without the If statement. The
syntax is:
if (condition)
{
statements;
}
else
{
statements;
}
E.g
target *=1.10;
}
else
{
target*=0.90;
}
CONTROL STRUCTURES OF ALGORITHMS
(3) Iteration: this requires repeating a block of instructions until a logical
event is verified or not.
• The iteration structure can be implemented using
• Repeat Until Loop • The While Loop • The For Loop
• Any program instruction that repeats some statement or sequence of
statements a number of times is called an iteration or a loop. The
commands used to create iterations or loops are all based on logical tests.
CONTROL STRUCTURES OF ALGORITHMS
• Repeat Until Loop
The syntax is
REPEAT
A statement or block of statements
UNTIL
a true condition
• For example, A program repeatedly asking for an entry of a number in the range 1
to 100 until a valid number is entered.
REPEAT
Output “Enter a number between 1 and 100”
UNTIL
number >= 1 OR number <= 100
CONTROL STRUCTURES OF ALGORITHMS
THE WHILE LOOP
The second type of iteration. This type of conditional loop tests for terminating condition at the beginning of the
loop.
• The syntax is:
WHILE (a condition is true)
A statement or block of statements
ENDWHILE
The while loop enables your programs to repeat a series of statements, over and over, as long as a certain condition
has not been met. The format is:
while (condition)
{
statements;
}
CONTROL STRUCTURES OF ALGORITHMS
The For Loop: The third type of iteration. This, in its simplest form, uses an
initialisation of the variable as a starting point, a stop condition depending on
the value of the variable. The variable is incremented on each iteration until it
reaches the required value.
The syntax is:
FOR (starting state, stopping condition, increment) Statements
ENDFOR
• For example:
FOR (n = 1, n <= 4, n++)
Output n
ENDFOR
FOR LOOP Cont’d
For (initialization; stopping condition; increment/decrement)
e.g
For (j=2; j<=80; j+=5)
For (i=10; i>=1; i--)
For (j=99; j>=0; j-=11)
• THE DO-WHILE LOOP
It is similar to the while loop except the test condition occurs at the end (rather than the
beginning) of the loop. The syntax of do-while is:
do
{
statements
}
while (condition)
CONTROL STRUCTURES OF ALGORITHMS