What Is Computer Science?
What Is Computer Science?
ELEMENTS OF PROGRAMMING
Problem definition
The first part requires you to understand and define the problem at hand.
You should be able to identify the necessary inputs and the required
output. This task produces the requirements lists.
Algorithm development
Then verify the algorithm by following the steps and check to see if the
algorithm solves the problem.
• Implementation phase
PROPERTIES OF ALGORITHM
1. Finiteness
2. Absence of ambiguity
3. Sequence definition
• The order in which the steps are carried out should be clearly specified.
• The algorithm can have zero or more inputs and one or more outputs.
5. Effectiveness
• The algorithm can only give instructions that the computer can carry out.
• Cases wherein there is insufficient information to perform a task should not
exist.
6. Scope definition
REPRESENTATION OF ALGORITHMS
Flowchart
Pseudocode
FLOWCHARTS
FLOWCHARTING BASICS
Almost every program involves the steps of input, processing and output as in
the program:
1. Get number
2. Answer = number * 2
3. Print answer
Therefore, most flowcharts need some way to visually separate these three steps
INPUT OPERATIONS
parallelogram
PROCESSING STATEMENTS
FLOWLINES
Solution:
The best way to end the program is to have predetermined value for NUMBER
that means “Stop the program!”
The programmer and the user could agree that the user will never need to know
the double of 0, so the user could enter a 0 when he or she wanted to stop.
The computer could then test any incoming value for NUMBER and, if it is a 0,
stop the program.
DECISIONS
A connector will be used when the physical constraints of page size stop the
programmer from completing the flowchart.
VARIABLES
FLOWCHART STRUCTURES
• Sequence
• Selection
• Iteration
SEQUENCE
PSEUDOCODE
Arithmetic Calculations
• Display a message asking the user for a value and store the value typed
by the user in a variable.
o Example: Input custName
• The example means display a message asking the user to input a
customers name and store the value typed by the user in the variable
called custName.
BASIC CONSTRUCTS
SEQUENCE
IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
IF-THEN-ELSE EXAMPLES
Example 1:
ELSE
ENDIF
Example 2:
ENDIF
Example 3:
ELSE
ENDIF
IF-ELSE IF -ELSE
IF condition THEN
statements
statements
statements
ELSE
statements
ENDIF
Note: the ELSE part is optional. Use it where required by the logic.
grade = “1"
comment = "Excellent"
grade = “2.5"
grade = “2.75"
grade = “3"
ELSE
grade = “5"
comment = "Poor"
ENDIF
PSEUDOCODE STATEMENTS: BOOLEAN OPERATORS
Boolean Operators
• The WHILE construct is used to specify a loop with a test at the top.
• The beginning and ending of the loop are indicated by two keywords
WHILE and ENDWHILE. The general form is:
WHILE condition
sequence
ENDWHILE
WHILE: EXAMPLE
Example 1:
ENDWHILE
Example 2:
count = 1
Display count
count = count + 1
ENDWHILE
Example 3:
Input itemPrice
Input itemPrice
ENDWHILE
Example 4:
Input quantitySold
Input quantitySold
ENDWHILE
REPEAT-UNTIL
This loop is similar to the WHILE loop except that the test is performed at the
bottom of the loop instead of at the top.
REPEAT
sequence
UNTIL condition
The "sequence" in this type of loop is always performed at least once, because
the test is performed after the sequence is executed.
At the conclusion of each iteration, the condition is evaluated, and the loop
repeats if the condition is false. The loop terminates when the condition
becomes true.
FOR LOOP
This loop is a specialized construct for iterating a specific number of times, often
called a "counting" loop.
sequence
ENDFOR
FOR x = 1 to 10
xSquared = x * x
Display x, xSquared
ENDFOR
NESTED CONSTRUCTS
• The constructs can be embedded within each other, and this is made
clear by use of indenting.
• Nested constructs must be clearly indented from their surrounding
constructs.
Example:
REPEAT
READ Temperature
total = total + 1
END IF
In the above example, the IF construct is nested within the REPEAT construct,
and therefore is indented.
Usually there is no Start and Stop when using pseudocode, unlike with
flowcharts.
A BRIEF HISTORY OF C
In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the
computing world.
C provides:
C is used:
int main(){
int answer;
float answer2;
printf(“Hello world!\n”);
//addition
answer= 5 + 2;
//subtraction
answer= 5 – 2;
//multiplication
answer = 5 * 2;
//Division
answer = 5 / 2;
answer2 = 5 / 2;
return 0;
}
COMMENTS
between /* */ or after //
Example:
Enclosed between { }
VARIABLES
• \n – newline
• \t – tab
• \r – carriage return
• \f – formfeed
• \\ - backslash
• \” – double quote
• \’ – single quote
%d is a placeholder that will be replaced by the value of a variable.
Arithmetic Operators
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulo (%)
• Assignment (=)
• Stores a value into a variable
• Unary plus and minus
• +answer
• -answer
Arithmetic Operators
• Increment
o Increase value of a variable by 1
o Prefix
§ ++answer
o Postfix
§ answer++
• Decrement
o Decrease value of a variable by 1
o Prefix
§ --answer
o Postfix
§ answer—
PRECEDENCE
(taken from https://en.cppreference.com/w/c/language/operator_precedence)
DATA TYPES IN C
§ Keyword: char
§ A char is a letter, digit or symbol enclosed in single quotes.
§ Here are some examples of characters:
o ‘c’ ‘S’ ‘2’ ‘$’ ‘ ‘
§ Note that the space (‘ ‘) is a character just like a any letter or digit
§ Can also be an escape sequence
o ‘\n’
THE INTEGER DATA TYPE
• Keyword: int
• Represents whole numbers in C
• Its values range from –32768 to 32767 (at least)
o Size of int may vary depending on the compiler
• Here are some example of integers:
o -214
o +2001
o 2002
• An int cannot contain commas.
o For example: 2,001 is not a valid integer value
• Can contain an optional sign (+ or -)
TYPE MODIFIERS
Code:
float pi;
pi = 3.1415926535897;
printf(“%f”, pi);
Float pi;
pi = 3.1415926535897;
printf(“%.3f”, pi);
• The scanf function (found in the stdio.h header file) reads input from the
keyboard and stores the value into a variable.
• Usage:
o To read in an integer value
§ int answer;
§ printf(“Enter a number: “);
§ scanf(“%d”,&answer);
o To read in a real value using a float data type
§ float answer;
§ printf(“Enter a number: “);
§ scanf(“%f”,&answer);
o To read in to a real value using a double data type
§ double answer;
§ printf(“Enter a number: “);
§ scanf(“%lf”,&answer);
o To read in a character value
§ char answer;
§ printf(“Enter a character: “);
§ scanf(“%c”,&answer);
• It is possible to get more than one value using the scanf function
• Example:
o int answer1;
o float answer2;
o scanf(“%d %f”,&answer1,&answer2);
• As a general rule, it is not good to get more than one input using scanf.
Only get one input at a time using the scanf function.
CONSTANTS
• The scanf function allows a programmer to get input from the user.
• But what if we already know the value that we need?
o Value of pi accurate up to 15 decimal places
§ 3.141592653589793
o It would be a hassle for the user if they have to enter the value of pi
• Three ways of declaring constant values.
INITIALIZE VARIABLES
CONST KEYWORD
After using the #define statement to create a constant, you can use it just like a
variable.
Example:
#include<stdio.h>
int main(){
scanf(“%f”,&radius);
circumference = 2*radius*pi_value;
area = pi*radius*radius;
return 0;
TYPE CASTING
RECALL...
RELATIONAL EXPRESSIONS
• Boolean expressions
o Evaluate to either true or false
• Examples:
o 12 > 5 is true
o 7 <= 5 is false
o Given x = 10
§ x == 10 is true,
§ x != 8 is true
§ x == 8 is false
THE IF STATEMENT
float examscore,grade;
examscore = 97;
grade = 1.75;
printf(“Good work!\n”);
Format:
if (expression) {
statements
} else {
statements
NESTED IF STATEMENTS
LOGICAL OPERATORS