COMP-121
Algorithm
C Program
1.
2.
3.
4.
5.
6.
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 not be changed during the execution of the program
Rules in Naming Identifiers
Page |1
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
1.73, 8.901234
double
9.1253678
F, 1, w
Sample Identifiers
Valid
Invalid
return
num1
1num
number_of_students
number of students
AgeYears
Age-Years
Page |2
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
Page |3
COMP-121
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.
||
(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
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
Page |5