0% found this document useful (0 votes)
75 views18 pages

Lesson 5 - Introduction To Computer Programming

This document provides an introduction to computer programming and the C programming language. It discusses what a program and programming language are, and covers different types of programming languages from machine language to very high-level languages. The document then focuses on the C programming language, describing its basic structure, components like variables and data types, and functions. It also discusses programming concepts like syntax, semantics, compilers, and integrated development environments.

Uploaded by

Rodge Seda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
75 views18 pages

Lesson 5 - Introduction To Computer Programming

This document provides an introduction to computer programming and the C programming language. It discusses what a program and programming language are, and covers different types of programming languages from machine language to very high-level languages. The document then focuses on the C programming language, describing its basic structure, components like variables and data types, and functions. It also discusses programming concepts like syntax, semantics, compilers, and integrated development environments.

Uploaded by

Rodge Seda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

10/23/2019

PROGRAM

 List of computer instructions required to


INTRODUCTION TO COMPUTER arrive at the desired results.
PROGRAMMING

College
College of
of Information
Information &
& Math 112 - Fundamentals of Computing I College of Information & Math 112 - Fundamentals of Computing
Communications
Communications Technology
Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

PROGRAMMING LANGUAGES (PL) TYPES OF PL


 Machine Language – Strings of numbers giving machine specific
A formalize notation that allows instructions
programming to be presented in a rigorous  Assembly Language – English – like abbreviations representing
elementary computer operations (translated via assemblers)
and precise way.  High – Level Languages – Codes similar to everyday English; Use
mathematical notations (translated via compilers)
 Very-High Level Languages – Closer to human languages and are
accessible to people without formal training as programmers.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

 Syntax deals solely with the form and structure of


symbols in a language without any consideration
given to their meaning.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

 Reveals the meaning of syntactically valid strings


in a language.
 Describes the behavior that a computer follows
when executing a program in the language.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Integrated Development Environment (IDE)

An IDE normally consists of at


least the following:

 A Source Code Editor


 Build Automation Tools, and
 A Debugger.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

STRUCTURE OF A C PROGRAM
 ‘C’ is high level language and is the upgraded version of The basic components of a C program are:
another language (Basic Combined Program Language).
 C language was designed at Bell laboratories in the early
 main()
1970’s by Dennis Ritchie.  pair of braces { }
 C being popular in the modern computer world can be  declarations and statements
used in Mathematical Scientific, Engineering and
Commercial applications  user defined functions

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

COMMENTS
(Multiple Lines)
VARIABLES
PRE-PROCESSOR
DIRECTIVE

MAIN FUNCTION PROGRAM


IDENTIFIERS TERMINATOR
COMMENTS
(Single Line)

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Definition: Variable Definition: Variable Declaration


 This communicates to the C Compiler the names
of all variables used in a program. They also tell the
 A name associated with a memory cell used for compiler what type of information will be stored in
storing a program’s input data and its each variable.
computational results.
 Syntax: <data type> <variable list>;
 Examples: int num1, num2, sum;

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Common Variable Characteristics Definition: Data Type


Variable Attribute Description  A set of values and a set of operations on those
Name The name of the variable used to reference data in values.
program code
Type The data type of the variable (number, character, and so
 Pre-defined data type in C are:
on)
Value The data value assigned to the memory location int, double, float, char
Address The address assigned to a variable, which points to a
memory cell location

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

INITIALIZING VARIABLES AND THE ASSIGNMENT


OPERATOR KEYWORDS / RESERVED WORDS

//variable //variable initializations


declarations x = -4443;
int x; y = 554.21;
float y; c = 'M';
char c;

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

CONVERSION SPECIFIERS CONVERSION SPECIFIERS


COMMON CONVERSION SPECIFIERS USED WITH PRINTF () COMMON CONVERSION SPECIFIERS USED WITH Scanf()

Conversion Specifier Description


