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...
A laptop with a collar and a leash. A speech bubble says "ADD!" on the side. The laptop displays "2 + 2" and "4".
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:
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:
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.
println()command is also called a function, method, or procedure. That line tells the computer to call the procedure named
printlnthat knows how to display output in a console.
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
alert(), and you'll find even more ways in other languages like Python, Java, and Snap.
Want to join the conversation?
- Why do we use println and not text? Is it because there are multiple parameters to text?(6 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.(2 votes)
- I learned PHP but it's never taught. Is PHP just too old and irrelevant nowadays?(4 votes)
- Programming give instructions to computer by using programming languages and this instructions are changed in to 0s and 1s so how this 0s and 1s tells for computer to do something? How does it works?(0 votes)
- Diving into how the 0s and 1s define operations for the computer is an entire course in and of itself. I will try to simplify it to provide a high-level overview:
Consider this... Once the high-level programming language instructions have been converted into binary machine code (by running through an assembler and compiler, sometimes these two are coupled) then that machine code will be loaded into the computer's main memory (RAM - Random Access Memory - also referred to as Dynamic Random Access Memory). The computer's "brain," the Central Processing Unit (CPU) will sequentially pull instructions from Main Memory (i.e. it will pull instruction #1, then instruction #2, etc. - it is a bit more complicated than this, but this is a simplifying assumption that makes this explanation easier) and will interpret (decode) and execute each instruction. Instructions are a sequence of bits, and this sequence can be broken apart to represent a specific operation.
Consider a sequence of 16 bits that were pulled from the main memory. These 16 bits represent the first program instruction. The bit sequence might be 1110011111010000. The most-significant bits, '111' have no meaning. The seven bits following '111', i.e. '0011111' may represent that you want to add 1 to a value stored in one of your registers (a register is the smallest unit of computer memory, generally holding one word worth of data - 32 bits in a 32-bit machine or 64-bits in a 64-bit machine - and resides within the CPU itself) and then store the result somewhere. The next three bits, i.e. '010' might represent where you want to store the result, in this instance let us say you want to store it back in the same register that you pulled from before incrementing by 1. The last 3 bits, '000' may define a JUMP operation. In this instance, '000' will mean that you do not want to perform a JUMP operation. Note that a JUMP operation refers to jumping to another program instruction based on the results of your earlier operation (similar to an If-statement in a high-level language).
Note that I have used the expression "may" many times in this explanation. I have used this expression repeatedly because the sequence of bits that represent a program instruction will be interpreted differently in different machines based on their instruction set architecture. Also, the instructions may be represented by more or less than 16-bits, I simply used 16-bits above for a simple instruction and referenced the nand2tetris material available online to define how those 16-bits might be converted to machine operations.
The process is complex and too much for one comment, but hopefully this motivates you to explore further. I hope this was helpful!(8 votes)
However, you can use browser provided functions like confirm in our Webpage-making environment here:
Just put it inside a script tag and you'll see the confirmation box pop-up.(2 votes)
- will the outcome change if you put 'print' or 'printIn'(2 votes)
printlnwill print a new line to Khan Academy's console.(2 votes)
- What's the difference between println and print()?(2 votes)
- It depends on the language and the environment.
println()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 named
print()that print to a console. For example, Python 3 has a
print()function that works similarly to the
- I’m using an iPad and Can’t see the editor properly. I tried to open a new tab, but there’s no output for the command.(2 votes)
- yo what is the difference between Canvas and Console?(0 votes)
- The console is more bare-bones, it's the simplest way for us to interact with a computer (no graphics, but still something that can be handled by a human without too many additional skills). If you have windows you can right-click on the start symbol and click Windows PowerShell, on Linux systems you can search for the terminal app. Both are consoles.
The canvas is something created by the program as a place for your graphics. It's a lot more complex and resource-intensive than the console. Normally creating a canvas takes a little bit more work than just working with the console, but here the programming language does most of the work for you.(4 votes)
- Are these commands case sensitive?(0 votes)
- Where is python i need python(1 vote)