Upload
ambatar
View
40
Download
0
Tags:
Embed Size (px)
DESCRIPTION
a concept for programing
Citation preview
Chapter 2INTRODUCTORY CONCEPTS
Electrical Engineering Department MOHD NASRI BIN HASHIM
Faculty of Information Technology and Multimedia, 2008/2009
Learning Outcomes:
◊ Understand Constants and Variables◊ Understand Data Types◊ Understand Operators and Expressions
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Keyword
intThe acronym for integer
voidRefer to the function that will
not return any value
case default switch break
for continue float double
return while if do int
Example:
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Identifier
Representing particular name in programmingStore values to be used in programmingRefers to the storage in computer
Standard identifier
User-definedidentifierType
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Special built-in wordsReferred as function name which will called from C library
Standardidentifier
printf() scanf()puts() gets()
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Example..#include <stdio.h>
#include <conio.h>
#define PI 3.142 //define the constant value, PI=3.142
void main()
{
float radius, area;
printf("\nEnter radius: ");
scanf("%f",&radius);
area = PI * radius * radius;
printf("Area = %.2f \n",area);
printf("Press a key to finish.\n");
getch(); //to handle the screen
}
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Name given to the declaration of data to be used in program Refer to the storage nameStore data values/result/output
User-definedidentifier
Constant VariableType
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
User-definedidentifier
Identifiers name can only consists of name, number and underscoreIdentifiers name cannot be started with numbersSymbol cannot be used in identifier nameCannot contains spaces between two identifiers nameIdentifiers name should be uniqueIdentifiers is not case sensitive
RULES
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
UThM DIT1064 Seven_eleven integer
Valid identifiers
8Century BIT 1033 Two*four
‘Sixsense’ void
Invalid identifiers
Identifier Example:
WHY?
WHY?
WHY?WHY?
WHY?
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Name which used to store data valueRefer to name of one cell in computer storageContants value is fixed
Constant
Identifier
How to give name to a constant value?
Follow identifiers rules
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
const data_type const_name = const_value;
Declaration format:
const float pi = 3.142;Reserved word
Data type
1
Constant name
Constant Value
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
#define const_name const_value;
Declaration format:
#define pi 3.142;Reserved word
2
Constant n
ame
Constant value
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Example..
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
#define minimum 0;#define MAX 100;
const int counter = 100;const char alphabet = ‘J’;const float value = 4.5;
Example of constant:
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
data_type variable_name;
Name which used to store data/input valueRefer to the name of one cell in computer storageVariable’s value can be modified/changed during execution
Variable
Declaration Format:
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Declaration Example
int number;
float weight;
char alphabet;
Declaration of a variable number of integer data type.
Declaration of a variable weight offloating point data type.
Declaration of a variable alphabet of character data type.
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Variable/constant declaration example
//Variable and constant declration#include <stdio.h>
int number;float weight;
void main(){ const float pi =3.142;
int bilangan; float berat; char abjad; }
Constant declaration
Variable declaration
Variable declaration
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Variable and constant declaration example:
//Variable and constant declaration#include <stdio.h>
const float pi=3.142;
void main(){ int bilangan, bil, bilang;
float berat, kg; char A; }
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Assigning value to variables
#include <stdio.h>
void main(){
int number = 10; float weight; weight = 60.00;
printf(“Enter the value of number :”); scanf(“%d”,&number);
number = 50.00;}
Initialize a variable
Interactive
Example:
Initialize a variable
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Represents types of data can be stored in computer.Types of data to be stored and used in programming should be informed to the compiler/system
Types
Integer
Floatingpoint
Character
Data Types
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data TypesRepresents any round number with +/- values.Divided into short and long integer.Reserved word for integer – intValid until 5 places of integer number.
Integer
Example:age is used to represent the age of students between 18 and 25 years old. The declaration for thevariable is as follow:
int age;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data TypesRepresents any floating point numbers +/-Reserved word– double /float
Floating number
Example:height is used to represent the student’s height between 150 cm and 180 cm. The declaration for thevariable is as follow:
float height;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data Types
Represents character data.Reserved word – char
Character
Example:gender is used to represent the gender of a student. The declaration for the variable is as follow:
char gender;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Determine whether the following identifiers is valid or invalid. Give reason for invalid cases.
1) Parit Raja
2) 20thCentury
3) int
4) INTEGER
5) _BMW2003
6) Reservedword
7) BIT1033
8) markah_pelajar
9) jam*kredit
10) printf
Exercise:
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Write a suitable variable declaration for each of the following statement:
i. Salary of an employee
ii. Student’s mark for programming subject
iii. ATM pin number
iv. Phone number
v. Price of one item
vi. Bus seat number
vii. Student name
Exercise:
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Given the value of x is 10 and a is 12, find the result of the following equation:
y = 2x + a - 6
Based on the following problem, determine the appropriate
variables can be declared:
Exercise:
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Mrs Leeya needs to determine her students grade for programming subject based on the mark scored duringfinal examination. The ‘A’ grade will be given if the markscored is between 85 to 100. If a student has scored 90 marks, what is the grade should Mrs Leeya give to thestudent?
Based on the following problem, determine the appropriate
variables can be declared:
Exercise:
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Based on the following problem, determine the appropriate
variables can be declared:
Exercise:
A box has height, width and length. Calculate the volume of a box.
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Uncle Degawan wants to buy 5 tins of paint from Cinda’s shop. The price of each tin of the paint is RM 15.60. Calculate the price which Uncle Degawan haveto pay for all the tin of paints he bought.
Based on the following problem, determine the appropriate
variables can be declared:
Exercise:
EC201 Fundamental Programming
Example..
30
Operators and Expressions
Faculty of Information Technology and Multimedia, 2008/2009
Expressions
Combination of Operators and Operands
Example 2 * y + 5
Operands
Operators
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
a) Operand A quantity or function upon which a mathematical or logical operation is performed
EC201 Fundamental Programming
Operators and Expressions
Faculty of Information Technology and Multimedia, 2008/2009
b) Operator An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C has a rich set of operators which can be classified as
EC201 Fundamental Programming
Operators and Expressions
Arithmetic
Relational
Logical
Assignment
Increment and Decrement
Faculty of Information Technology and Multimedia, 2008/2009
i. Arithmetic- All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand, therefore the number 5 when operated by unary – will have the value –5
EC201 Fundamental Programming
Operators and Expressions
Operator Meaning
+ Addition or Unary Plus
– Subtraction or Unary Minus
* Multiplication
/ Division
% Modulus Operator
Faculty of Information Technology and Multimedia, 2008/2009
Example : There are two integer variables a and b. If the starting value of a=7 and b=2, the result will be:
Expression Value
a – b 5
a + b 9
a * b 14
a / b 3
a % b 1
Operators and Expressions
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Integer Arithmetic When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result. Let x = 27 and y = 5 be 2 integer numbers. Then the integer operation leads to the following results.
x + y = ?x – y = ?x * y = ?x % y = ?x / y = ?
In integer division the fractional part is truncated
32221152
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
ii. Relational OperatorOften it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators.
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and ExpressionsGiven below is a list of examples of relational expressions and evaluated values.
6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE
** The result of evaluation of a relational operation is either True (represented by 1) or false (represented by 0).
For example, if a = 7 and b = 5, then a < b yields 0
and a != b yields 1.
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressionsiii. Logical OperatorC has the following logical operators, they compare or evaluate logical and relational expressions.
Operator Meaning
&& AND
|| OR
! NOT
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and ExpressionsLogical AND (&&) This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true. Example
a > b && x = = 10 The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10. Logical OR (||) The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true. Example
a < m || a < n
The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Logical NOT (!) The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression. For example ! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y
Faculty of Information Technology and Multimedia, 2008/2009
The result of logical operation on a and b are summarized as below:-
Operators and Expressions
EC201 Fundamental Programming
Variable Expression
A B A && B A || B !A
T T T T F
T F F T F
F T F T T
F F F F T
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions4. Assignment OperatorsC provides several assignment operators for abbreviating assignment expressions. For example the statementc = c + 3;can be abbreviated with the addition assignment operator += asc += 3;The += operator adds the value of expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator. Any statement of the formVariable = variable operator expression;Where operator is one of the binary operators +, -, *, / or %, can be written in the formVariable operator = expression;Thus the assignment c += 3 adds 3 to c. Table shows the arithmetic assignment operators, sample expressions using these operators and explanations.
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Assignment operator
Sample expression
Explanation Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 13
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d – 4 1 to d
Assignment operator
Sample expression
Explanation Assigns
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions5. Increments and Decrement Operators
Operator Sample expression
Explanation
++ ++a Increment a by 1 then use the new value of a in the expression in which a resides
++ a++ Use the current value of a in the expression in which a resides, the increment a by 1 (Still remain same number)
-- --b Decrement b by 1 the use the new value of b in the expression in which b resides
-- b-- Use the current value of b in the expression in which b resides, then decrement b by 1 (Still remain same number)
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions5. Increments and Decrement Operators
#include <stdio.h>int main(void);{int j = 5, k = 5, l = 5, m = 5;printf("j: %d\t k: %d\n", j, k);printf("j: %d\t k: %d\n", j++, k--);printf("l: %d\t m: %d\n", l, m);printf("l: %d\t m: %d\n", ++l, --m);}
Output:j: 5 k: 5j: 5 k: 5l: 5 m: 5l: 6 m: 4
Faculty of Information Technology and Multimedia, 2008/2009
HIERARCHY OF OPERATOR• The hierarchy of operator precedence form
highest to lowest is summarized below:
Operator Category Operator
Unary - - - + +
Arithmetic multiply, devide, remainder
* / %
Arithmetic add and subtract + -
Relational operators < <= > >=
Equality operators = = ! =
Logical AND &&
Logical OR ||
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Find the answer using the hierarchy of operator.
Use: a=12, b=2, c=3a) X = a % bb) X = a /bc) X = a % b / cd) X = a / b % c
EC201 Fundamental Programming
Exercise:
Example..
Faculty of Information Technology and Multimedia, 2008/2009
EXERCISE 1:
int a=5, b=2, c=3, d=4;
float answer;
answer = (a % c)* (d+b) * d / (a-c);
printf("The answer is : %.3f",answer);
printf("\n");
EXERCISE 2:
int m;int a=100, b=5, c=3, d=2, x=3;
m = a + b * c / d – x++;
printf(The answer is : %d",m);printf(/n);
Example..
Exercise:
EC201 Fundamental Programming
answer=24 m=104
Faculty of Information Technology and Multimedia, 2008/2009
Consider the following example:
2*3+4/2 > 3 AND 3<5 OR 10<9
[2*3+4/2] > 3 AND 3<5 OR 10<9
[[2*3]+[4/2]] > 3 AND 3<5 OR 10<9
[6+2] >3 AND 3<5 OR 10<9
[8 >3] AND [3<5] OR [10<9]
True AND True OR False
[True AND True] OR False
True OR False
True EC201 Fundamental Programming
Example..
Exercise: