What is pseudocode and how do you use it?

Codecademy
8 May 201910:50

Summary

TLDR大家好,我是Carlos,一位来自纽约布鲁克林的网页开发者。今天我们将学习伪代码的使用和编写,并通过一个算法实例演示其应用。伪代码是用简单英语编写的程序代码的简化版本,不依赖于具体编程语言,旨在帮助程序员理解和编写代码。伪代码有助于专注于解决问题而非语言语法,便于将大问题分解为小步骤。通过FizzBuzz示例,我们展示了伪代码的编写和实际代码的转换。常见日常任务也可以用伪代码练习,如做饭或洗衣,从而提升编程逻辑思维。

Takeaways

  • 😀伪代码是一种用简单英语编写的编程代码,不属于任何特定的编程语言。
  • 😀伪代码是用于帮助程序员理解和编写代码的学习和推理工具。
  • 😀伪代码允许程序员专注于解决问题,而不是语言的具体语法。
  • 😀通过伪代码,可以将复杂问题分解为更小的可管理部分。
  • 😀编写伪代码时,应使用大写字母表示关键命令,并保持缩进以明确代码块。
  • 😀每行伪代码应表达一个计算机操作,并且要具体、详细。
  • 😀伪代码应该简单易懂,即使是非技术人员也能理解。
  • 😀编写伪代码有助于提前思考问题,避免不必要的搜索和浪费时间。
  • 😀伪代码不需要严格的语法,可以根据个人习惯进行调整。
  • 😀通过练习编写伪代码,可以提高编程技能,例如将日常任务转化为伪代码。

Q & A

  • 什么是伪代码?

    -伪代码是一种用简单英语编写的程序代码,在特定编程语言中实现之前的简化版本。它没有严格的语法规则,仅代表思维方式,使其对大多数人来说都易于阅读和理解。

  • 为什么要使用伪代码?

    -伪代码帮助程序员专注于解决问题,而不被具体语言的语法困扰。它允许你使用纯粹的编程逻辑来解决问题,不需要了解特定编程语言提供的内置函数。

  • 伪代码的主要用途是什么?

    -伪代码主要用于帮助程序员理解和编写代码。它作为一种学习和推理工具,使得程序逻辑在不同编程语言之间具有通用性。

  • 编写伪代码时需要注意哪些关键点?

    -编写伪代码时,应注意以下几点:保持关键字大写、每行只写一条语句、使用缩进、确保具体描述每一步操作、保持简洁。

  • 伪代码如何帮助提高编程效率?

    -伪代码可以将复杂问题分解成小的可管理部分,提前考虑问题的各个方面,避免在编码过程中频繁查找资料,从而提高编程效率。

  • 伪代码对初学者有何帮助?

    -对初学者来说,伪代码能简化编程逻辑,使他们在学习编程语言之前,先掌握解决问题的思维方式和步骤。

  • 伪代码是否有严格的语法标准?

    -伪代码没有严格的语法标准。它是一种自由的表达方式,主要目的是清晰地描述程序的逻辑步骤。

  • 如何将日常任务转化为伪代码?

    -可以将日常任务如做饭、洗衣等步骤用简单英语描述成伪代码。例如,做拉面可以写成:1. 打开包装;2. 煮沸水;3. 放入拉面煮5分钟;4. 每分钟搅拌一次;5. 倒掉水;6. 享用拉面。

  • 什么是FizzBuzz算法?

    -FizzBuzz算法是一个常见的编程练习,要求编写一个程序,输出1到20的数字,对于3的倍数输出'Fizz',5的倍数输出'Buzz',同时为3和5的倍数输出'FizzBuzz'。

  • 如何用伪代码解决FizzBuzz问题?

    -可以用以下伪代码解决FizzBuzz问题:1. 设定一个循环从1到20;2. 如果当前数能被15整除,输出'FizzBuzz';3. 如果能被3整除,输出'Fizz';4. 如果能被5整除,输出'Buzz';5. 否则,输出当前数字。

Outlines

00:00

😀 伪代码简介及应用

Carlos介绍了伪代码的概念,它是一种用简单英语表达的编程代码,不依赖于特定编程语言。伪代码是一种学习和推理工具,帮助程序员理解和编写代码。它允许程序员抽象掉语言的语法细节,专注于解决问题的逻辑。此外,伪代码有助于将复杂问题分解为更小、更易于管理的部分,并且能够预见问题,提前规划解决方案。Carlos还强调了在编程前先写伪代码的重要性,这有助于更有效地使用搜索引擎,避免浪费时间。

