You are on page 1of 51

Review of

Computer Programming 1

Week 1-2

Mark G. De Vera
Outline for this lesson
Review of Computer Fundamentals
- Data types
- Variables
- Arithmetic, logical and relational
operations
- Control Structures
- Functions

Mark G. De Vera
Goal
Review the essentials of C Language
and be able to assess student’s
understanding of Basic C programming

Mark G. De Vera
Data Types
char
the character data type.
The char data type is used to
represent/store/manipulate character
data values.
A char data type value requires one
byte of memory space.
The range of values that can be
assumed by a char value is from 0 to
255.
The number-to-character coding that is
Mark G. De Vera
Data Types
int
the integer data type.
The int data type is used to
represent/store/manipulate signed
whole numeric values.

Mark G. De Vera
Data Types
float
the single precision floating point
data type.
The float data type is used to
store single precision signed real
numbers (i.e., those numbers with
fractional components).

Mark G. De Vera
Data Types
double
the double precision floating point
data type.
The double data type is used to
store double precision signed real
numbers.

Mark G. De Vera
Notes on Data Types
The amount of memory space required
to store an int, a float and double is
platform-dependent. (depends on the
machine and the software).
Note that a char data is actually
numeric (from 0 to 255), and is treated
as a subset of int values. Any
operation on integer values can also be
performed on characters.

Mark G. De Vera
Variable Declaration
A variable declaration is an
“action” by which a variable is
“introduced” to a
program/function.
All variables in a C program must
be declared. If you forgot to do so,
the compiler will report a syntax
error.
Mark G. De Vera
Declaring A Variable
In C, the syntax for declaring a variable is as
follows:
data_type variable_name;
A semicolon signifies the end of a declaration.
A missing semicolon will cause the compiler to
generate a syntax error.
Variables should be named following the C
naming conventions.
Example:
 char ch1, ch2;

 int x, y, z;

 float hourly_rate, number_of_hours,

salary; Mark G. De Vera
Can I use any name for the
variables?

- YES, as long as you follow the
naming conventions, and do not
use the reserved words in C.
- It is recommended, however, as a
good programming practice for
you to use a name that is
descriptive or suggestive.

Mark G. De Vera
What is the value of the variable
initially?

GARBAGE

Mark G. De Vera
What are operators?
Operators are symbols
representing operations that can
be performed on constants and
variables.
Different sets of OPERATORS:
 Assignment
 Arithmetic
 Logical
 Operational
Mark G. De Vera
Assignment Operators
How do you perform an assignment
operation in C?
Symbol used is an equal sign (=)
It is used to assign/store values to a variable.
It should be terminated by a semicolon (;)

int x;
char choice;

x=25; /*25 is assigned to x*/
choice=a; /*a is assigned to choice*/
Mark G. De Vera
Assignment Operators
Note that it is possible to assign a value of
a variable to another variable of a
compatible data type:

ch1 = ‘Z’;
ch2 = ch1;
x = 5;
y = x;
z = x;

Mark G. De Vera
What will happen if I assigned a
double to an int?

The data type will be converted (either demoted or
promoted). In general, one that requires smaller
memory will fit into one that requires big memory. The
opposite will result into loss of information.

Hierarchy of Data Types
- char
- int
- float
- double

- Actual size in bytes can be seen by using the sizeof
keyword

Mark G. De Vera
Rules on DT Conversion
char to int/float/double assignment:
the char value will be converted
automatically to an int/float/double
value.

Example:
char ch;
int i;
float f;
double d;
ch = ‘A’;
i = ch; /* value of i will be 65 (since 65 is the
ASCII of ‘A’) */
f = ch; /* value of fMarkwill
G. De Vera
be 65.000000 */
Rules on DT Conversion
2. int to char assignment: if the int value is
within the possible range of char values then
that value is assigned; otherwise the behavior
is undefined

Example
char ch;
int i;
i = 65;
ch = i; /* value of ch will be 65
or ‘A’ */
i = 299;
ch = i; /* Mark
noteG. De Vera
that a char can
Rules on DT Conversion
3. float/double to char assignment: the fractional
part will be discarded; if the whole number is within
the possible range of char values then that value is
assigned; otherwise the behavior is undefined

Example:
char ch;
float f;
double d;
f = 65.123;
ch = f; /* value of ch will be 65 or ‘A’ */
d = 90.908765;
ch = d; /* value of ch will be 90 or ‘Z’ */
ch = 1000.89 /* behavior is undefined */
Mark G. De Vera
Rules on DT Conversion
4. int to float/double assignment: the int
value will be converted automatically to
float/double value

