Anda di halaman 1dari 5

College of Information and Communications Technology (BPSU Main)

COMP-121

IDENTIFIERS, DATA TYPES, OPERATORS AND IO STATEMENTS


Programming Problem:
Write an algorithm and flowchart that will accept price of the product and quantity. Compute and display the bill.

Algorithm

C Program

1.
2.
3.
4.
5.
6.

/*This is a sample program that uses the C Language.*/


#include <iostream.h>
#include <conio.h>

Initialize all variables: x, y, sum.


Read/input x.
Read/input y.
Compute sum where sum = x + y;
Display sum.
Stop

main()
{
int x, y, sum;
clrscr();
cout<<"Enter value of x: ";
cin>>x;
cout<<"Enter value of y: ";
cin>>y;
sum = x + y;
cout<<"\nThe sum of x and y is "<<sum<<".";
getch();
return 0;
}

Identifiers
Simply references to memory locations which can hold values
Formed by combining names to objects in a program
Created to give names to objects in a program
Types of Identifiers
1. Variables

Identifier whose value may change during the course of execution


2. Constant

Identifier whose value may not be changed during the execution of the program
Rules in Naming Identifiers

Must start with a letter


Must not include space or special character
Must only use letters, numbers and underscore
Must not be a reserved word
Must not exceed 255 characters
Be descriptive in naming variable. Name a variable according to what it represents.
Avoid using spaces in between words. You may use the underscore character if space is needed.
Refrain from using dash or any mathematical symbol. The computer will consider it as an operator, treating
your variables as mathematical expression
When a variable is declared, this variable name now will represent the specific data in all locations where the
data is needed.
Be consistent when using upper- and lowercase characters.
Keyword
Also called as reserved words
Have standard, predefined meanings and must be used only for their intended purpose

3rd Hand-out (Midterm)

Page |1

College of Information and Communications Technology (BPSU Main)

COMP-121

Examples of Keyword
do
long
sizeof
for

break
int
typedef
switch

case
float
while
goto

char
struct
else
return

Data types
Type of data that a variable can hold
Data Type

Keyword

integer

int

Range
-32768 to 32767

Examples
-1, -75, 235, 32766

long integer

long

-4294967296 to -4294967295

short integer

short

-128 to 127

2, 120, -10

unsigned

0 to 65535

23, 892

unsigned integer

33000

character

char

0 to 255

floating point

float

Approximately 6 digits of precision

1.73, 8.901234

double

Approximately 12 digits of precision

9.1253678

double Floating point

F, 1, w

Sample Identifiers
Valid

Invalid

return

num1

1num

number_of_students

number of students

AgeYears

Age-Years

Declaring data type and variable


Whatever the data type, all variables must be declared prior to their use
General form of declaration
o type variable_name
Examples
o int sum;
o float amount;
Operators
Symbol that directs the computer to perform certain mathematical or logical manipulations and is usually used
to manipulate data and variables
Basic C Operators
1. Arithmetic operators
used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo
(or remainder).

There are 2 types of arithmetic operators in C:


o Binary Operators
operators that require two operands

3rd Hand-out (Midterm)

Page |2

College of Information and Communications Technology (BPSU Main)

C operation

Operator

Example

Addition

b=a+3

Subtraction

b=a-4

Multiplication

b=a*3

Division

b=a/c

Modulus

b=a%c

COMP-121

The division of variables of type integer will always produce a variable of type integer as the
result.
You could only use modulus (%) operation on integer variables.

Unary Operators
operators that require only one operand
C operation

Operator

Example

Explanation

Positive

a = +3

Negative

b = -4

Increment

++

i++

Equivalent to i = i + 1

Decrement

--

i--

Equivalent to i = i - 1

The following table illustrates the difference between the prefix and postfix modes of the
increment and decrement operator. Assuming we have the following variables declaration.
int R = 10, count=10;
++ Or -- Statement

