1/60
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
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
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
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()
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
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;
…}
build function
specify
function name
input parameters
type of value returned ( if exists)
block of statements executed when function is called → function body
function parameter
local variable of a function whose parameter is determined each function call
→ no parameter → use void
return statement
return a value
→ may have multiple returns → first one encountered terminates the others
function declaration & definition
definition: specify all components
declaration: specify name, type of parameter, return type → function prototype
global & local variable
global: declare outside function → accessed by multiple functions
local: declared inside a function → accessed only in that function
→ name conflict → local variable takes precedence
Array
block of variables that have the same datatype → fixed number of elements
Pointers
variable that can store the memory of another
→ format specifier: %p
How to use pointer
declare variable pointed to by pointer: int num
declare pointer: int *numPtr = NULL;
assign reference to pointer: numPtr = #
dereference pointer: *numPtr = 65;
String processing
assignment( copy): strcpy ()
concatenation: strcat ()
comparison: strcmp ()
get strign length: strlen ()
string comparison
strcmp (string1, string2)
→ lexicographically compare
result:
= 0 → equal
> 0 → string 1 > string 2
< 0 → string 1 < string 2
Struct
collection of variables
struct nameOfstruct {
};
→ names of variables inside struct: struct fields/ members
→ access a field: use . → nameOfstruct.variableName
typedef
define new data type name that can be used in variable declaration
input/ output stream
buffered area used for high-level data input, output
→ 3 standard streams:
stdin: input
stdout: output
stderr: error