Example
int i;
float f;
double d;
i = 123;
f = i; /* value of f will be 123.000000
*/
d = i; /* value of d will be 123.000000
*/ Mark G. De Vera
Rules on DT Conversion
5. float/double to int assignment: the fractional part
will be discarded, the whole part will be assigned to the
integer if and only if the whole part is within the range
of possible int values; otherwise the behavior is
undefined

Example
int i;
float f;
double d;
f = 135.66;
i = f; /* value of i will be 135 */
d = -49.991234;
i = d; /* value of I will be –49 */
i = 999999999999.99 /* very
Mark G. De Vera large number!
Rules on DT Conversion
6. float to double assignment: the
floating point value will be converted
automatically to double

Example
data type
float f;
double d;
f = 8974.234;
d = f; /* value of d will be
8974.234 */ Mark G. De Vera
Rules on DT Conversion
7. double to float assignment: if the value is
within the range of possible floating point
values then a less precise value is assigned;
otherwise, the behavior is undefined

Example
float f;
double d;
d = -875.39391234;
f = d; /* value of f will be 875.393912
*/
f = 9999999999999999.999 /* very large!
behavior is undefined */
Mark G. De Vera
General Rule on DT
Conversion
As a general rule, something that is
small (say a cup) will fit into
something that is big (say a
pitcher). The opposite, does not
hold true but will result into what is
called as OVERFLOW.
(Imagine pouring a pitcher full of
water into an empty cup!).
Mark G. De Vera
Arithmetic Operations
Basic Arithmetic Operations
+ Addition Yields the sum
- Subtraction Yields the
* Multiplication difference
Yields the product
/ Division Yields the
% Modulus Division quotient
Yields the
remainder

Mark G. De Vera
Relational Operations
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal

Results to 1 (true) or 0 (false)
Mark G. De Vera
Logical Operations
! Logical NOT
&& Logical AND
|| Logical OR

0&&0 0 0||0 0
!0 1
0&&1 0 0||1 1
!1 0
1&&0 0 1||0 1
1&&1 1 1||1 1

Mark G. De Vera
Control Structures
specify the sequence of execution
of a group of statements.
Three different type of control
structures
 Sequential
 Conditional
 Loop

Mark G. De Vera
Sequential Control
Structure
A sequential control structure is
organized such that statements are
executed in sequence, i.e., one after the
other in the order of their appearance in
the source code.
Example:
a = 1; /* first statement
*/
b = 2; /* second statement */
c = a + b; /* third statement
Mark G. De Vera
Conditional Control
Structure
The conditional control structure
allows the program to make
choices depending on a condition.
Two types of conditional structures
 if statement (including if-else and
nested if)
 switch case statement

Mark G. De Vera
If Statement
if (<expression>)
<statement>

The value of <expression> is first
evaluated, if it is non-zero, then the
condition is specified as true. If it is
evaluated as zero, then the condition is
specified as false. If the condition is
true, then the <statement> is
executed.
Mark G. De Vera
If Statement Example
void main(void)
{
int n;
printf(“Input an integer value
n: ”);
scanf(“%d”, &n);
if (n >= 0)
printf(“n = %d is
POSITIVE\n”, n);
} Mark G. De Vera
If - Else Statment
if
(<expression>)

<statement-1>
[else

<statement-2>]

The value of <expression> is first evaluated, if
it is non-zero, then the condition is specified as
1 (for true) and <statement-1> is executed.
Otherwise if it is evaluated
Mark G. De Vera as zero, then the
If-Else Example
#include <stdio.h>
void main(void)
{
int n;
printf(“Input an integer value n: ”);
scanf(“%d”, &n);
if (n >= 0)
printf(“n = %d is POSITIVE\n”, n);
else
printf(“n = %d is NEGATIVE\n”, n);
}
Mark G. De Vera
Nested If Example
if (day >= 1 && day <= 5) /* check for weekday */
{
if (start_time >= 600 && start_time <= 1800)
rate = 2.5 /* regular rate */
else
rate = 2.0; /* discounted rate */
}
else /* assume it’s a weekend */
{
rate = 1.5; /* weekend rate */
}
charge = rate * duration;
printf(“The charge for this call is = %.2f\n”,
charge);
Mark G. De Vera
Switch-Case Statement
#include <stdio.h>
void main(void)
{
int day;
printf(“Input day in numeric form: ”);
scanf(“%d”, &day);
switch(day)
{
case 1 : printf(“That day is a Monday!\n”);
break;
case 2 : printf(“That day is a Tuesday\n”);
break;
case 3 : printf(“That day is a Wednesday!\n”);
break;
}
printf(“Have a nice day.\n”);
}

