computer programming mr. josé a. ortiz morris. computer language languages that the computer...

Post on 02-Apr-2015

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Computer ProgrammingMr. José A. Ortiz Morris

Computer Language Languages that the computer

understands.

They are low level languages. (BINARY 1 / 0)

People used to program computers using them.

Low level languages Language that only the computer’s CPU

and other processing components can understand.

They are very difficult to understand by human beings. Ex. 01101110

Programmers used to program in this type of language.

High level language Language that is similar to regular human

languages.

A CPU can’t understand it and it is used by programmers to program software. Ex. System.out.println(“Hello World!”);

This type of language is sent to a compiler so that it can be turned to a low level language so that the computer can understand it.

Starting to Program When we start to program the first thing

that we have to understand is that the computer needs specific instructions to be able to perform the specific tasks it has to do.

Computers are not intelligent. They just do whatever the programmer tells it to do.

Giving instructions to the computer… When a programmer gives instructions

to the computer he/she used a high-level programming language to give them and its written down in a source code (Group of instructions that form a computer program).

The different instructions activate different components of the computer.

Using different tools to help with programming… Since understanding a high-level

computer language is difficult at first, pseudocodes and flowcharts to help understand the process the program has to do before writing down the source code.

FlowchartsStart

End

Display = “Hello!”

Display = “Goodbye!”

Pseudocode

StartDisplay = “Hello!”;Display = “Goodbye!”;End

Understanding each instruction… In the previous examples of flowcharts

and pseudocode we can see only 1 type of instruction. The Display instruction is used to send information to a monitor or any other type of output device to present it.

Start and End are just there to know that the program’s source code starts and ends at those specific points.

Other types of instructions… Get = this instruction is used to acquire

information from an input device. In this class we will always assume is from a keyboard. Ex. Get = name;

Explaining the basic form of instructions… In the last example we had an instruction:

Get = name;

This instruction has 4 components: The function ( Get ) An assigning operator ( = ) A variable ( name ) And an end mark ( ; )

Example…StartDisplay = “Please enter your name”;Get = name;Display = name;End

Variable Data Types For the remainder of this topic we are going to use

different types of variables and each type of variable has different data types: String = stores text.

Ex. “Anthony” Integer = stores whole numbers.

Ex. 25 Real = stores real numbers.

Ex. 25.5 Boolean = stores true or false (used for logical

comparisons) Ex. true

To use variables… When you are going to use a variable in

a computer program code you have to define them so that the computer will know what type of value they will store and to reserve the space in memory.

The format in this class to define a variable is:

DEF name : string;

Reviewing previous code… The last code that we wrote had an error. We

didn’t define the variable to be able to use it. The correct way to write it would be the following:

StartDEF name : string;

Display = “Please enter your name”;Get = name;Display = name;End

Exercise… Write a program that:1. Asks the user his name2. Asks the user his age.3. Asks the user 2 grades.4. Calculates the average of the 2 grades.5. Displays the result as the following:

Hello, John DoeYou are 12 years oldYour grade average is 75.3

StartDEF name : string;DEF age : integer;DEF grade1, grade2, average : real;Display = “Please enter your name: ”;Get = name;Display = “Please enter your age: ”;Get = age;Display = “Please enter your first grade: ”;Get = grade1;Display = “Please enter your second grade: ”;Get = grade2;average = (grade1 + grade2)/2;Display = “Hello, ” + name;Display = “Your age is ” + age;Display = “Your grade average is ” + average;End

Exercise 2… Write a program that:1. Asks the user his name2. Asks the user his last name3. Asks the user his age.4. Asks the user 3 grades.5. Asks the user his phone number.6. Calculates the average of the 3 grades.7. Displays the result as the following:

Hello, John DoeYou are 12 years oldPhone#: 7872536894Your grade average is 75.3

StartDEF name, lname : string;DEF age, phone : integer;DEF grade1, grade2, grade3, average : real;

Display = “Please enter your name: ”;Get = name;Display = “Please enter your last name: ”;Get = lname;Display = “Please enter your age: ”;Get = age;Display = “Please enter your phone #: ”;Get = phone;Display = “Please enter the 1st grade: ”;Get = grade1;Display = “Please enter the 2nd grade: ”;Get = grade2;Display = “Please enter the 3rd grade: ”;Get = grade3;average = (grade1 + grade2 + grade3)/3;Display = “Hello, ” + name + “ ” + lname;Display = “You are ” + age “ years old.”Display = “Phone#: ” + phoneDisplay = “Your grade average is ” + average;End

The If Statement function… Programs have a need to be dynamic to

be able to perform different functions for the user.

The if statement helps the programmer create something that can do 1 of 2 or more things.

If statement example… If you have a program that has already

asked the user his name, his last name and his marital status (single or married) and gender(Male or Female). You can show the different marriage status title before the name (Mr., Ms., or Mrs.)

Here is how it would be coded:If gender = “Male”Display= “Hello, Mr. ” + name + “ ” +

lnameElse

Display= “Hello, Mrs. ” + name + “ ” + lname;

Example with Marital Status…

Start...If gender = “Male”

Display= “Hello, Mr. ” + name + “ ” + lnameElse if mstatus = “single”

Display= “Hello, Ms. ” + name + “ ” + lnameElse

Display = “Hello, Mrs. “ + name + “ ” + lname;

.

.End

Grade Homework Create a program that: Modifies the previous program about the

grade average and adds the grade letter to the average display depending on the average.

Hello, John DoeYou are 12 years oldPhone#: 7872536894Your grade average is 75.3 C

top related