scanf("conversion specifier", variable);
%d Displays integer value
%f Displays floating-point numbers
%c Displays character

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Let’s Try this Simple Exercises: Exercise 1:


Circumference is the distance around the edge of a circle. There are
two (2) ways on how we can calculate the circumference of a circle
(C) depending on the given parameter; (1) Given the radius (R), the
formula for C = 2piR and (2) Given the diameter (D), the formula
Write a C program that converts a given integer (in
for C = piD. NOTE: pi = 3.14159. days) to years, weeks and days, assumes that all
1. Write a C Program that asks a user input for the radius and years have 365 days.
computes the circumference of a circle (C).
2. Write a C Program that asks a user input for the diameter and
computes the circumference of a circle (C).

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Exercise 1: Output Quick Recap:


Sample Output:

Input: Number of Days 1. Write a C program that


outputs your name, course,
Output: Years, Weeks and Days
section and birthday using
only 1 print function.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Quick Recap: Quick Recap:

3. Write a C program that accepts two


2. Write a C program that asks for (2) numbers (not just integers) and
a user input of three (3) integers calculates the sum, difference, product
and prints these integers in and quotient of the 2 numbers. Include
a note in your program stating that the
reverse order. second number should not be equal to
zero (0).

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Concept ReCap: Identifiers vs Variables Identifiers: Naming Rules


 Can only have alphanumeric characters and underscore symbol.
IDENTIFIERS Identifier – name given to an entity  Should be unique
Variable  First character must be an alphabet or underscore.
Names
Class
 Can’t use keywords or reserve words as identifiers.
Function
Names
Names  Only first thirty-one (31) characters are significant.
Structure  Must not contain white spaces.
Names
 Identifiers are case-sensitive.
Variable – name given to memory location

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Definition: C Constants Definition: C Constants


These are the fixed values that Remember:
are used in a program and its Syntax:
value remains the same 1. Constants are also called const type CONSTANT_NAME = <value>;
during the entire execution of literals.
the program. 2. Constants can be any of the
data types. Example:
3. It is considered best practice const float PI = 3.14159;
to define constants using only
upper case names.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Definition: Operators in C OPERATORS: Arithmetic Operators

 The Arithmetic operators


performs arithmetic
operations.

Symbols that are used to perform mathematical or


logical manipulations.
College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

ARITHMETIC OPERATOR PRECEDENCE Sample Simple Program (SSP): Arithmetic Operators

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

OPERATORS: Relational Operators OPERATORS: Logical Operators


 Compare arithmetic, logical
and character expressions. The  Use to evaluate logical
Relational Operators compare and relational expressions.
their left hand side expression
with their right hand side
expression. Then evaluates to an
integer. If the Expression is false
it evaluate to “zero”(0) if the
expression is true it evaluate to
“one”

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

OPERATORS: Assignment Operators OPERATORS: Increment / Decrement Operators

 Use to assign a value to a  The increment/decrement


variable. operator act upon a Single
operand and produce a new
value is also called as “unary
operator”. The increment
operator ++ adds 1 to the
operand and the Decrement
operator – subtracts 1 from
the operand.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Sample Simple Program (SSP): Increment/Decrement PROGRAM STATEMENTS

 Control program execution and functionality.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Escape Sequences Escape Sequence Purpose

 Sequenced characters used to format output. • \n Creates a new line


• \t Moves the cursor to the next tab
• \r Moves the cursor to the beginning of the current line
• \\ Inserts a backslash
• \" Inserts a double quote
• \' Inserts a single quote

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

DATA TYPES DELIMETERS

 Integers (int)
 Floating-point numbers (float)
 Double (double)
 Characters (char)

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Selection Structures: Conditional Statements

 The conditional expressions


are mainly used for decision
making.