Mark G. De Vera
Loop Control Structure
#include
<stdio.h> Output
void 1
main(void)
2
{
3
printf(“%d\n”,
1); 4
printf(“%d\n”, 5
2);
printf(“%d\n”,
3); Mark G. De Vera
Loop Control Structure
Is the previously presented source
code acceptable for you in printing
the output?

Mark G. De Vera
Loop Control Structure
Three Loop Control Structures
 While loop
 For loop
 Do while loop

Mark G. De Vera
While Loop
while (<expression>)
<statement>
#include <stdio.h>
void main(void)
{
int i;
i = 1; /* initialization */
while (i <= 5) /* conditional check */
{
printf(“i = %d\n”, i); /* body */
i++; /* change of state */
}
}
Mark G. De Vera
For Loop
for ([initialization]; [condition];
[change of state])
<statement>
#include<stdio.h>
void main(void)
#include <stdio.h>
{
void main(void) int i=1;
{ for( ;i<=5; )
int i; printf(“%d\n”,i);
for (i = 1; i <= 5; i++) i++;
}
printf(“%d\n”, i);
}

Mark G. De Vera
Do While Loop
do
<statement>
while (<expression>)

#include <stdio.h>
void main(void)
{
int i;
i = 0; /* initialization */
do /* body of the loop */
{
printf(“%d\n”, i);
i++; /* change of state */
} while (i <= 5); /* condition */
}
Mark G. De Vera
Functions
a program by itself – it may have
input(s), output(s) and will perform
some kind of processing steps.

Two types of function
 Pre-defined
 User-defined

Mark G. De Vera
Pre-defined Functions
those functions that have been
written by for us (by some other
programmers);

Example
printf(), scanf(), exit(), strcmp()
library functions

Mark G. De Vera
User-defined Functions
those function that we are going to
write/implement by ourselves;

Example
implementation of the main()
function

Mark G. De Vera
Defining a Function in C
<data type> <function name>
(<parameter list>)
{
[<local variable declaration>]
[<statement>]
[<return value>]
}

Mark G. De Vera
Defining a Function in C
void is the <data type> of the
Example: value returned by the function – void
denotes that fact that the function does
#include <stdio.h> not return anything

void main(void)main is the <function name>
{ void is the <parameter list> –
int x; void denotes the fact that it does not
have any parameter
x = 1;
printf(“%d\n”, int
n);x is the <local variable
declaration> and x = 1;
} printf(“%d\n”, n); are the
[<statements>]
Mark G. De Vera
Example w/ two functions
#include <stdio.h>
void hello(void)
{
printf(“Hello\n”);
}
void main(void)
{
hello(); /* call (invoke) the
function*/
}
Mark G. De Vera
Examples…
void main(void)
{
int i;
for (i = 0; i < 500; i++)
hello(); /* call inside the
body of the loop*/
}

Mark G. De Vera
Function with parameter
#include <stdio.h>
/* this function has one parameter, type is int
and name is x */
void OneParam(int x)
{
printf(“Number is %d\n”, x);
}
void main(void)
{
int x;
x = 100;
OneParam(x); /* this is how OneParam()
function is called*/
Mark G. De Vera
Function with Data Type
#include <stdio.h>
int Sum(int x, int y)
{
int z;
z = x + y;
return z; /*don’t forget to return a
value*/
}
void main(void)
{
int a, b, c;
printf(“Sum = %d\n”, Sum(5, 10)); /* use constants*/
c = Sum(100, 300); /*store the return value
into a variable*/
printf(“c = %d\n”, c);
a = 25;
b = 75;
printf(“Sum = %d\n”, Sum(a, b)); /* use variables*/
printf(“Sum = %d\n”, Sum(a+2, b-3);
Mark G. De Vera
/* use expressions*/
}