Operators, strings and subroutines
Diperkenalkan
13:08
Pemrograman
Berikut program pendek. Program ini membaca lima nilai ujian tes dan mencetak totalnya. Lima nilai tersebut adalah delapan, sembilan, tujuh, enam, dan sepuluh, sehingga total seharusnya empat puluh. Perhatikan…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
In this lesson
| English | Makna |
|---|---|
| operators/ˈɒpəreɪtəz/ | operator |
| procedure/prəˈsiːdʒə/ | |
| function/ˈfʌŋkʃn/ | |
| relational/rɪˈleɪʃənl/ | |
| logical/ˈlɒdʒɪkl/ | |
| parameter/pəˈræmɪtə/ | |
| local variable/ˈləʊkl ˈveərɪəbl/ | |
| global/ˈɡləʊbl/ |
The tools of a program
- Programs combine values with operators 运算符 and work with strings.
- To avoid repeating code, we use procedures 过程 and functions 函数.
- Good naming and comments make a program maintainable.
Operators
- Arithmetic:
+ - * /,^(power),MOD(remainder),DIV(whole-number part). e.g.17 MOD 5 = 2,17 DIV 5 = 3. - Relational 关系 (give a Boolean):
= < <= > >= <>(not equal). - Logical 逻辑:
AND(both true),OR(at least one),NOT(reverses).

What is 17 MOD 5?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2.
What is 17 DIV 5?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded).
String handling
- A string is made of characters. Useful operations:
- LENGTH (number of characters), SUBSTRING (a part), UCASE (capitals), LCASE (small).
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"
SUBSTRING: take part of a string
The first character is at position 1. SUBSTRING(Word, Start, Length) takes Length characters beginning at Start — drag the two handles and read the call it builds.
What does LENGTH("Computer") return?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
"Computer" has 8 characters.
Match each operator or string function to what it gives.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings.
Procedures, functions and good style
- A procedure does a task (run with
CALL); a function returns a value. - A parameter 参数 is a value passed in (up to three).
- A local variable 局部变量 exists only inside its subroutine (safer); a global 全局 is visible everywhere.
- Library routines:
MOD,DIV,ROUND,RANDOM. - A maintainable program uses meaningful names, comments, and subroutines.
A local variable:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Locals are safer because other parts of the program cannot change them by mistake.
A function returns a value (used in an expression) while a procedure just performs a task, and a local variable can only be used inside the subroutine where it is declared.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain.
Worked example: minutes to hours
A film lasts 135 minutes. Use DIV and MOD to output it in hours and minutes, then write it as a function.
DIV(135, 60)= 2 (the whole number of hours) andMOD(135, 60)= 15 (the remainder), so the film is 2 hours 15 minutes.- As a function with a parameter, it returns a value to the program that called it:
FUNCTION Hours(Mins : INTEGER) RETURNS INTEGER
RETURN DIV(Mins, 60)
ENDFUNCTION
What is 23 MOD 4?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
23 = 5 × 4 + 3, so the remainder is 3.
Name ← "Shanghai". What does SUBSTRING(Name, 1, 5) return?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
It takes 5 characters starting at position 1.
You've got it
MOD= remainder,DIV= whole-number part; logicalAND/OR/NOT- strings: LENGTH, SUBSTRING, UCASE, LCASE
- a procedure does a task; a function returns a value; parameters pass values in
- local variables are safer than global; maintainable = meaningful names + comments + subroutines