Selection Structures / Decision Logic Structure

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Selection Structures: Conditional Statements if Statement The simple structure of ‘if’ statement is
i. if (< conditional expression>)
statement-1;
(or)
a. if statement.  Use to express conditional
ii. if (< conditional expression>)
b. If-else statement. Or 2 way {
expressions. If the given statement-1;
if statement condition is true then it will statement-2;
c. Nested else-if statement. execute the statements statement-3;
otherwise skip the statements. ……………
d. Nested if –else statement.
……………
e. Switch statement. STATEMENT-n
}

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

if(<exp>)
Example: if Statement if-else Statement {
Statement-1;
Statement -2;
……………
Statement- n;
 Use to execute the either of the }
two statements depending upon else
the value of the expression. {
Statement1;
Statement 2;
……………
Statement- n;
}

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Example: if-else Statement Nested if-else Statement

 When a logical expression is if ( <exp1> )


encountered whose value is true Statement-1;
the corresponding statements will else if ( <exp2> )
be executed and the remainder of Statement-2;
the nested else if statement will be
else if ( <exp3> )
bypassed. Thus control will be
transferred out of the entire nest Statement-3;
once a true condition is else
encountered. Statement-4;

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Example: nested if-else Statement Nested else-if Statement

 One complete if-else statement if(exp1)


if(exp3)
will be executed if expression1 is
Statement-3;
true and another complete if-else
else
statement will be executed if Statement-4;
expression1 is false. else
if(exp2)
Statement-1;
else
Statement-2;

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Quick Check: Individual Task

1. Show the output of the following program lines when the data
entered are 5 and 7.
int m, n;
printf(“Enter two integers: ”);
scanf(“%d%d”, &m, &n);
m = m + 5;
n = 3 * n;
printf(“m = %d\nn = %d\n”, m, n);

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Quick Check: Individual Task If - Else


2. Show the output of the following program lines.
int length = 5, width = 2, area;
area = length * width;
if(length == width)
{
printf("It's a square! Area is %d", area);
}
else
{
printf("Area of the Rectangle: %d", area);
}

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Problem: (if – else) Activity: Selection Structure (if – else)


1. Write a C Program that will check if a given number is an even or
an odd number.
Write a C program that can compute for the salary of the employee
with the following conditions: for employees who worked for more 2. Write a C Program that will check if a given number is a negative
than 40 hours, the salary of the employee is computed as the or a positive number.
employee’s rate plus its 10% multiplied by the number of working
hours. Otherwise, salary is equal to the rate * number of working
hours.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Code Analysis: Tracing an if-statement Code Analysis: Tracing an if-statement

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Code Analysis: Tracing an if-statement Code Analysis: Tracing an if-statement

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

Quick Check: Selection Statements Quick Check: Selection Statements

2. Write an interactive C Program that computes and displays the


1. Write a C Program to carry out the following steps: area of a rectangle (length * width) or a triangle [(base * height)/2]
a. Store the absolute value difference of the integers x and y in y, where after prompting the user to type the first character of the figure
the absolute difference is (x – y) or (y – x), whichever is positive. name (R/r or T/t). However, if selected value is (R/r), the program
b. If x is 0, add 1 to zero_count. If x is negative, add x to minus_sum. If x checks if length == width and the program shall display that it is a
is greater than 0, add x to plus_sum. Print the necessary output for square. Further, if a user entered a different character (other than
each condition. R/r or T/t), the program must show the error message, “Invalid
Input”.

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa
10/23/2019

Switch Statement
switch(expression)
{
case value1:
 A switch statement is used to statement(s);
choose a statement (for a group break;
of statement) among several case value2:
statement(s);
alternatives. The switch break;
statements is useful when a case value3:
variable is to be compared with statement(s);
different constants and in case it break;
default:
is equal to a constant a set of statement(s);
statements are to be executed. }

College of Information & Math 112 - Fundamentals of Computing College of Information & Math 112 - Fundamentals of Computing
Communications Technology Ms. Brenda Dy – Po - Benosa Communications Technology Ms. Brenda Dy – Po - Benosa

You might also like