Professional Documents
Culture Documents
SYLLABUS PROGRAM/SOFTWARE
1. Programming Concepts ● The list of instructions that the computer must follow to
2. Basic Program Structure be able to process data into information.
3. Using Conditional Constructs ● These instructions consist of statements written in a
specific programming language. When a program is
written, we are basically solving a problem. To solve a
PROGRAMMING CONCEPTS problem, we need to use our mind power or logic to
develop the detailed instructions.
COMPUTER
PROGRAMMING
● An electronic device that accepts data, processes it,
produces results, displays them to the users, and ● The procedure of developing the detailed
stores the results for future usage. instructions for the computer.
Indicates directly
TYPES OF ERROR Direct
accessible data, such as
Access
those on magnetic disks
Can be typographical error or Storage
Syntax error & floppy disks.
incorrect format of the statement used.
Indicates data on media
Caused by incorrect use of control
Logic error Documents human beings can read.
structures.
SELECTION STRUCTURE
the marked path FALSE
is to be followed. ● One of several alternative actions is selected and
executed.
Arrows are used to direct ● It involves the use of decision based on the given
Arrowheads/ the flow of the flowchart. condition. It uses decision block or the diamond-
Flow lines It shows the flow of the shaped block. Alternative action will be represented
solution. by a processing block.
● Examples of selection structure in flowchart:
It is used as a
continuation symbol of a
Connector
flowchart. It shows the
flow of the solution.
It is used to initialize
Initialization/ variables not explicitly
Preparation defined in the input but a
Symbol requirement in the
production of the output.
TYPES OF FLOWCHARTS
SEQUENTIAL STRUCTURE
● Steps are performed in a strictly sequential manner;
each step being executed exactly once. This is the
simplest method of control. It is easy to develop and
understand.
● Example of sequential structure in flowchart:
ALGORITHM
● A set or series of instruction for carrying a task.
● It is also a procedure to produce the required output from
the given input.
● It is similar with a paragraph that expresses a single
thought. It uses English language. It is also known as
pseudo code.
ALGORITHM DEVELOPMENT
PSEUDO CODE
BASIC PROGRAM STRUCTURE
● A mixture of language and symbols, terms and other INTRODUCTION TO C
features commonly used one or more high-level
languages.
C LANGUAGE
● High-level languages are programs used by the
computer. ● A general-purpose programming language which
features economy of expression, modern control flow
Typically, the common features of various pseudo codes that and data structures and a rich set of operations.
appear in textbook are the following: ● is has been called as “system programming language”
because it is useful for writing compilers and operating
1. The usual computer symbols are used for arithmetic systems.
operations: “+” for addition, “-“ for subtraction, “*” for ● A structured programming language. It is considered a
multiplication, and “/” for division. high- level language because it allows the programmers
2. Symbolic names (identifiers) are used to represent to concentrate on the problems at hand.
quantities being processed by the algorithm.
3. Certain keywords that are common in high-level language
may be used, for example, read or enter to indicate input HISTORY OF C
operation; display, print and write for output operations.
4. Indentation is used to set off certain key blocks of Basic Combined Programming Language (BCPL)
instructions. is developed by Martin Richards in 1967.
1967
This language is turn strongly influenced the
Three basic control structures in algorithm. development of the next language –B.
3. Repetition structure
INTERPRETER
ALGORITHM PrintOddNumbers ● Reads the source code of your program one line at a
A -> 1, B -> 10 time and performs the specific instructions contained in
INPUT A, B that line.
WHILE A <= B DO
IF A is odd THEN
OUTPUT A COMPILER
END IF ● Reads the entire program and converts it into object
A -> A + 1 code-the form that can be directly executed by the
END WHILE
computer.
END PrintOddNumbers
COMPILE TIME
● Events that occur during the compilation process.
OBJECT CODE
● A translation of the program source code in a form that
can be directly executed by the computer. It is also
called the binary code and machine code.
1ST SEMESTER: MIDTERMS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
IDENTIFIERS
IDENTIFIERS
● Names that are used to reference variables, functions,
labels, and various other user-defined object.
● Sequence of letters, digits and the special character ‘_’,
which is called an underscore.
SOURCE CODE
● The text of a program that a user can read. Rules in naming identifiers:
● Commonly thought of as a program.
1. An identifier in Turbo C can vary from one to several
characters; the first character must be a letter or an
RUN TIME underscore with subsequent characters being letters,
● Refers to the events that occur while the program is numbers, or the underscore. It may not have a space
executing. or a hyphen.
2. Must consist only of alphabetic characters, digits, or
LIBRARY underscores.
● Collection of pre-written. 3. The first 63 characters of an identifier name are
significant. In C, upper-and lower case are treated as
● Ex. #include <stdio.h> different and distinct from one another.
Example: Sname, Sname, and sName are three separate
SYNTAX ERRORS identifiers
● Detected during execution-time.
4. An identifier may not be the same as a Turbo C
SEMANTIC/RUN keyword, and it should not have the same name as a
function.
● Time errors are detected during execution time.
DATA TYPES
COMPONENTS OF TURBO C
int variable
Turbo C is more than just a version of the C language.
Rather, it includes a complete environment in which to create, ● Whole number consisting of an optional sign (+ or -)
test, and run programs. This programming environment followed by a sequence of digit. It cannot contain
consists of several components: commas.
● Variables of this type are often used for controlling loops
and conditional statements.
TURBO C COMPONENTS
TYPES OF INTEGERS
Editor Used to create program source code.
Type Byte size Min. value Max. value
This version of C is significantly extended
from the “base bone” language of Ritchie’s Short int 2 -32768 +32767
Extended C specifications. This extension includes
language enhancement which make the Turbo C Int 4 -2147483648 +2147483647
compatible with the new proposed and ANSI
Standard. Long int 4 -2147483648 +2147483647
+ Plus Addition
Using ‘#define’
● It is used at the beginning of the program. - Minus Subtraction
● Like const, #define is also used in declaring
constants. - Unary minus Negation
● Syntax: #define variable value
● Example: #define kiss 99 % Modulus Modulus division
NOTE: It is not necessary to tell a computer the type of a ++ Plus-plus Increment operator
declared constant. C determines the data type from the
assigned value. Remember that although, const and # -- Minus-minus Decrement operator
define defines a constant, the two modifiers have different
syntax. () Parenthesis parenthesis
Example: 11 % 2 = 1, 5 % 3 = 2, 147 % 20 = 7
1ST SEMESTER: MIDTERMS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
Z = Y + X++ Z = Y + X; 13 == Equal to a == b
X = X + 1; 4
!= Not equal to a != 100
Z = Y + ++X X = X + 1; 4
Z = Y + X; 14
LOGICAL OPERATORS
Z = Y + X-- Z = Y + X; 13
X = X - 1; 2 SYMBOL MEANING EXAMPLE
Z = Y + --X X = X - 1; 2 && And N = 5 && 6
Z = Y + X; 12
|| Or 1 || 1
TYPES OF ARITHMETIC OPERATORS
! Not 5 != 6
UNARY OPERATORS
● Require only a single operand like unary plus and unary NOTE: In C, true is any value other than 0; false is 0
minus.
● Unary plus operator (+) – causes no change to the 1 True
quantity which follows.
● Unary minus operator (-) – causes the sign of the 0 False
following quantity to be changed.
● Example: Example:
Int x = 50 +x = 50; -x = -50 N = 10 ? 5 %% !(10 < 9) || 3 <= 4
1 && !(0) || 1
BINARY OPERATORS 1 && 1 || 1
● Requires two operands. N = 1
● Example:
+ (addition) X+Y TERNARY OPERATORS
x (multiplication) X * 3 ● Uses question mark and colon symbols.
● Expression1 is evaluated, if true, then expression2 is
COMBINED OPERATORS evaluated and become the value of the expression;
● C’s “shorthand” operation otherwise, expression3 is evaluated and its value
become the value of the expression
● Example:
X = X + Y; ● exp1 – conditional; exp2 – “if”; exp3 – “else”
X+ = Y (shorthand version) ● Syntax: Expression1? expression2: expression3;
● Example: x = 10, y = 5;
x > y? sum = x + y: diff = x – y;
1ST SEMESTER: MIDTERMS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
COMMA OPERATORS ● An include file, is also called “header file”, can contain
● Used to string together several expressions. #define lines and other #include lines.
● Example: x = (y = 3, y++, y * 2); y = 10; ● By convention, the names of header files end in ‘.h’
y = 5; y = 4; y = 8; ● C system provides a number of standard header files.
x = (y = y – 5, 25/Y); ● Example: stdio.h, string.h, math.h
x = 5 o These files contain declarations of functions in the
Finally, x = 8 standard library, macros, structure templates, and
other programming elements that are commonly used.
EXPRESSIONS ● The preprocessing directive #include <stdio.h> causes
● Combinations of operators, constants, and variables. a copy of the standard header file stdio.h to be included
in the code when compilation occurs.
PRECEDENCE OF OPERATORS
STANDARD INPUT/OUTPUT
OPERATOR SYMBOLS USED Formatted Output
Explanation:
Corresponding How the corresponding argument
● If the above lines occur in a file that is being compiled,
argument is printed
the preprocessor first changes all occurrences of the
identifier LIMIT to 100 and all occurrences of PI to
3.14159. %c As a character
● The identifier LIMIT and PI are called symbolic %d As a decimal integer
constants.
● The use of symbolic constants in a program makes it As a floating-point number in
more readable. More importantly, if a constant has been %e
scientific notation
defined symbolically by means of #define facility and
used throughout a program, it is easy to change later,
%f As a floating point
if necessary.
%lf As double
The #include
● This is commonly known as macro include. In the e-format of f-format
%g
● A preprocessing directive that causes a copy of the file whichever is shorter
to be included at this point in the file when compilation
occurs. A #include line can occur anywhere in a file, %s As a string
though it is typically at the head of the file.
● The quotes surrounding the name of the file are
Example: printf(“%c”, x);
necessary.
printf(“%s”, name);
1ST SEMESTER: MIDTERMS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
field – the place where the argument is printed ENTER key is pressed.
● ENTER key is not stored byte is replaces be the null
field width – number of characters in its fields terminator.
The field width can be specified in a form at as an integer
getche()
occurring between the percent character (%) and the
conversion character. ● Reads the character without echo.
● Does not wait for carriage return or ENTER key.
Example: printf(“%c %3c %5c\n”, ‘A’, ‘B’, ‘C’);
Will print: A__B____C getch()
● Reads a character with echo.
Formatted Input
● Does not wait for carriage return or ENTER key.
The scanf() function
getchar()
● Analogous to printf(), but is used for input rather than
output. ● Reads a single individual character from the keyboard
● The first argument is a control string having formats that ● Waits for the carriage return or ENTER key.
corresponds to the various ways the characters in the
input stream to be interpreted. The other arguments are puts()
addresses. ● Writes a string to the screen, followed by a newline.
● Syntax: scanf(“%conversion specification”, &variable); ● It can only output a string of characters. It cannot output
● Example: scanf(“%d”, &x); numbers or do format conversion.
● Takes up less space and run faster.
scanf() can be used to convert a string of decimal digits
into an integer value and to store the value at the appropriate putchar()
place in memory:
● Writes a string (character) to the screen.
Conversion
Character
Valid characters in the input stream PROGRAM CONTROL STRUCTURE
%c To be a character CONTROL STRUCTURE
● Analyzes and chooses in which direction a program
%d To be a decimal integer flows based on certain parameters or conditions.
● Way to specify flow of control in programs.
%f To a floating-point number (float)
● Any algorithm or program can be clearer and more
understood if they use control structures as self-
%lf To a floating-point number (double)
contained modules.
%s To a string ● Three types of logic/flow of control:
o Sequence logic/flow
o Selection logic/conditional flow
ESCAPE SEQUENCE o Iteration logic/repetitive flow
FLOW OF CONTROL
Sequence Meaning
SEQUENTIAL FLOW OF CONTROL
\n Newline
● Statements in a program are normally executed one
\t Move to the next tab stop after another.
● Often it is desirable to alter the sequential flow of control
\xhh Insert char hh where hh is a hex. number to provide for a choice of action, or repetition of action.
● By means of if, if-else, and switch statement, a
\a Sound the bell selection among alternative actions is made.
● By means of while, for and do statements, interactive
\\ Insert a backslash character actions can be taken.
CONDITIONAL STATEMENTS
<= Less than or equal to expr <= expr
The if statement
>= Greater than or equal to expr >= expr ● Form:
if (expr)
EQUALITY OPERATORS statement;
● Action: If expr is non-zero (true), then statement is
== Equal expr == expr executed; otherwise statement is skipped control
passes to the next statement.
!= Not equal expr != expr ● Example:
if (grade >= 90)
EQUALITY OPERATORS printf(“Congratulations!);
printf(“Your grade is %d\n”, grade);
! (unary) Negation !expr
Expressions in if statement is relational,
&& Logical “end” expr && expr logical, equality is permissible in any domain.
- Break: at the point, execution should jump to the end The for statement
of the switch statement. ● Form:
- Default: specifies an action to be taken if the value for(initialization; condition; increment)
of the switch expression does not match any of the statement;
listed values.
● Action: Allows many variants, but there are three parts:
o Initialization – assignment statement that is used to
Unconditional Transfer Statement
set the loop control variable.
● break(); Used to terminate a case in a switch statement; o Condition – expression that determines when the
terminates loop through bypassing, but program control loop will exit.
resumes at the next statement. o Increment - defines how the loop control variable will
● Example: change each time the loop is repeated.
while(1) { ● Example:
scanf(“%lf”, &x);
for(x = 100; x != 65; x += 5) {
if(x < 0.0)
break; z = sqrt(x);
printf(“%f\n”, sqrt(x)); printf(“The square root of %d is %f”,x, z);
} }
An important point above for loop is that conditional
● continue; Causes the current iteration of the loop to test is always performed at the top of the loop. This
stop and causes the next iteration of the loop to begin means that the code inside the loop may not be executed
immediately. Occurs inside for, while, and do loops. at all if the condition is FALSE to begin with. For loops
● Example: can be also nested.
do {
scanf(“%d”,&num); The do statement
if (x < 0 ) ● Form:
continue; do {
printf(“%d”,x); statement;
} while (x != 100);
} while(expr);
next statement;
An if-else statement can be used as the statement ● Action: First statement is executed and expr is
part of another if statement, this is what you evaluated. If true, control passes back to the beginning
call nested if. of the do statements and loops. If false, control passes
to the next statement.
● Example: ● Example:
if(x == 50) { do {
if(y >= 120) { scanf(“%d”,&num);
sum = x + y; sum += num;
printf(“the sum of x and y is %d”, sum); } while (num > 100);
} printf(“Then sum is %d”, sum);
else {
diff = x – y;
printf(“the difference between x and y is %d”, diff);
}
else {
printf(“Next time”);
}
LOOPS
● Repetition of action in computers where a looping
control mechanism executes specific statements, which
is convenient for fixing large amounts of data.