If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: AP®︎/College Computer Science Principles>Unit 4

Lesson 1: Building algorithms

# Expressing an algorithm

We can express an algorithm many ways, including natural language, flow charts, pseudocode, and of course, actual programming languages.
Natural language is a popular choice, since it comes so naturally to us and can convey the steps of an algorithm to a wide audience. When we're developing algorithms, we are often working both with people who know programming and those who don't—but they all know natural language.
However, natural language has its drawbacks. It has a tendency to be ambiguous and too vaguely defined, since it has no imposed structure. That makes it difficult for others to follow the algorithm and feel confident in its correctness. Flow charts and pseudocode are more structured formats that can more precisely express an algorithm, and are popular with computer scientists and programmers.
Let's try out the more structured formats by expressing the Pig Latin algorithm from the previous article.

### Flow charts

A more formal way to express an algorithm is with a flow chart, a diagram with boxes connected by arrows.
To start simple, here's a flow chart for the basic version of the Pig Latin algorithm:
Each rectangle represents a step in the sequence, and the arrows flow from one step to the next.
This next flow chart is for the improved algorithm and uses a diamond to represent the selection phase:
Finally, this flow chart visualizes the complete algorithm with iteration:
Expressing an algorithm in a flow chart allows us to visualize the algorithm at a high level, plus it forces us to think very carefully about sequencing and selection. Which arrow goes to what node? Are there missing arrows? Those are the kinds of valuable questions that can come up while translating an algorithm into a flow chart.

### Pseudocode

Ultimately, most algorithms become code that actually runs on a computer. Before that happens, programmers often like to express an algorithm in pseudocode: code that uses all the constructs of a programming language, but doesn't actually run anywhere.
Here's the Pig Latin algorithm written in the style of AP CSP pseudocode:
``````FOR EACH word IN words
{
APPEND(word, "-")
letter ← FIRST_LETTER(word)
IF (IS_VOWEL(letter)) {
APPEND(word, "yay")
} ELSE {
APPEND(word, letter)
APPEND(word, "ay")
REMOVE_FIRST(word)
}
}``````
Every programmer writes pseudocode differently, since there is no official standard, so you may run into pseudo-code that looks very different.
Expressing an algorithm in pseudocode helps a programmer think in familiar terms without worrying about syntax and specifics. It also gives computer scientists a language-independent way to express an algorithm, so that programmers from any language can come along, read the pseudo-code, and translate it into their language of choice.

### Programming languages

Once we've planned our algorithm, whether in natural language, flow charts, pseudocode, or a combination, it's time to turn it into running code.
We'll translate the Pig Latin algorithm to JavaScript, since that's our language of choice on Khan Academy. We can use a `for` loop for the repetition, an `if`/`else` for the selection, and then a mix of string and array operations for the steps.
📝 See similar code in: App Lab | Snap | Python
An algorithm can be translated into any general purpose programming language. For proof, just check out RosettaCode.org, a wiki that lists hundreds of algorithms translated to over 700 languages.
Software is written in a variety of programming languages, depending on what it does and who's making it. Khan Academy currently uses four different languages for different parts of the codebases. Fortunately, we can use the same algorithms everywhere!

## Want to join the conversation?

• Why does Khan Academy use 4 different programming languages? Why not consolidate to one language?
• There are different reasons for each language. We use JavaScript for the frontend, since JavaScript is the language understood by web browsers. On the backend, we currently use a mix of Python and Kotlin. However, we are moving the backend over to Go, for performance reasons. So we will have a mix of those three languages for a while, since there is a lot of code to upgrade!
(Also, our data analytics team uses a mix of Python and R, two languages that have a lot of available libraries for data processing)
• Pig Latin -
If a word starts with a consonant and a vowel, put the first letter of the word at the end of the word and add "ay."
If a word starts with two consonants move the two consonants to the end of the word and add "ay."
If a word starts with a vowel add the word "way" at the end of the word.

to help anyone unfamiliar with the concept of pig latin
• Algorithms are what are used to create path-finding, correct?
• Could you post some links to information on using algorithms for creating artificial intelligence?
• Why use so many code languages rather than just one?
(1 vote)
• Why have so many different types of tools rather than just using hammers or screwdrivers? It's because each one has different uses and strengths/weaknesses. Over time, we develop better ones and encounter situations that require something new. For example, SQL is good at querying databases, but would not be used to create games.
• What are the "STORE words" & "FOR word IN words" steps in the flowchart example for?
(1 vote)
• "STORE words" just means to keep a list of the words in the sentence, then "FOR word IN words" means that for each word that was in the list of words we stored, do stuff using that word
(1 vote)
• Is 4 the limited number of languages?
(1 vote)
• No according to the article, Khan Academy uses four different languages for different parts of the site. That's specific to this site, there are hundreds of programming languages. But you can take algorithm and implement it in hundreds of different programming languages, in that way algorithms are universal. Although some languages are better at some algorithms than others.
(1 vote)
• how does this work?
(1 vote)
• Whats the point of having multiple coding languages? Wouldnt just one be easier?
(1 vote)
• Much like tools (such as hammers and drills), different programming languages have their own uses and strengths/weaknesses. Over time we develop better languages or encounter situations that require a new language. For example, SQL is good at interacting with databases, but you wouldn't develop a game using SQL.
(1 vote)
• did somebody knew how can dowmload the java?
(1 vote)