Professional Documents
Culture Documents
LESSON 3 Notes
LESSON 3 Notes
LESSON 3 Notes
CHAPTER THREE
Structured Programming, Pseudocode, • Pseudocode is structured english that is used as
an alternative method to flowcharts for planning
syntax, semantics, data types, structured programs.
expressions, statements, variable, input-
• Pseudo-code instructions are written in English,
output, control structures
• they can be easily understood and reviewed by
users.
• The only syntax rules to be concerned with
involve the LOOP and SELECTION structures.
• They must be used as CAPITALISED words.
• Write a pseudocode that would solve the area of a • In order to have a basic understanding of
parallelogram shown below programming methodology, we shall use C
6 cm
programming language.
• C is a general-purpose, procedural, imperative
4 cm
3 cm
computer programming language developed in 1972
by Dennis M. Ritchie at the Bell Telephone
Laboratories to develop the UNIX operating system.
C is the most widely used computer language.
•4 C is a powerful system programming language,
Lecturesand
on
Numerical
C++ is an excellent general purpose programming
Methods
1
Tokens in C Tokens in C
• Keywords • String Literals
• These are reserved words of the C language. For • A sequence of characters enclosed in double
example int, float, if, else, for, quotes as “…”. For example “13” is a string literal
while etc. and not number 13. ‘a’ and “a” are different.
• Identifiers • Operators
• An Identifier is a sequence of letters and digits, but • Arithmetic operators like +, -, *, / ,% etc.
must start with a letter. Underscore ( _ ) is treated • Logical operators like ||, &&, ! etc. and so on.
as a letter. Identifiers are case sensitive. Identifiers
are used to name variables, functions etc. • White Spaces
• Valid: Root, _getchar, __sin, x1, x2, • Spaces, new lines, tabs, comments ( A sequence of
x3, x_1, If characters enclosed in /* and */ ) etc. These are
• Invalid: 324, short, price$, My Name
Lectures on
used to separate the adjacent identifiers,Lectures
kewords on
5 Numerical 6 and constants. Numerical
• Constants Methods Methods
2
Constants
Basic Data Types • Numerical Constants
• Constants like 12, 253 are stored as int type.
• Floating Point Numbers No decimal point.
• Floating point numbers are rational numbers. • 12L or 12l are stored as long int.
Always signed numbers.
• 12U or 12u are stored as unsigned int.
• float Approximate precision of 6 decimal digits .
• 12UL or 12ul are stored as unsigned long int.
• Typically stored in 4 bytes with 24 bits of signed
mantissa and 8 bits of signed exponent. • Numbers with a decimal point (12.34) are stored as
• double Approximate precision of 14 decimal double.
digits. • Numbers with exponent (12e-3 = 12 x 10-3 ) are
• Typically stored in 8 bytes with 56 bits of signed stored as double.
mantissa and 8 bits of signed exponent. • 12.34f or 1.234e1f are stored as float.
Lectures on • These are not valid constants: Lectures on
9 • One should check the file limits.h to what Numerical
is 10 Numerical
implemented on a particular machine. Methods 25,000 7.1e 4 $200 2.3e-3.4 etc.
Methods
Constants
• Character and string constants Variables
• ‘c’ , a single character in single quotes are stored
as char.
Some special character are represented as two • Naming a Variable
characters in single quotes.
‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’
• Must be a valid identifier.
= double quotes. • Must not be a keyword
Char constants also can be written in terms of their • Names are case sensitive.
ASCII code.
‘\060’ = ‘0’ (Decimal code is 48). • Variables are identified by only first 32 characters.
• A sequence of characters enclosed in double • Library commonly uses names beginning with _.
quotes is called a string constant or string literal. For • Naming Styles: Uppercase style and Underscore
example style
“Charu”
“A” Lectures on • lowerLimitlower_limit Lectures on
11 Numerical 12 Numerical
“3/9” Methods • incomeTax income_tax Methods
“x = 5”
3
Declarations Global and Local Variables
/* Compute Area and Perimeter of a circle */
#include <stdio.h>
• Declaring a Variable
• Global Variables
float pi = 3.14159; /* Global */
scanf(“%f” , &rad);
• Declaration announces the data type of a variable and allocates • Life time of a global
appropriate memory location. No initial value (like 0 for integers) should be variable is the if ( rad > 0.0 ) {
}
any function defined
• Examples else
below the printf( “Negative radius\n”);
int sum = 0; declaration, in a
file. printf( “Area = %f\n” , area );
char newLine = ‘\n’; Lectures on }
Lectures on
13 Numerical 14 Numerical
float epsilon = 1.0e-6; Methods Methods
Global and Local Variables /* Compute Area and Perimeter of a circle */ Operators
#include <stdio.h>
float pi = 3.14159; /* Global */
• Arithmetic Operators
• Local Variables
main() {
float rad; /* Local */
• +, - , *, / and the modulus operator %.
• These variables are
declared inside some
functions.
printf( “Enter the radius “ );
scanf(“%f” , &rad);
• + and – have the same precedence and associate left to
• Life time of a local right.
variable is the entire
3 – 5 + 7 = ( 3 – 5 ) + 7 3 – ( 5 + 7 )
if ( rad > 0.0 ) {
execution period of float area = pi * rad * rad;
the function in which
float peri = 2 * pi * rad;
it is defined. 3 + 7 – 5 + 2 = ( ( 3 + 7 ) – 5 ) + 2
• Cannot be accessed by
any other function.
printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri );
• *, /, % have the same precedence and associate left to
• In general variables } right.
declared inside a
block are accessible
else
• The +, - group has lower precendence than the *, / %
only in that block. printf( “Negative radius\n”);
group.
printf( “Area = %f\n” , Lectures
area ); on Lectures on
15 } Numerical 16 Numerical
Methods Methods
4
Operators Operators
• Arithmetic Operators
• % is a modulus operator. x % y results in the
3 – 5 * 7 / 8 + 6 / 2 remainder when x is divided by y and is zero when x
3 – 35 / 8 + 6 / 2 is divisible by y.
• The operands of a binary operator must have a the In case the two operands of a binary operator are different, but
same type and the result is also of the same type. compatible, then they are converted to the same type by the
compiler. The mechanism (set of rules) is called Automatic
• Integer division: Type Casting.
c = (9 / 5)*(f - 32) c = (9.0 / 5)*(f - 32)
It is possible to force a conversion of an operand. This is called
The operands of the division are both int and hence Explicit Type casting.
the result also would be int. For correct results, one c = ((float) 9 / 5)*(f - 32)
may write
c = (9.0 / 5.0)*(f - 32)
Lectures on
19 Numerical
Methods
5
Automatic Type Casting Explicit Type Casting
1. char and short operands are converted to int • The general form of a type casting operator is
2. Lower data types are converted to the higher data • (type-name) expression
types and result is of higher type.
• It is generally a good practice to use explicit casts than to
3. The conversions between unsigned and signed types Hierarchy rely on automatic type conversions.
may not yield intuitive results. Double • Example
4. Example float C = (float)9 / 5 * ( f – 32 )
float f; double d; long l;
long • float to int conversion causes truncation of fractional
int i; short s;
Int part
d + f f will be converted to double
Short and • double to float conversion causes rounding of digits
i / s s will be converted to int
char
Lectures on • long int to int causes dropping of the higher
Lecturesorder
on
21 l / i i is converted to long; long result Numerical 22 Numerical
Methods bits. Methods
Lectures on Lectures on
23 Numerical 24 Numerical
Methods Methods
6
Operators
Operators
• Logical Operators
• Relational Operators • &&, || and ! are the three logical operators.
• <, <=, > >=, ==, != are the relational operators. The • expr1 && expr2 has a value 1 if expr1 and
expression expr2 both are nonzero.
operand1 relational-operator operand2 • expr1 || expr2 has a value 1 if expr1 and
takes a value of 1(int) if the relationship is true and 0(int) if expr2 both are nonzero.
relationship is false. • !expr1 has a value 1 if expr1 is zero else 0.
• Example
• Example
int a = 25, b = 30, c, d;
• if ( marks >= 40 && attendance >= 75 )
c = a < b; grade = ‘P’
d = a > b;
25
Lectures on
Numerical 26
• If ( marks < 40 || attendance Lectures
< 75 on)
Numerical
value of c will be 1 and that of d will be 0 . Methods grade = ‘N’ Methods
Operators
Operators • Increment and Decrement Operators
• The operators ++ and –- are called increment and
• Assignment operators decrement operators.
• The general form of an assignment operator is • a++ and ++a are equivalent to a += 1.
• v op= exp • a-- and --a are equivalent to a -= 1.
• Where v is a variable and op is a binary arithmetic • ++a op b is equivalent to a ++; a op b;
operator. This statement is equivalent to • a++ op b is equivalent to a op b; a++;
• v = v op (exp) • Example
• a = a + b can be written as a += b Let b = 10 then
• a = a * b can be written as a *= b (++b)+b+b = 33
7
Floating Point Arithmetic Floating Point Arithmetic
• Representation • Representation
• All floating point numbers are stored as 0.d1d 2 d p B
e
EXERCISE 3
a) Define the following terms citing example in each case.
i) Syntax (2 mark)
ii) Semantics (2 mark)
iii) Variable declaration (2 mark)
iv) Data type (2 mark)
v) Identifier (2 mark)
b) Rewrite the following mathematical statement in their equivalent
C statements:
i) c = 5 (f-32)
6 [1 Mark]
ii) T=3x2 + y
2 2
( x +100)(x ) [3 Marks]