Basic Programming Concepts
This material was developed with funding from the
National Science Foundation under Grant # DUE 1601612
Next
Restart
Back
Arrays
Introduction
2/8
Have you ever tried to learn a new language like French or Spanish? Usually in the beginning you learn some basic parts of speech like nouns, verbs, and adjectives. When you learn to program it is much like learning a new language and you need to start with the foundation like parts of speech. The basic elements of programming are data.
Functions
Decision Structures
Data Types
Loops
Variables
Arguments
Conclusion
Statements
ball
Data can be of varying types, called data types. Three common data types are strings, integers, and floats. An integer is any whole number, for example the number 2. A float is a decimal number, for example the number 2.2. A string is generally thought of as text, that is, a string of characters; for example, “ball”.
Integer
Float
2
String
2.2
Data can be stored in variables. Variables are like boxes that hold data and they have a label or name assigned to the variable. Much like labeling a box full of sports balls, balls. Variables can be thought about like nouns. For example, the variable “balls” can contain the word “ball.”
balls = “ball”
1/3
Statements are a unit of a programming that expresses some action to be carried out. A program is formed by a sequence of one or more statements.
Statement BallsMove = balls AND move
red ball
rolls
Function AddBalls (red,blue){return red + blue}
Functions are a named group of code that performs a specific task. Depending on the language, you may also hear functions called subroutines, methods, or procedures. By giving the function a name and defining parameters it should accept, you can pass information to the function and return some result. For example, we can use the function AddBalls to add red balls to blue balls regardless of the value of red or blue.
(3,5)
3
5
AddBalls
An argument is an expression or value that is passed in a method. When we call a function, we can set the values of the items within the function by using arguments. For example, using our AddBalls function that adds the number of red balls and the number of blue balls, we can use an argument to indicate to the function what the value of the number of red balls is and the value of the number of blue balls is.
Function AddBalls (red,blue){return red + blue}
AddBalls(3,5)
, 3,
Arrays are an ordered list of related values that includes both strings and integers. These can be thought of like order lists or set of instructions. When the array is called the variables are run in the order in which they were placed in the array. In our red ball scenario, this might look like “red ball”, “3”, “blue ball”, “5”.
, 5
Array Balls = (“red balls”, 3, “blue balls”, 5)
ELSE-IF
bounces
Decision structures allow you to tell a story with multiple outcomes. Decision structures usually consist of if, else-if, and else statements. In these statements, the outcome of what will happen depends upon one of the conditions being met. For example, if red ball, rolls. Else-if the blue ball, bounces. Else, flies.
ELSE
flies
If red ball:
rolls
Else-if blue ball:
bounces
Else green ball: flies
IF
Control Structures
red ball
blue ball
green ball
rolls
While red ball:
rolls
Loops allow you to execute a particular set of instructions multiple times. For example while the red ball rolls. This set of instructions says that as long as a there is a red ball, it rolls.
Hopefully this animation has helped you understand some of the major terms associated with programming. Knowing the meaning of these terms will be a powerful tool to help you understand programming concepts.
2/3
Variables can store all data types and can be used to construct a section of code, much like nouns and verbs, are used to construct a sentence. The correct way in which to order the nouns and verbs in language is called syntax and in programming it is also called syntax. For example, “ball” must proceed the verb “rolls.”
move = “rolls”
+
3/3
balls = (“red” + “ball”)
We can add the adjective “red” to the variable balls by using concatenation. In programming we can use concatenation to connect two strings end-to-end. For example, we can change the variable balls to be set equal to “red” + “ball” and the resulting value of the variable “balls” will now be set to “red ball.”
=