05:01

😉 如何编写伪代码

Carlos提供了编写伪代码的一些建议,包括使用大写字母表示关键字(如if语句),每行只写一个语句,使用缩进来区分代码块,确保具体性,以及保持简单性,避免使用过多的技术术语。他通过一个经典的编程问题'FizzBuzz'来演示如何将这些建议应用到实际中。在这个问题中,需要打印从1到20的数字,但遇到3的倍数打印'fizz',5的倍数打印'buzz',同时是3和5的倍数则打印'fizzbuzz'。Carlos展示了如何使用for循环和if语句来构建伪代码,并强调了缩进和关键字大写的重要性。

10:03

👍 伪代码的实际应用示例

Carlos通过一个实际的例子——煮拉面的步骤——来展示如何将日常生活中的任务转化为伪代码。他详细描述了从打开包装、加水、煮沸、煮面、搅拌到最终享用的整个过程。这个例子说明了伪代码不仅限于编程,也可以应用于日常生活中的各种任务,帮助人们以结构化的方式思考和规划行动。通过这种方式,伪代码的编写和理解变得更加直观和易于掌握。

Mindmap

Keywords

💡伪代码

伪代码是一种简化的编程代码版本,用于在特定编程语言实现之前以简单的英语描述程序。它没有严格的语法,旨在帮助程序员理解和编写代码。视频中提到,伪代码是一种学习和推理工具,适用于任何编程语言。

💡语言无关

伪代码与特定的编程语言无关,这意味着它是一种用简单英语书写的步骤说明,而不是实际的代码。视频中强调了伪代码的这一特性,使其更易于理解和交流。

💡模块

模块(modulo)运算符用于检查一个数字是否可以被另一个数字整除。视频中,模块运算符被用来确定数字是否是3或5的倍数,例如在FizzBuzz问题中,用模块运算符来打印“fizz”、“buzz”或“fizzbuzz”。

💡条件语句

条件语句用于根据不同的条件执行不同的代码块。视频中展示了如何在伪代码和实际代码中使用if和else if语句来实现FizzBuzz算法。

💡循环

循环(例如for循环)用于重复执行代码块。视频中使用for循环来遍历1到20的数字,并根据不同的条件打印相应的结果。

💡缩进

缩进用于在代码中显示不同的层次结构,使代码更易读。视频中强调在伪代码和实际代码中使用缩进来明确显示代码块的层次。

💡代码块

代码块是由一组相关的代码语句组成的部分。视频中展示了如何在if语句和for循环中使用代码块,以实现逻辑的清晰分离。

💡具体化

具体化指的是在伪代码中详细描述每一步骤,使其易于理解。视频中建议在编写伪代码时详细描述每个步骤,以确保每个人都能理解发生了什么。

💡简单性

伪代码应保持简单,以便即使是外行或客户也能理解。视频中强调,不要在伪代码中使用过多的技术术语或复杂的表达方式。

💡FizzBuzz

FizzBuzz是一个经典的编程面试题,要求编写一个程序,从1到20打印数字,但对3的倍数打印“fizz”,对5的倍数打印“buzz”,对既是3又是5的倍数打印“fizzbuzz”。视频中用FizzBuzz作为示例,展示了如何编写伪代码并将其转化为实际代码。

Highlights

介绍什么是伪代码及其用途:伪代码是一种用简单英语编写的编程代码,在实现为特定编程语言之前使用,主要用于帮助程序员理解和编写代码。

伪代码的重要性:伪代码不是编程语言,而是一种学习和推理工具,可以帮助程序员专注于解决问题的逻辑,而不被具体语言的语法所困扰。

伪代码的语言无关性:伪代码是语言无关的,任何人都可以轻松阅读和理解其步骤。

使用伪代码的好处:伪代码抽象掉了语法细节,让你可以专注于解决当前问题的逻辑,帮助你成为更好的程序员。

伪代码帮助分解问题:伪代码有助于将大问题分解成更小的、可管理的部分,从而更好地思考和解决问题。

写伪代码的建议:伪代码没有严格的书写规范,但有一些建议,如使用大写字母写关键字、每行写一个语句、使用缩进等。

