+ All Categories
Home > Documents > Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson...

Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson...

Date post: 11-Jun-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
17
Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado. (No contaban con su astucia)
Transcript
Page 1: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

Lesson 1: Hello, world!Line by line explanation

● Pic 10A

Ricardo Salazar

El chapulín colorado.(No contaban con su astucia)

Page 2: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

What is a computer?

● Computers can handle repetitive chores without getting bored.

● A computer is programmable to handle different tasks (e.g. balancing checkbooks, processing words, playing games).

● Computer actions are composed of huge numbers of primitive operations.

Page 3: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

What is a program?

“A computer program is a list of instructions that tell a computer what to do. Everything a computer does is done by using a computer program.”

● A program tells the computer the sequence of steps needed to fulfill a task. Computers are not smart; the instructions must be very detailed and precise.

● A programmer designs and implements these programs.

In this course you will be the programmer and will write simple computer programs.

● In order for the computer to understand your instructions, you have to learn a certain set of grammar rules (syntax).

Page 4: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

C++ is a language

● Learning C++ is like learning a foreign language.

● We'll teach you basic words and some simple grammar rules.

● We won't teach you every word/rule you will ever need.

● With the basics in place you must learn to form new phrases of your own.

● Simply repeating the examples given in class is not enough and will not get you very far.

● Practice, practice, practice!

Page 5: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

Computers speak binary000101100001010000000000000000000100010100010000000000010010011100010001001010000000

Machine instructions

● Extremely primitive.

● Encoded as numbers:

161 40000 45 100 127 11280

● Tedious and error prone to look up numeric codes and enter them manually.

● Each processor has its own set of machine instructions.

Assembler

● Assigns short names to commands:

mov 40000, %eaxsub 100, %eaxjg 11280

● Makes reading easier.

● Translated into machine instructions by another program.

● Still processor dependent.

Different programming languages use different syntax

● C++:

if (int_rate > 100) message_box("Interest rate error");

● Pascal:

if int_rate > 100 then message_box('Interest rate error');

Higher-level languages

● Independent of the underlying hardware.

● Easiest to read and write.

if (int_rate > 100) message_box("Interest rate error");

● Translated by compilers into machine instructions.

Page 6: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

The compilation process

● The C++ source code is your program.

● The compiler is a program that translates your C++ code into object code.

● Object code consists of machine instructions and information on how to load the program into memory.

● A linker program takes the object code from your program and the code from the various libraries and builds them into an executable file.

● Libraries contain code used by your program.

Page 7: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

The compilation process

● Compiler errors:- Something violates the language rules (e.g. missing ;, mis-spelling).- Missing libraries and/or source files (linking errors).

● Logic or run-time error:- Program doesn't do what it's supposed to do.- Program author must test and find the error.

Page 8: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

A simple program:¡Hola, mundo!

The “Hello, World!” program is the universal introductory program. I simply changed the language for fun.

When 'coding' this program you can use the language you like the most.

Warning: The numbers at the beginning of every line are there just for reference. You do not have to type them.

Let us examine the code line by line...

Tomorrow you will run the following program:

● C++ is case sensitive:cout, Cout and COUT are not the same instruction.

● C++ has freeform layout:You should make an effort to make your programs readable.

Page 9: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

¡Hola, mundo!(header files)

● reads the file 'iostream.h' that contains the definition for the stream input/output package (more on this later).

● cout (on line 7) is an instruction defined in the library file 'iostream.h'.

● without the first line we would get a compiler error.

Other libraries we might call are:cmath Math functions like sine and cosine.

string Manipulates text strings like “Hola”MyLibrary.h We can define our own 'header files'.

Page 10: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

¡Hola, mundo!(standard namespace)

● tells the computer to use the standard namespace (more on this later).

● when the computer reads the statement cout (on line 7) it knows to look it up in the included library.

● it is a fairly new addition to c++ (added for large projects with many programmers).

Note that this line ends in a semi-colon (;), every statement in c++ ends in a semi-colon. Forgetting the semi-colon is the most common compiler error.

Page 11: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

¡Hola, mundo!(the driver)

● The function main( ) is called the driver. It is the first place where the computer looks to run code.

● The empty parenthesis ( ) indicate that no arguments are passed to the function. An alternative is to write: int main (void) { … }

● The int indicates that the program will return an 'integer' (i.e., a whole number) when it is done running. The c++ convention is to send a 0 (zero) to the computer if everything ran smoothly.

● The { } enclose a set of instructions often called a block of code.

Page 12: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

¡Hola, mundo!(return statement)

● sends a value to the computer, telling us that the driver (the function main) has completed its work.

● because of the int in the driver, the last statement in the main routine should send a whole number (integer).

● returning zero indicates that everything worked fine. Sending a different number could indicate an error without crashing the program.

Page 13: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

¡Hola, mundo!(the cout statement)

● cout = “console out”, it writes whatever it follows to the console (usually a pop-up screen that appears when a program is run).

● #include <iostream> is needed in order to use the cout statement.

● the angle brackets << push whatever it follows to the console (screen).

● the output to the screen is enclosed in quotes " ". They must be balanced!

● the \n does not appear on the screen, it skips to the next line

The backslash \ is a special escape character. A character that follows will not appear on the screen. Instead it gives a special command to the output.

Page 14: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

● For the first few weeks your assignments will look like this.

● The STATEMENTS part could be very long and even call other functions.

● Indentation is a good programming practice; it produces code that is easier to read.

Another widely used form of indentation is

int main(){ ---------------- -- STATEMENTS -- ---------------- return 0;}

Basic template

Page 15: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

More about cout

● Some escape characters:

\n new line

\' single quote

\" double quote

\\ backslash

\t horizontal tab

\v vertical tab

● Escape characters need to appear inside quotes " ".

● Examples:- One push (text)cout << "Hey.\n\"How are you?\"";shows

Hey."How are you?"_

- One push (values)cout << 22 ;cout << 2 + 2 ;shows

224_

- Consecutive pushescout << "I\'m\t\twell." << "\nYou?";cout << "\n2+3=" << 2*3 << "?\n";shows

I'm well.You?2+3=6?_

Page 16: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

Exercises

What is the output of the following statements?

● cout << "\"Eso, eso, eso.\'";

"Eso, eso, eso.'_

● cout << "Se me\nchispot\teo";

Se me chispot eo_

● cout << "n\nn";

n n_

● cout << "n\\nn";

n\nn_

● cout << "n\\\nn";

n\ n_

● cout << "El chavo del" << 8;

El chavo del8_

● cout << "Del "<< 4+4 << "?\n";

Del 8? _

● cout << "cout << \"Hola.\";";

cout << "Hola.";_

● cout << "E" << "S" << 0 <<", " << "e" << "so,"; cout << "\n\teso.\n";

ES0, eso, eso. _

Page 17: Lesson 1: Hello, world! - UCLA Program in Computingrsalazar/pic10a/lessons/lesson01.pdf · Lesson 1: Hello, world! Line by line explanation Pic 10A Ricardo Salazar El chapulín colorado.

Exercises (cont)

● Write a piece of code that produces the following output:“El chavo del 8, escribio:”<< Que bonita vecindad >>_

cout << "\"El chavo del "<< 4*2 <<", escribio:" << "\"\n";cout << "<< Que bonita vecindad >>" << "\n";

● What is wrong with the following statements?

cout << "\"El chavo del 8."";

cout << "El chavo del " << 2 + 2*3 <<"."

cout << "El chavo del 8"\n;

● What about these statements?

cout << "El chavo del" << 8 << "\n";

cout << "\"El chavo del << 4 + 4 " << "\n";


Recommended