Main content
AP®︎/College Computer Science Principles
Course: AP®︎/College Computer Science Principles > Unit 3
Lesson 1: What is programming?Our first program
When we write a computer program, we're giving the computer a set of instructions for what we want it to do. Just like we can tell a dog to "sit" or "beg", we can tell a computer to "add" or "print". A computer isn't as furry as a dog, but fortunately, it's a lot more reliable! And just a bit better at math...
There are many languages we can use to write computer programs. Here on Khan Academy, we'll use our live editing environment with the JavaScript language.
Let's start with a simple "Hello World" program, the first program of many new programmers. Our goal is to get the computer to display "Hello World" somewhere on the screen.
That's as simple as:
println("Hello, World");
Try typing that in the editor below:
If you make any typos or syntax mistakes along the way, our error buddy will pop up and let you know. Everyone makes mistakes while programming, so it's totally okay. That's how we learn and improve!
How does it work?
Now let's break down our 1-line program:
println("Hello, World");
That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do.
The
println()
command is also called a function, method, or procedure. That line tells the computer to call the procedure named println
that knows how to display output in a console. The
println()
procedure expects a single parameter which specifies what text the computer should display. In this program, the value passed in is "Hello, World!", so that is what's displayed.Display commands are different in each environment and language. The
println()
command works here, but it may not work in other places. Other ways to display in JavaScript include console.log()
and alert()
, and you'll find even more ways in other languages like Python, Java, and Snap.🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- What's the difference between println and print()?(16 votes)
- It depends on the language and the environment.
In the Khan Academy programming environment, we use the JavaScript language plus ProcessingJS functions.
There's aprintln()
function in ProcessingJS that prints out to the console, so that's what our examples use. That stands for "print line", as it always puts a new line after whatever it prints.
Other environments may have functions namedprint()
that print to a console. For example, Python 3 has aprint()
function that works similarly to theprintln()
function here.
https://repl.it/@PamelaFox1/PrintExample(25 votes)
- Why do we use println and not text? Is it because there are multiple parameters to text?(7 votes)
- From the author:The text() command displays text on the canvas, while println() displays text in a console. We aren't teaching any ProcessingJS drawing functions in this course, since the focus is on programming concepts common across many languages and environments. Thus, I decided to use println() for the examples.(15 votes)
- why can't we do other commands in the javascript box, such as 'confirm("hello world");'(4 votes)
- Our JavaScript environment on Khan Academy is optimized for the ProcessingJS library, to make drawings and animations. The "confirm()" function is provided by browsers for use on webpages, and we don't expose it in the ProcessingJS environment.
However, you can use browser provided functions like confirm in our Webpage-making environment here:
https://www.khanacademy.org/computer-programming/new/webpage
Just put it inside a script tag and you'll see the confirmation box pop-up.(9 votes)
- Why do you have to put all those parenthesis and colons and stuff? Why can't you just put: Say "flamingo" Or something like that?(3 votes)
- Because, like spoken languages such as English, programming languages have their own syntax and punctuation usage. In JavaScript (and most common programming languages), parentheses are needed to indicate that we are calling a function. In this case, we are calling "println". We put the string, "Hello, world!", inside the parentheses to pass it to the function so println can display it on the screen. Finally, we add the semicolon at the end of the line to indicate that the statement is finished.(10 votes)
- I learned PHP but it's never taught. Is PHP just too old and irrelevant nowadays?(5 votes)
- PHP is still taught, and used! It is not irrelevant at all.(4 votes)
- Why is it necessary for quotation marks within the parenthesis?(5 votes)
- Yes. If you put there text not in quotation marks JS will thing that this is not a text, but variable, function or other things. For outputting exactly what you've written there you must out text in q.marks, so JS will know that this text is String object.(3 votes)
- What language I have to use for computer science?(6 votes)
- For the command 'println',
1. I assume 'print' part refers to required or desired output or IS IT SOMETHING ELSE ?
2. What does 'ln' refer to ?
3. Why are round brackets and then double inverted commas used ?
4. In this particular language, is semicolon analogous to a full stop ?(3 votes)- 1. "print" refers to the fact that the text is being printed to the standard output.
2. "ln" is an abbreviation for "line"
3. The parentheses are used when invoking a function (such as "println") to group together any arguments used by said function. Referring to "println" again, the function accepts only one argument, a string that will be printed to the standard output. The quotation marks are used to denote the beginning and end of a string.
4. Yes, I suppose you could say that a semicolon would be like a full stop for Javascript.(6 votes)
- will the outcome change if you put 'print' or 'printIn'(3 votes)
println
will print a new line to Khan Academy's console.print
(while still printing to the console) will put it on the same line as a previous call.(4 votes)
- how do you say "hello, world" in python(1 vote)
- print("hello, world")(8 votes)