Chapter 1 / 第一章:什么是编程?
第一章:什么是编程?
Chapter 1: What is Programming?
---
想象一下这样的场景:清晨,你被手机的闹钟声唤醒。赖床五分钟——这个简单的动作,背后却隐藏着复杂的逻辑。闹钟响起,屏幕亮起,你点击“稍后提醒”,系统记录下时间,五分钟后再次响起。如果你连续点击三次,系统可能会问你:“还要继续睡吗?”或者直接切换到更高音量的铃声。这个看似简单的交互过程,正是编程的魔力所在。
Now, imagine this scenario: You wake up to your phone's alarm. You want to sleep five more minutes—you hit "snooze." This simple action involves complex logic behind the scenes. The alarm goes off, the screen lights up, you tap "snooze," the system records the time, and five minutes later, it rings again. If you hit snooze three times in a row, the system might ask, "Still want to keep sleeping?" or switch to a louder ringtone. This seemingly simple interaction is the magic of programming.
每当你滑动手机屏幕、发送微信消息、玩电子游戏、甚至在自动售货机上选择饮料时,你都在与程序进行对话。而这些程序的背后,是无数程序员用代码编写出的指令。今天,我们将开启一段奇妙的旅程——学习如何成为那个“指挥”计算机的人。
Every time you swipe your phone screen, send a WeChat message, play a video game, or select a drink from a vending machine, you're having a conversation with a program. Behind these programs are countless programmers who wrote instructions in code. Today, we're beginning a wonderful journey—learning how to become the person who "commands" computers.
---
什么是编程?
What is Programming?
编程的简单定义
A Simple Definition of Programming
编程(Programming)是用计算机能理解的语言,编写一系列指令,让计算机按照我们的想法去执行任务的过程。如果用做饭来比喻,编程就像是写一份食谱。你告诉厨师:“先加热锅,倒入油,放入鸡蛋,翻炒三分钟,出锅。”厨师会按照这个步骤执行,最终做出美味的炒蛋。编程也是如此——你给计算机写“食谱”,计算机会一丝不苟地按照步骤执行。
Programming is the process of writing a series of instructions in a language that computers can understand, so that computers execute tasks according to our intentions. If we compare it to cooking, programming is like writing a recipe. You tell the chef: "First, heat the pan, add oil, crack in the egg, stir-fry for three minutes, then serve." The chef follows these steps and makes a delicious scrambled egg. Programming works the same way—you write a "recipe" for the computer, and the computer follows the steps precisely.
编程与写代码的区别
The Difference Between Programming and Writing Code
很多人误以为编程就是坐在电脑前疯狂敲键盘,写出一行行神秘的代码。诚然,代码(Code)是编程的重要产物,但编程的内涵远比“敲代码”丰富得多。
Many people mistakenly believe that programming means sitting at a computer and frantically typing mysterious lines of code. Indeed, code is an important product of programming, but programming encompasses far more than just "typing code."
想象一下,如果你要设计一款帮同学们自动排座位的程序,你会怎么做?
Imagine you need to design a program that automatically assigns seats for classmates. What would you do?
首先,你需要思考:
First, you need to think:
- 排座位的规则是什么?(按身高?按学号?还是自由选择?)
- What are the rules for assigning seats? (By height? By student number? Or free choice?)
- 有人提出特殊要求怎么办?(视力不好的同学要坐前排?)
- What if someone has special requirements? (Students with poor eyesight should sit in front?)
- 如何让这个程序使用起来简单直观?
- How do we make this program easy and intuitive to use?
然后,你才会进入写代码的环节。思考的过程往往比写代码更重要。一个优秀的程序员,可能90%的时间都在思考和规划,只有10%的时间在真正写代码。
Then, you'll enter the phase of writing code. The thinking process is often more important than actually writing code. An excellent programmer might spend 90% of their time thinking and planning, and only 10% actually writing code.
编程是一种思维方式
Programming is a Way of Thinking
让我们换一个角度。编程不仅仅是告诉计算机做什么,更是一种思维方式——一种把复杂问题拆解成简单步骤的思维方式,一种寻找规律、解决问题的思维方式。这种思维方式,我们称之为计算思维(Computational Thinking)。
Let's look at it from another angle. Programming isn't just about telling computers what to do—it's a way of thinking—a way of breaking complex problems into simple steps, a way of finding patterns and solving problems. We call this way of thinking Computational Thinking.
当你学会用计算思维去看待世界,你会发现:
When you learn to view the world through computational thinking, you'll discover:
- 排队买午餐时,你会思考:如何设计队列让等待时间最短?
- When queuing for lunch, you think: How can we design the queue to minimize wait time?
- 整理房间时,你会思考:按照什么顺序整理最有效率?
- When tidying your room, you think: What's the most efficient order to clean?
- 考试复习时,你会思考:哪些知识点需要重点复习,如何安排时间?
- When reviewing for exams, you think: Which knowledge points need extra focus, and how should I schedule my time?
计算思维不仅仅是为了编程,它是一种让人变得更聪明、更高效的生活技能。
Computational thinking isn't just for programming—it's a life skill that makes you smarter and more efficient.
---
计算思维:编程的灵魂
Computational Thinking: The Soul of Programming
计算思维是由计算机科学家Jeannette Wing在2006年提出的概念。她认为,计算思维是每个人都需要具备的基本技能,就像阅读、写作和算术一样重要。
Computational Thinking was a concept proposed by computer scientist Jeannette Wing in 2006. She argued that computational thinking is a fundamental skill everyone needs, as important as reading, writing, and arithmetic.
计算思维包含四个核心要素:
Computational thinking consists of four core elements:
1. 分解(Decomposition)
分解是把一个大问题拆分成若干个小问题的过程。就像拆解一台复杂的机器,我们需要了解每个零件的功能,才能理解整台机器是如何工作的。
Decomposition is the process of breaking a large problem into smaller sub-problems. Like disassembling a complex machine, we need to understand the function of each part before we can understand how the whole machine works.
例子:策划一场班级生日派对
Example: Planning a class birthday party
这个任务看起来很简单,但其实包含很多子任务:
This task seems simple, but it actually contains many sub-tasks:
- 确定日期和时间 → 需要协调大家的空闲时间
- Deciding the date and time → Need to coordinate everyone's availability
- 选择地点 → 教室?餐厅?某位同学家?
- Choosing a location → Classroom? Restaurant? A classmate's house?
- 准备食物 → 蛋糕、饮料、零食
- Preparing food → Cake, drinks, snacks
- 准备活动 → 游戏、礼物交换
- Planning activities → Games, gift exchanges
- 布置场地 → 气球、彩带、装饰品
- Decorating the venue → Balloons, streamers, decorations
通过分解,原本令人头疼的大任务变成了一个个可以逐一解决的小任务。
Through decomposition, what seemed like an overwhelming task becomes a series of smaller tasks that can be solved one by one.
2. 模式识别(Pattern Recognition)
模式识别是找出问题中重复出现的规律。很多看似不同的问题,其实遵循相同的模式。一旦识别出模式,我们就可以用相同的方法来解决类似的问题。
Pattern Recognition is finding recurring patterns in problems. Many seemingly different problems actually follow the same patterns. Once we identify a pattern, we can use the same method to solve similar problems.
例子:数列中的规律
Example: Patterns in number sequences
观察以下数列:2, 4, 6, 8, 10, ?
观察以下数列:What comes next in this sequence: 2, 4, 6, 8, 10, __?
你可能已经发现了规律:每个数都比前一个数大2。那么下一个数就是12。这就是模式识别在起作用。
You probably noticed the pattern: each number is 2 greater than the previous one. So the next number is 12. This is pattern recognition at work.
在编程中,模式识别帮助我们发现代码中重复的结构,可以简化代码、提高效率。比如,你发现每次写程序都要“询问用户名字”,这个动作可能重复出现很多次。有了模式识别的意识,你就可以把这个重复的动作写成一个可重用的模块,以后只需要调用它就可以了。
In programming, pattern recognition helps us discover repetitive structures in code, which can simplify code and improve efficiency. For example, if you notice that you always need to "ask the user for their name" when writing a program, and this action appears many times, with pattern recognition awareness, you can write this repetitive action as a reusable module that you can call whenever needed.
3. 抽象(Abstraction)
抽象是从纷繁复杂的细节中,抽取出最本质、最核心的信息,忽略那些不重要的细节。就像看地图时,我们不需要知道每条街道上种了多少棵树,只需要知道道路的走向和重要地标。
Abstraction means extracting the most essential and core information from complex details, while ignoring unimportant details. Like looking at a map—we don't need to know how many trees are planted on each street, only the road directions and important landmarks.
例子:设计一个学校管理系统
Example: Designing a school management system
假设你要设计一个学校管理系统。学生有很多信息:姓名、年龄、身高、鞋码、喜欢的颜色、讨厌的食物、血型、过敏源、家长联系方式……
Suppose you're designing a school management system. Students have lots of information: name, age, height, shoe size, favorite color, foods they dislike, blood type, allergies, parent contact information...
如果把所有信息都放进系统,会非常复杂。但通过抽象,我们可以只关注与系统功能相关的信息。比如,一个成绩管理系统可能只需要知道学生的姓名、学号和各科成绩,而不需要知道他们的鞋码和喜欢的颜色。这就是抽象的力量——帮助我们在复杂的世界中聚焦真正重要的事物。
Including all this information in the system would be extremely complex. But through abstraction, we can focus only on information relevant to the system's function. For example, a grade management system