Equivalent Statements

count

R = count++;

R = count;
count = count + 1

10

11

R = ++count;

count = count + 1;
R = count;

11

11

R = count --;

R = count;
count = count 1;

10

R = --count;

count = count 1;
R = count;

2. Assignment operators
Assignment operators are used to combine the '=' operator with one of the binary arithmetic operators
In the following example, all operations starting from c = 9
Operator

Example

Equivalent Statement

Results

+=

c += 7

c=c+7

c = 16

-=

c -= 8

c=c8

c=1

*=

c *= 10

c = c * 10

c = 90

/=

c /= 5

c=c/5

c=1

%=

c %= 5

c=c%5

c=4

3rd Hand-out (Midterm)

Page |3

College of Information and Communications Technology (BPSU Main)

COMP-121

3. Equalities and relational operators


Used to compare the results of two expressions or operands
Equality Operators:
Operator
==
!=

Example
x == y
x != y

Meaning
x is equal to y
x is not equal to y

Relational Operators:
Operator
>
<
>=
<=

Example
x>y
x<y
x >= y
x <= y

Meaning
x is greater than y
x is less than y
x is greater than or equal to y
x is less than or equal to y

4. Logical operators
Used to return a true or a false value based on the state of the variables
Operator

Description

Example

&&

Called Logical AND operator. If both the operands are (A && B) is false.
non zero (true) then condition becomes true.

||

Called Logical OR Operator. If any of the two


operands is non zero (true) then condition becomes
true.

(A || B) is true.

Called Logical NOT Operator. Use to reverses the


logical state of its operand. If a condition is true then
Logical NOT operator will make false.

!(A && B) is true.

Precedence Operators
The rules specify which of the operators will be evaluated first.
Rank
1
2
3
4
5
6
7
8

Operator
()
++, --, !
*, /, %
+, <, <=, >, >=
==, !=
&&, ||
=, *=, /=, %=, +=, -=

Description
Parenthesis
Unary Operators and Logical NOT
Multiplication, Division, Modulus
Addition, Subtraction
Relational Operators
Equality Operators
Logical AND, Logical OR
Assignment Operators

Associativity
Left to Right
Right to Left
Left to Right
Left to Right
Left to Right
Left to Right
Left to Right
Right to Left

Statement
Smallest executable entity within a programming language
Each line of a program is an individual statement
I/O Statements
1. cout means character out to output device, it is a function that capable of displaying the content of the
identifier to the monitor or screen.
Syntax: cout<<identifiers
Example :
cout<<no_of_students;
cout<<FINAL GRADE = <<fg;
2. cin means character in to a specified identifier, it is a function that capable accepting input values from the
keyboard and assign them to a variable.
Syntax: cin>>identifiers
Example :
cin>>no_of_students;
cin>>x;
3rd Hand-out (Midterm)

Page |4

College of Information and Communications Technology (BPSU Main)

COMP-121

Types of Statement
1. Comment statement
A remark defined as text embedded in a program for documentation purposes
They are used to describe what the program does, who wrote it and when it was written among others
2. Declaration statement
Used to inform the computer about the variables to be used by a program
The computer will perform the following functions:
o allocate memory space for the variables
o associate the address of the locations reserved for the variable name
3. Assignment statement
Used to assign values to variables
4. Control statement
Used to affect the flow of execution of a program

// This is a sample program


Comment Statement
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int x, y, sum;
Declaration Statement
cout<<"Enter the first number: ";
cin>>x;
cout<<Enter the second number: ;
cin>>y;
sum = x + y;
Assignment Statement
cout<<The sum of <<x<< and <<y<< is <<sum<<.;
if (sum>10)
cout<<\n<<sum<< is greater than 10;
else
Control Statement
cout<<\n<<sum<< is less than 10;
getch();
return 0;
}

3rd Hand-out (Midterm)

Page |5

Anda mungkin juga menyukai