1/52
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Algorithm
Set of instructions specifying the steps required to accomplish a task
Components of an algorithm
Variables and values
Instructions:
Sequences
Selections
Iterations
Procedures
(computer) Program
A sequence of instructions aims at solving a specific task
→ Instruction is carried out one after the other, none carried out when the previous isn’t accomplished
Interpreted & compiled language
Interpreted: translated during execution
Compiled: translated before execution
Problem - solving process
Progran → Algorithm → Program
Values
Represent quantities/ amounts/ measurements
Variables (definition)
Containers/ places to store values
→ Can be restricted to contain a specific type of value
Sequence structure
Series of instructions to be carried out one after the other
Selection
An instruction that decides which of two possible sequences is executed
→ Based on a condition (true/false)
if …
then …
else …
Iteration
Repeat an instruction while/ until some true or false condition occurs.
→ Two kinds of iteration: Test the condition each time before/ after repeating the instruction
Procedure
A series of instructions with a name
→ divide a program into smaller parts with different names
C Language Structure
• #include <stdio.h> → To declare using the standard I/O library
• int main() → To declare the main() function → only one
• { … } → The syntax to open and close a block of codes.
• return 0; → Stop the program
Syntax
Keywords: reserved words for specific meaning
User’s names: defined by user to specify a variable, a function
Specific characters: represent expressions in a program and make it have structure
Keywords
Flow control (6) – if, else, return, switch, case, default
Loops (5) – for, do, while, break, continue
Common types (5) – int, float, double, char, void
Structures (3) – struct, typedef, union
Counting and sizing things (2) – enum, sizeof
Rare but still useful types (7) – extern, signed, unsigned, long, short, static, const
Jump to label (1) – goto
Weirdies (3) – auto, register, volatile
Common used characters
{…} create a block of instructions
“…” create a string to display
/* … */ create a block of comment in the program
; End of an instruction
other characters for formulas such as +, -, *, /, (),
Identifier
When declare a variable/ procedure
Variable
has a name (identifier)
has types
declared at the beginning of a function: <data type> <variable list>; → Assignment = puts a specified value into a specified variable
Basic data types
• char
• short
• int
• long
• float
• double
Strings
Groups of char form strings
→ must be enclosed by double quote “ ”
Logic
every number give also a boolean value
• 0 for false
• 1 for true
Const declaration
A variable declaration with the prefix const indicates that the value of that variable never changes in the program.
Data input/ ouput
printf()
scanf()
Conversion character of printf()
do not display in the screen → replaced by values
• %d: signed decimal integer
• %u: unsigned decimal integer
• %x: hexadecimal integer
• %o: octal integer
• %s: string
• %c: single character
• %f: fixed decimal floating point
• %e: scientific notation floating point
= To print a character % → use %%
Formatting with printf
specify the field width by following:
% [-] [fwidth] [.p] X
• [fwidth] the field width
• [-] left justified
• [.p] the number of decimal places/ how many characters are to be printed
Formatting with scanf
• %d : decimal integer (int)
• %ld : long decimal integer (long)
• %x : hexadecimal integer
• %o : octal integer
• %h : short integer (short)
• %f : float type
• %lf : long float or double
• %c : single character
• %s : character string
Return value of scanf()
n = scanf ("string...", pointers);
→ The value n returned is the number of items matched or the end of file character EOF, or NULL if the first item did not match
Expressions
Combine values using operators, function calls
Return a value of a known type
Operator
Take values & do something to produce a result
Operand
Operated upon by an operator
Arithmetic operators
+ (unary plus)
- (unary minus)
+ (addition)
- (subtraction)
* (multiplication)
/ (division or quotient)
% (modulus or remainder)
Comparison operators
• < (less than)
• <= (less than or equal)
• > (greater than)
• >= (greater than or equal)
• == (equal)
• != (in-equal)
Logic
Have 2 values: true (1) ; false (0)
Create the selection of conditions/ the loop for an algorithm
Logic operators
• && (and)
• || (or)
• ! (not)
• comparison (==, !=, <, >, <=, >=)
Bit operators
& (and bit)
| (or bit)
~ (negation)
>> (shift right)
<< (shift left)
Short-circuiting
A complex Boolean expression is only evaluated as far as necessary
Extend assignment operators
+=
-=
*=
/=
%=
Increment, decrement operators
++ is the increment operator
-- is the decrement operator
→ Two ways of writing: prefix (++i) and suffix (i++):
Prefix return value after adding 1
Suffix return value before adding 1
In both cases, value of i increases by 1
Conditional Expressions
<Condition> ? <Expression 1> : <Expression 2>
Casting data type
int a;
float f;
a = (int) f;
Precedences
• Unary operators (!, -)
• Multiply, divide (*, /, %)
• Addition, subtraction (+, -)
• Comparison 1 (<, <=, >, >=)
• Comparison 2 (==, !=)
• And (&&)
• Or (||)
• Assignment (=)
if statement
if ( expression )
statement
Statements
lines of instructions in programs ending with a semicolon (;).
• A compound statement/ block: series of statements surrounded by braces.
• An empty statement: a single semicolon.
else statement
if ( expression )
statement1
else statement2
only occur after an if statement
only executed when the if block does not execute
Cascading if (else-if)
if (expr1)
statement1
else if (expr2)
statement2
else if (expr3)
statement3
else
statement4
At most only one block will be executed
Choose one among several conditions
switch statement
switch (integer value)
{
case 1: statement1;
break; /* optional line */
case 2: statement2;
break; /* optional line */
....
default: default statement;
break; /* optional line */
}
The expression in the parentheses is evaluated
Check to see whether the result matches any constants labelled with case
A match is made → execution start after that case statement → carry on until the closing brace } / break statement
Default case are executed for all cases not specifically listed
Using break
All following statements are carried out until a break statement
→ break is a way of jumping straight out of the switch block
Loop
Repeat statement(s)
→ Provide versatile manipulation:
number of repetitions
range of loops
while loop
while ( condition(s), )
statement(s)
• Execute a statement according to a condition
• Verify the condition at the beginning of the loop
• Stop the loop when the condition is false ( = 0 )
for loop
for ( initialization ; condition ; update )
statement
All components are optional → If the condition is empty, interpreted as true
(,) separates elements of a component
do … while statement
do {
statements;
} while ( condition);
→ The loop always executed at least once before the test → determine whether it should continue
Infinitive loops
The loop condition is always true
while (1)
for (;;)
Break statement
Use in an infinitive loop to terminate the loop
→ for (;;)
{ …
if (condition) break;
}
Continue statement
A loop will stop whatever it is doing → go straight to the start of the next loop pass
→ for (…)
{…
if ( condition) continue;
…}