Professional Documents
Culture Documents
Introductory Concepts: Electrical Engineering Department Mohd Nasri Bin Hashim
Introductory Concepts: Electrical Engineering Department Mohd Nasri Bin Hashim
Chapter 2
INTRODUCTORY CONCEPTS
Learning Outcomes:
Understand Constants and Variables Understand Data Types Understand Operators and Expressions
void
int
case
default
switch
break
for
continue
return while
float
if
double
do int
Standard identifier
Type
User-defined identifier
Example..
#include <stdio.h> #include <conio.h> #define PI 3.142 //define the constant value, PI=3.142
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
User-defined identifier
Constant
Type
Variable
Invalid identifiers
8Century BIT 1033 Sixsense WHY? WHY?
Reserved word
const
float pi = 3.142;
Constant Value
Reserved word
#define pi 3.142;
Example..
//Variable and constant declration #include <stdio.h> int number; float weight;
#include <stdio.h> void main() { int number = 10; float weight; weight = 60.00;
Initialize a variable
Data Types
Data Types
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 Floating point
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009
Character
Data Types
Data Types
Integer
Represents any round number with +/values. Divided into short and long integer. Reserved word for integer int Valid until 5 places of integer number.
age is used to represent the age of students between 18 and 25 years old. The declaration for the variable is as follow:
Example:
int age;
Data Types
Data Types
Floating number
Represents any floating point numbers +/Reserved word double /float
height is used to represent the students height between 150 cm and 180 cm. The declaration for the variable is as follow:
Example:
float height;
Data Types
Data Types
Character
Represents character data. Reserved word char
gender is used to represent the gender of a student. The declaration for the variable is as follow:
Example:
char gender;
Example..
Determine whether the following identifiers is valid or invalid. Give reason for invalid cases.
1)
2) 3) 4) 5) 6) 7)
Parit Raja
20thCentury int INTEGER _BMW2003 Reservedword BIT1033
8)
9) 10)
markah_pelajar
jam*kredit printf
Example..
Write a suitable variable declaration for each of the following statement: i. ii. iii. iv. v. Salary of an employee Students mark for programming subject ATM pin number Phone number Price of one item
vi.
vii.
Example..
Based on the following problem, determine the appropriate
variables can be declared:
Given the value of x is 10 and a is 12, find the result of the following equation:
y = 2x + a - 6
Example..
Based on the following problem, determine the appropriate
variables can be declared:
Mrs Leeya needs to determine her students grade for programming subject based on the mark scored during final examination. The A grade will be given if the mark scored is between 85 to 100. If a student has scored 90 marks, what is the grade should Mrs Leeya give to the student?
Example..
Based on the following problem, determine the appropriate
variables can be declared:
A box has height, width and length. Calculate the volume of a box.
Example..
Based on the following problem, determine the appropriate
variables can be declared:
Uncle Degawan wants to buy 5 tins of paint from Cindas shop. The price of each tin of the paint is RM 15.60. Calculate the price which Uncle Degawan have to pay for all the tin of paints he bought.
30
Expressions
Combination of Operators and Operands
Operators
Example
2*y+5
Operands
Arithmetic
Logical
Relational
Assignment
*
/ %
Multiplication
Division Modulus Operator
Operator < <= > >= is less than is less than or equal to is greater than
Meaning
==
!=
is equal to
is not equal to
** 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.
Operator && || !
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 EC201 Fundamental and Multimedia, Programming 2008/2009
10 to c 1 to d
Assignment operator *= /= %=
Sample expression e *= 5 f /= 3 g %= 9
Assigns 20 to e 2 to f 3 to g
++
a++
--
--b
--
b--
#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: 5 j: 5 k: 5 l: 5 m: 5 l: 6 m: 4
HIERARCHY OF OPERATOR
The hierarchy of operator precedence form highest to lowest is summarized below:
Operator Category Unary Arithmetic multiply, devide, remainder Arithmetic add and subtract Relational operators Equality operators Logical AND Logical OR Operator - -- ++ * / %
+ < <= > >= == !=
&& ||
Example..
Find the answer using the hierarchy of operator. Use: a=12, b=2, c=3 a) X = a % b b) X = a /b c) X = a % b / c d) X = a / b % c
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009
Example..
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);
answer=24
m=104
Example..
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
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009