Operators, strings and subroutines
Introducido
13:08
Programación
Aquí hay un programa corto. Lee cinco notas de prueba e imprime el total. Las cinco notas son ocho, nueve, siete, seis y diez, así que el total debería ser cuarenta. Mira…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| program/ˈprəʊɡræm/ | A set of instructions specifying operations for a computer to carry out. |
| values/ˈvæljuːz/ | |
| operators/ˈɒpəreɪtəz/ | operadores |
| strings/strɪŋz/ | LENGTH, SUBSTRING , UCASE, LCASE. |
| code/kəʊd/ | |
| comments/ˈkɒments/ | Explanatory text in source code or markup that does not execute as a program instruction. |
| Arithmetic/əˈrɪθmətɪk/ | |
| power/ˈpaʊə/ | |
| library routines/ˈlaɪbrəri ruːˈtiːnz/ | Reusable functions or procedures supplied in a collection for programs to call. |
| remainder/rɪˈmeɪndə/ | For integer division here, the amount left after taking whole groups of the divisor. |
| Relational/rɪˈleɪʃənl/ | |
| Boolean/ˈbuːlɪən/ | A logical value that is either true or false. |
| Logical/ˈlɒdʒɪkl/ | |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| LENGTH/leŋθ/ | |
| SUBSTRING/ˈsʌbstrɪŋ/ | |
| UCASE | |
| LCASE | |
| procedure/prəˈsiːdʒə/ | procedimiento |
| function/ˈfʌŋkʃn/ | función |
| RETURN/rɪˈtɜːn/ | |
| parameter/pəˈræmɪtə/ | parámetro |
| local variable/ˈləʊkl ˈveərɪəbl/ | variable local |
| subroutine/ˈsʌbruːtiːn/ | |
| global/ˈɡləʊbl/ | |
| CALL/kɔːl/ |
Las herramientas de un programa
- Los programas combinan valores con operadores y trabajan con cadenas.
- Para evitar repetir código, utilizamos procedimientos y funciones.
- Un buen nombramiento y comentarios hacen que un programa sea mantenible.
Operadores
- Aritméticos:
+ - * /,^(potencia),MOD(resto),DIV(parte entera). Ej.:17 MOD 5 = 2,17 DIV 5 = 3. - Relacionales (devuelven un Booleano):
= < <= > >= <>(distinto de). - Lógicos:
AND(ambos verdaderos),OR(al menos uno),NOT(invierte).

Un bucle de pre-condición (WHILE) verifica antes del cuerpo, por lo que puede ejecutarse cero veces; un bucle de post-condición (REPEAT) verifica después, por lo que se ejecuta al menos una vez.

What is 17 MOD 5?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2.
What is 17 DIV 5?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded).
Manejo de cadenas
- Una cadena está compuesta por caracteres. Operaciones útiles:
- LENGTH (número de caracteres), SUBSTRING (una parte), UCASE (mayúsculas), LCASE (minúsculas).
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"

Una variable es un almacenamiento con nombre cuyo valor puede cambiar
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
"Computer" has 8 characters.
Match each operator or string function to what it gives.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings.
Procedimientos, funciones y buen estilo
- Un procedimiento realiza una tarea (se ejecuta con
CALL); una función devuelve un valor. - Un parámetro es un valor que se pasa (hasta tres).
- Una variable local existe solo dentro de su subrutina (más segura); una global es visible en todas partes.
- Rutinas de biblioteca:
MOD,DIV,ROUND,RANDOM. - Un programa mantenible utiliza nombres significativos, comentarios y subrutinas.

Los tipos de datos básicos: entero, real, char, string y boolean
A local variable:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
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.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Worked example: minutes to hours
A film lasts 135 minutes. Use DIV and MOD to calculate its length in hours and minutes, then write a function that returns 返回 the whole number of hours.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
23 = 5 × 4 + 3, so the remainder is 3.
Name ← "Shanghai". What does SUBSTRING(Name, 1, 5) return?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
It takes 5 characters starting at position 1.
Ya lo tienes
MOD= resto,DIV= parte entera; lógicosAND/OR/NOT- cadenas: LENGTH, SUBSTRING, UCASE, LCASE
- un procedimiento realiza una tarea; una función devuelve un valor; los parámetros pasan valores
- las variables locales son más seguras que las globales; mantenible = nombres significativos + comentarios + subrutinas
¿Qué salió mal? Solo el equipo del sitio ve esta información.