The primary reason to program is to be able to manipulate data. The data is what makes your program useful. Programs will require some form of data from the user and then output some useful interpretation of the input. As such many beginning programming students should become familiar with algorithms and input, process, output (IPO) charts. Just know that programming is a two-phased process, what is the solution, and how to put the solution in a programming language. The data is broken into two parts:
Literals are values that are hard-coded into a program. Examples of literals could look like these
5
which is interpreted as an integer
2.7
which is interpreted as a double
, float
, or real
"Hello"
which is interpreted as a string
literal. Some languages allow single quotes for string literals.These are the primary literal data interpretations in programming.
To manipulate data, we will look at outputting data, creating variables, and inputting data.
One of the first things beginner programmers learn to do is to put some type of message on the screen. Since most beginner coders will learn console output, this is what we will stick to for this informational session. As a word of warning, different programming languages require different actions to be setup before the code will work. Four common programming languages that I will focus on for this lesson are C++, C#, Java, and Python. Each of these languages require an explicit statement that tells the operating system to put the data on the screen.
Here are complete "Hello World!" programs in each lanuage
Language | Code | The programmer has control over the names of the class and file |
---|---|
C++ |
#include <iostream>
|
C# |
using System;
|
Java |
public class MyProgram {
|
Python |
print("Hello World!")
|
An interactive program is one where the user of a program inputs data and the program responds with corresponding output. Consider loan calculators where the user inputs how much money they would like to finance, how long they would like to finance the amount of money for, and at what interest rate they believe they woould be financed. The user of the program desires to know what his or her monthly financial committment will be.
Without getting into details, when we ask for input, the program we write needs to find a place to put the data received. This data is also primarily expected from the keyboard. When the user inputs data, the data has to be stored somewhere. The data has to be stored in variables (yes! those concepts you saw in algebra class). In the variables section, we will talk about the concept in detail. However, for now, you need to know in three of the major programming languages we are observing require that the variables be in existence before the input operation. Those languages also require that the type of data be explicitly known at the time of creation.
Note that data from other sources such as mice are called event-driven programs and not covered in this page.
In the table we will look at writing an echo program. This program will ask the user to enter some text. The program will then repeat the text entered.
Language | Code |
---|---|
C++ |
#include<iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
text = input("Enter text: ")
|
In C#, Console.ReadLine()
will read all input from the keyboard as a string (or text). If we want to store other types of data, we must use the Parse function to store the text as a numeric value. We will see examples as we go along.
In Java, Scanner
is used to read various data from the keyboard (and keyboard buffer). We are encouraged to close the buffer before the end of the main method (keyIn.close()
).
So Python is winning the coding game so far!
Variables are these wonderful placeholders that hold a single value at any moment in the code. Technically, variables are storage locations in the computer's RAM. These locations are allocated sizes from 1 byte to 16 bytes for predefined types and other type sizes are determined as the program runs and needs more memory. Programming has come a long way. In the earlier history of programming, memory was scarce. Programmers had to do a lot with a little. Older programming languages devised the concept of the data type, a predefined identifier that informs the compiler/interpreter of how much memory needs to be allocated for a space and what type of data will be stored in the space.
Programming data will come in four (4) primary forms: integers (whole numbers), real (floating point numbers), string (characters), and custom types known as objects. Object-oriented programming is a concept that is outside the scope of this exploration. Each language has a set of data types that meet the data requirements. For example, in the primary four languages we have been working with, int
means integer represents a 4 byte (except for Python) whole number. There are also 1-byte (char
and byte
), 2-bytes (short
), and 8-byte (long
) whole numbers as well. Some programming languages (e.g. C# and Swift) will allow the use of a data type called var
. This data type allows programmers to create variables without specifying the particular type of data that the variable will hold.
Unlike C++, Java, C#, and other similar programming languages, Python does not require the explicit use of data types unless you specifically require a variable to strictly behave a certain way. This freedom in coding brings up the idea of type discipline. TYpe discipline is the notion that if you use a variable for numeric purposes, that particular variable should not be arbitrarily used for anything but numeric purposes. Python blurs the lines of integers and floating-point numbers. If an expression yields a decimal, Python automatically adjusts where the other lanugages would either truncate (cut off the decimal values) - if the type were int
, or give a programming exception.
A google search can show all data types for each of the programming languages.
The rules of variable naming are consistent across all programming languages. The language does not restrict what a programmer choose to name a variable as long as the rules are followed. There are generally four (4) basic rules of variable naming.
1var
would be an illegal variable name.var^
would be an illegal variable name. use_the_underscore
or camelCase
.The purpose of variables is to hold data. The data can be outputted using the relevant print statement. If a variable x
is created and has a value set, putting x
in the output statement will show the value of x
. Here is a Python example:
x = 5
print(x)
The output is 5
.
This should help understanding the echo program ini the Echo program section.