保持伪代码简单明了:伪代码应易于理解,不应包含过多技术术语,以便即使是外行或客户也能理解。

FizzBuzz问题示例:展示如何用伪代码解决经典的面试问题FizzBuzz,包括用伪代码描述和实际编写代码。

编写伪代码的步骤:先写for循环,然后写多个if条件判断,最后输出结果。

模块运算符的使用:通过模块运算符判断一个数是否能被另一个数整除,并根据条件输出相应的结果。

从伪代码到实际代码的转化:详细演示如何将伪代码转化为实际的编程代码。

学习编写伪代码的建议:多写伪代码,并将日常任务(如煮拉面、洗衣服等)转化为伪代码进行练习。

伪代码的实践:通过实践,你会找到适合自己书写伪代码的方式,并使之变得更加自然。

伪代码的灵活性:伪代码的书写没有固定格式,关键是要清晰表达逻辑。

提高编程技能:通过在Codecademy上学习,可以进一步提高编程技能和对伪代码的理解。

Transcripts

play00:00

hi everyone my name is Carlos I'm a web

play00:02

developer based in Brooklyn New York and

play00:04

today we're gonna look into pseudocode

play00:06

we're gonna see how to use it how to

play00:07

write it and we're gonna look at an

play00:09

example where we actually use it to

play00:10

solve a coding algorithm so what is

play00:13

pseudocode

play00:14

well pseudocode is a simpler version of

play00:16

a programming code in plain English

play00:18

before it is implemented in a specific

play00:20

programming language pseudocode is often

play00:23

referred to as a syntactical

play00:25

representation of a program and it

play00:27

doesn't have a strict syntax since it

play00:28

only represents the way we're thinking

play00:30

so it should leave very little for the

play00:32

imagination so it's readable for most

play00:34

people one of the most important keys to

play00:37

understand about pseudocode is that it's

play00:39

not a programming language it's actually

play00:42

a learning and reasoning tool used to

play00:44

help programmers understand and write

play00:46

code so it's language agnostic it's

play00:49

something that you're right that is not

play00:50

an actual code in any programming

play00:52

language but that if anyone were to read

play00:54

it it would be very clear to what is

play00:56

happening or what steps are being taken

play00:58

so what pseudo code actually enables us

play01:01

to do is to grab a simple set of

play01:03

instructions that are written in plain

play01:04

English and eventually translate that

play01:06

into a program that can be executed now

play01:09

there are a few other reasons why we

play01:11

would use pseudocode for starters

play01:14

pseudocode abstracts away syntax to let

play01:17

you focus on solving the problem in

play01:19

front of you so instead of getting

play01:20

bogged down in the exact syntax of a

play01:23

language pseudocode allows you to work

play01:24

almost impure programming logic this way

play01:27

you don't actually have to know what

play01:28

exact built-in functions a programming

play01:31

language provides you can simply write

play01:33

down in plain English what it is you're

play01:35

trying to do so why else use pseudocode

play01:38

well some people can visualize the

play01:40

entire solution to an algorithmic

play01:42

problem in their mind while others may

play01:44

find it difficult to come up with a

play01:45

solution without having to first write

play01:48

it down somewhere in general pseudocode

play01:50

won't do any harm if not any good it

play01:53

essentially helps us break down large

play01:55

problems into smaller manageable pieces

play01:57

through this running pseudocode allows

play02:00

you to think through a problem with some

play02:02

foresight and lets you anticipate

play02:04

important questions before they arise

play02:06

this is possible because they're

play02:07

breaking down something into multiple

play02:09

steps oftentimes when faced with

play02:11

problems a common gutter

play02:13

action is to jump straight into Google

play02:15

but there are ways of googling that are

play02:16

helpful and some that are actually

play02:18

counterproductive so if you turn to

play02:20

Google before thinking you're not

play02:21

actually thinking like a programmer so

play02:23

if you don't take time to write

play02:25

pseudocode and think through the details

play02:27

of the project you'll end up googling in

play02:29

an efficient way

play02:30

you'll end up wasting time so in the

play02:33

short term even though you're writing

play02:34

pseudocode might feel like you're taking

play02:36

two steps back to go one step forward it

play02:39

is actually saving some time and

play02:40

headaches along the way and breaking

play02:42

things down with pure programming logic

play02:44

and then googling or research and what

play02:46

you need to look for will essentially

play02:47

help you in becoming a better programmer

play02:49

so now let's go ahead and see how to

play02:51

actually write pseudocode and then we'll

play02:53

solve an algorithm together now

play02:56

pseudocode doesn't have a strict

play02:57

systematic way or standard way of being

play03:01

written so don't think of these as rules

play03:03

think of these as more suggestions on

play03:05

how to get started writing pseudocode so

play03:08

for starters you may need to capitalize

play03:09

Kumasi will remain in the actual code so

play03:12

if you're using if statements for

play03:14

example it's important to keep those if

play03:16

statements in capital letters this makes

play03:19

it easier to create code blocks and

play03:21

actually spots some of the logic next

play03:24

step is to actually stick to writing one

play03:26

statement per lime so each statement in

play03:28

your pseudocode should express just one

play03:30

action for the computer in most cases if

play03:33

the task list is properly drawn then

play03:35

each task will correspond to one line of

play03:37

pseudocode another really important one

play03:39

is to use indentation so using white

play03:42

spaces between blocks of text will help

play03:44

keep different components of your

play03:46

pseudocode isolated and indenting

play03:48

different pieces of each block will

play03:50

indicate that those pieces of pseudocode

play03:52

go under a less indented section you

play03:55

also want to make sure you're being

play03:56

specific so everything that is happening

play03:58

in the process must be described

play04:00

completely pseudocode statements are

play04:03

close to simple English statements and

play04:05

will be very easy for the person who's

play04:07

reading it if you're actually being

play04:08

specific and detailed for each step

play04:11

lastly you want to keep things simple

play04:13

don't write pseudocode in a complete

play04:15

programmatic manner it is necessary to

play04:17

be simple to understand even for a

play04:19

layman or a client so don't incorporate

play04:21

too many technical terms and make things

play04:24

overly complex

play04:25

okay so now that we have an idea of how

play04:27

to write pseudocode let's go ahead and

play04:29

take a look at an algorithm and start

play04:31

writing our own pseudocode and solution

play04:33

with it so this is a classic interview

play04:35

question and coding challenge that is

play04:37

very popular and you might have seen

play04:38

before

play04:39

it's called fizzbuzz so here are the

play04:41

instructions for fizzbuzz what we're

play04:43

gonna do is we're gonna write a short

play04:45

program that prints each number from 1

play04:47

to 20 on a new line for each multiple of

play04:50

3 we're gonna print fizz instead of the

play04:52

number for each multiple of 5 we're

play04:54

gonna print Buzz instead the number and

play04:56

four numbers which are multiples of both

play04:58

3 & 5 we're gonna print fizzbuzz instead

play05:01

of the number okay so let's get started

play05:04

so I'm using Chrome here and I'm using

play05:06

my dev tools console in order to

play05:08

actually solve the problem I've also

play05:10

copied and pasted the instructions so

play05:12

it's easier to break down what exactly

play05:14

we're gonna be doing okay so first thing

play05:17

I notice is that we're gonna be running

play05:18

a program that produces a set of 20

play05:21

different values so this looks like a

play05:23

perfect situation for a for loop so we

play05:26

know a for loop is written with three

play05:28

optional expressions so the first one

play05:31

will be the initialization expression so

play05:33

we're gonna be sending out the counter

play05:34

to one then we're going to add a

play05:37

condition for the counter to actually

play05:38

break with and lastly we're gonna add a

play05:41

final expression which will be the

play05:42

incrementation number so once we have

play05:46

our for loop created we let's look back

play05:49

at the problem where you have about four

play05:51

different conditions we're gonna be

play05:53

working with this means within our for

play05:55

loop we're gonna have four different if

play05:57

statements that actually give out

play05:59

different results depending on the

play06:00

number we're on so once we have our for

play06:03

loop here let's make sure we are also

play06:04

using indentation so let's get started

play06:06

with our first conditional now if we

play06:09

want to check if a number is divisible

play06:10

by another number we can use our

play06:13

remainder operator called modulo so we

play06:16

use modulo on a number and it returns

play06:18

zero which means that remainder is zero

play06:20

it means that it was actually divisible

play06:22

by that number and in one of the cases

play06:24

were actually checking if a number is

play06:26

divisible by both multiples of three and

play06:28

