Algorithm study guide
Algorithm study guide
Chapter 2) Algorithm
I. Definition
Algo is a programming language that uses our own language (English) as syntax to write instruction for the
computer.
START
Instruction 1
…2
…n
END
III. Output instruction in Algo
We will use the function DISPLAY to print information on the screen of a computer.
Syntax:
DISPLAY (info)
Detail:
Example:
START
DISPLAY (“ LASALLE COLLEGE” + nxline)
DISPLAY (“My first program” + nxline)
DISPLAY (“Hello world!”)
END
START
DISPLAY (“ IGA” + nxline)
DISPLAY (“1 Nutella is 8$” +nxline)
DISPLAY (“5 Nutella is ” + (8+5) + ” $” + nxline)
END
START
DISPLAY (“ AGE CACULATOR”+ nxline)
DISPLAY (“ - - - - - - - - - - -”+ nxline)
DISPLAY (“I am born in 2000”+ nxline)
DISPLAY (“We are in 2024”+ nxline)
DISPLAY (“So, I am ” + (2024 - 2000) + “ years old”)
END
START
DISPLAY (“ LASALLE COLLEGE”+ nxline)
DISPLAY (“ - - - - - - - - - - - - - -”+ nxline)
DISPLAY (“I am Dimitri, here are my grades”+ nxline)
DISPLAY (“ -Midterm (30%) is 80/100”+ nxline)
DISPLAY (“ -Final (45%) is 90/100”+ nxline)
DISPLAY (“ -Project (25%) is 50/100”+ nxline)
DISPLAY (“So, my average is ” + (80*30/100 + 90*45/100 + 50*25/100) + “/100” + nxline)
END
IV. Input instruction
This instruction will read (or recuperate) and store the value entered by the user in a variable.
A variable is a name of a reserved memory space that will be declared BExE the reading.
In Algo, to recuperate value (input) from the user, we will use the function READ()
Syntax:
READ (VariableName)
Detail:
- READ: is a keyword that will read and stores the user input in the variable between “( )”
- VariableName: a memory space, declared before the reading, the value will be stored in it.
Remark
Detail:
- Type: the nature of the value of the variable (character, numeric, Boolean)
Example:
Character: name
Numeric: grade, salary
Boolean: is empty.
Example of READ:
START
Character name
Numeric age
DISPLAY(“ FBI” + nxline)
DISPLAY(“Enter your name: ”)
READ(name)
DISPLAY(“Enter your age: ”)
READ(age)
DISPLAY(“Sir or Miss ” + name + “,” + nxline)
DISPLAY(“You are ” + age + “ years old” + nxline)
END
START
Character Add, Sub, Mul, Div
Numeric Val1, Val2
DISPLAY(“ CACULATOR” + nxline)
DISPLAY(“ - - - - - - - - - -” nxline)
DISPLAY(“Enter value 1: ")
READ(Val1)
DISPLAY("Enter value 2: ")
READ(Val2)
Add = Val1 + Val2
Sub = Val1 - Val2
Mul = Val1 * Val2
Div = Val1 / Val2
DISPLAY("The addition of" + Val1 +" and ” + Val2 + “ is ” + Add + nxline)
DISPLAY("The subtraction of" + Val1+ “ by ” + Val2 + “ is ” + Sub + nxline)
DISPLAY ("The multiplication of" + Val1+ " by ”+ Val2 + “ is ” + Mul + nxline)
DISPLAY (" The division of" + Val1 + “ by ” + Val2 + “ is ” + Div)
END
This operator “=” allows us to assign (or affect, or stare) a variable (or the result of on instruction) in
VARIABLE
Example:
Exercise
Write a group of instructions (only = instruction) that will switch (or exchange) the context of 2 variables.
Val1 = 9 9 _
Val2 = 3 9 3
- Addition ‘ + ’
Remark ( the ‘+’ between values of character type will acts like a concatenation (put them together) operator
result = “15” + “5” result result “155”
- Subtraction ‘ - ‘
Result = 15 – 5 result 10
- Multiplication ‘ * ’
- Division ‘ / ’
Result = 5 % 2 result 1
Result = 4 % 2 result 1
CHAPTER 8
V. 4) The Switch Case
The Switch Case is an elegant alternative of the nested if where the comparison operators are only the “==”
Syntax
Switch (variable)
Case value1:
blockInstruction1;
Break;
Case value 2:
Blockinstruction2;
Break;
default
Blockinstructiondefault:
Break;
Example:
If (gender == ‘f’)
{title = “Miss”;}
{title = “Miss”;}
{title = “Sir”;}
Else
.
switch (gender)
{
case 'F':
case 'f':
title = "Miss";
break;
case 'M':
case 'm':
title = "Sir";
break;
default:
title = "Miss or Sir";
break;
}
Syntax:
Details
? = keyword
Valoraction1 = a value that will be returned or an instruction that will be executed if the test is true.
Version 1:
max = (val1 > val2) ? val1 : val2;
Console.WriteLine(max);
Version 2:
Console.Write("the maximum is" +((val1 > val2) ? val1 : val2));
}
Console.WriteLine("Thanks");
60
40
20
for (Int16 number=80; number >= 10; number = number-20) ;
{
Console.Write("-value: " + number+”\n”);
}
The three loops can be used in some situations but there is a way to find the right loop.
Syntax:
Detail:
New = keyword, this will reserve a memory space for the array.
Examples:
Syntax
Example
Syntax
Visibility = keywords (public or private) that indicate the scope of our function.
Type = the type of a function is the type of the VALUE RETURNED by the function; if the function does not return a
value, its type will be “VOID”.
FunctionName = choose a meaningful name for your function, usually use a verb in a name.
Parameter = 0 or many objects (or variables) that the function needs from the OUTSIDE to be able to do its job or
task.
Examples:
public void DiplayTitle(string anytitle)
{
Console.WriteLine("\t" + anytitle.ToUpper());
Console.WriteLine("\t _________________");
}
private Int32 toSquare(Int32 value)
{
Int32 result;
result = value ^ 2;
return result;
}