five if so we're gonna print fizzbuzz so

play06:32

in order to make this short and sweet we

play06:34

can actually just check if it's modulo

play06:36

15 if its remainder is zero then

play06:39

it's true and we can just print fizzbuzz

play06:41

from there so I'm gonna keep some of the

play06:44

key commands in capital letters like my

play06:46

if and mod and I'm going to use some

play06:49

indentation under my if statement to

play06:51

make sure that there's a code block and

play06:53

within that if statement so for the next

play06:54

conditional we want to check if the

play06:56

number is divisible by three so else if

play07:01

number mod three is equal to zero then

play07:06

we're going to be printing this after

play07:14

that we want to check if the number is

play07:15

divisible by five and we're going to be

play07:18

printing buzz for that one so you can

play07:21

see I'm basically following the same

play07:23

kind of pattern here it's non following

play07:27

a strict rigid syntax but it kind of

play07:29

keeps the capital letters for the key

play07:31

commands and I'm keeping indentation to

play07:33

make sure that there's a code block

play07:34

within each statement and for the last

play07:37

conditional which indicates that it is

play07:40

not divisible by any of those numbers 3

play07:42

5 or 15 we're simply going to print the

play07:45

number as is and there you go that is

play07:48

essentially our pseudo code so once our

play07:51

pseudo code is completed it's fairly

play07:53

easy to actually translate this to code

play07:55

if you actually have the steps pretty

play07:58

detailed and and laid out for you so I'm

play08:00

going to scroll this further down a

play08:01

little bit and I'm actually going to

play08:03

zoom out so we can see or pretty much

play08:05

what we're writing here

play08:06

so let's start out with our for loop

play08:08

here and we know that the for loop will

play08:10

start with the number 1 we'll keep going

play08:14

until it's less than or equal to 20 and

play08:16

we'll be incrementing it by 1 let's make

play08:21

sure the first if statement is added so

play08:24

we want to check if I is modulo and we

play08:28

use a percentage operator for this one

play08:30

is equal to 0 and if that's true we're

play08:33

going to actually console.log fizz buzz

play08:39

or close that if statements or write the

play08:42

second one so we want to check if the

play08:45

number is modulo 3 is equal to zero if

play08:50

so we're going to

play08:52

console.log fizz then we want to check

play08:59

if the number is divisible by five so i

play09:03

modulo five equals zero we want to print

play09:10

buzz for the capital b and lastly you

play09:20

simply have to print the number itself

play09:22

which is i will close that up close up

play09:26

the for loop and here you go we have the

play09:29

results here I'll zoom out a little

play09:31

further so you can see you can see all

play09:33

the numbers that are multiples of three

play09:35

are printed out as fizz all the numbers

play09:37

that are multiples of 5 are buzzed and

play09:39

the numbers that are multiples of 3 and

play09:42

5 which is 15 only here is fizzbuzz now

play09:46

the more you write pseudo-code the more

play09:48

you will find a way to write at them

play09:49

each sense to you specifically and

play09:51

running good pseudocode that makes sense

play09:54

to you is simply a matter of practice so

play09:56

what is some way you can actually

play09:58

practice in your everyday life

play10:03

so let's take a look at cooking ramen as

play10:06

pseudo-code well with cooking ramen the

play10:08

first thing you would do is open the

play10:09

packet then you might feel a pot with

play10:11

water then you might bring the water to

play10:13

a boil before putting the ramen in for 5

play10:16

minutes then you'll stir the ramen once

play10:18

every minutes and then after 5 minutes

play10:20

you drain the water before you actually

play10:22

enjoy it so you can turn everyday tasks

play10:24

for example doing laundry or cooking

play10:26

something else going out to a store and

play10:28

purchasing coffee into pseudocode and

play10:31

this will help you basically write

play10:33

pseudocode in a way that makes more

play10:35

sense to you so hopefully this gave you

play10:37

a better understanding on how to write

play10:39

pseudocode and how to use it they go for

play10:41

watching join the conversation by

play10:43

subscribing to this channel or drop in a

play10:45

comment below and if you want to take

play10:46

your skills to the next level start

play10:48

learning on Codecademy today

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
伪代码编程基础算法学习编程逻辑学习工具语言无关编程思维问题解决代码示例学习技巧
Benötigen Sie eine Zusammenfassung auf Englisch?