Beginning Programming All-in-One For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon
If you're just getting started writing computer programs, you need to learn programming branching and looping statements to perform the specific jobs you need for an effective program. Keeping a list of computer programming resources for useful information handy can help, too.

Storing stuff in data structures

Every programming language needs to store data. The simplest way to store data is to use a variable, but a variable can only hold one item at a time. Another limitation is that you must create enough variables to store all the data your program may need to store. Because you likely won’t know how much data your program needs to store, you need to store related data inside a data structure.

Think of a data structure as a super variable that can group related data together and grow or shrink in size depending on how much data your program needs to store. Structures group multiple variables together. Arrays or lists can grow or shrink to store different amounts of data. Dictionaries, maps, or hashes identify data using a “key” value to make it easy to retrieve data quickly. Stacks and queues provide two different ways to store and retrieve data.

Structures

A single variable can only hold exactly one chunk of data at a time. However, a structure can store multiple variables inside a single variable, like this:

struct person

{

string name;

int, age;

};

person.name = “Billy the Kid”;

person.age = 26;

This code defines a structure and gives it an arbitrary name of person. Inside the structure, it defines a name variable that can hold a string and an age variable that can hold an integer.

To store a string in the name variable, you need to define the structure name (person) and the string variable name to hold it (name). Thus, person.name specifies the structure (person) and the string variable (name) to hold “Billy the Kid”.

To store an integer in the age variable, you need to define the structure name (person) and the integer variable name to hold it (age). Thus, person.age specifies the structure (person) and the integer variable (age) to hold 26.

Arrays or lists

Arrays store one type of data in a list, which is why some languages refer to arrays as lists. Arrays/lists store multiple chunks of data within a single variable name. To identify a specific chunk of data, you must reference the array/list name followed by the position of the data inside that array/list.

The position of items, stored in an array/list, is identified by an index number. In zero-based arrays, the first stored item in the array is assigned an index number of 0. In one-based arrays, the first stored item in the array is assigned an index number of 1.

var arrayname = [-4, 90, 128, -57, 32, 306, -48]

arrayname[4] = 32

This code defines an array/list that holds multiple integers such as –4 and 90. To retrieve the number 32, you must reference the array/list name (arrayname) followed by the position or index number of the data you want to retrieve. If the index number of the first item is 0, then arrayname[0] would retrieve –4, arrayname[1] would retrieve 90, arrayname[2] would retrieve 128, arrayname[3] would retrieve –57, and arrayname[4] would retrieve 32.

Dictionaries, maps, or hashes

Also called hashes, dictionaries or maps store a key-value pair where a unique key is associated with specific data. To store data, you must store a key followed by the data you want linked to that key, like this:

mydictionary = {‘pi’: 3.14, ‘taxrate’: 0.75}

This code stores the key ‘pi’ that’s linked to the value 3.14. Then it stores the key ‘taxrate’ that’s linked to the value 0.75. Notice that in a dictionary/map/hash, the keys must all be the same data type and the stored values must also be the same data type. In this example, the keys are all strings (‘pi’ and ‘taxrate’), while the values are all decimal numbers (3.14 and 0.75).

To retrieve data, specify the dictionary/map/hash name followed by the key associated with the data to retrieve, like this:

mydictionary[‘pi’] = 3.14

This code says to retrieve the value linked to the ‘pi’ key stored in the dictionary/map/hash called mydictionary. In this example, the value linked to the ‘pi’ key is 3.14.

Stacks

Stacks are known as “last in, first out” (LIFO) data structures because the last item stored is the first one that can be removed. Stacks can shrink and grow and store any data types. In the real world, stack data types behave like a stack of dishes. The first item you store in a stack gets placed at the bottom. Each additional item stored in a stack buries the first item further and further. To retrieve the bottom item (the first item stored) in a stack, you must retrieve all items stored above it.

You can perform two types of operations on stacks:

  • Push: The Push command adds new data to a stack. When you push data onto a stack, you must also specify the data to push to the top of a stack.
  • Pop: The Pop command removes data from the top of the stack. Because the Pop command always removes the top item, you only need to specify that you want to pop the top item off the stack.

By using a combination of Push and Pop commands, you can store and retrieve data from a stack.

Queues

Queues are known as “first in, first out” (FIFO) data structures because the first item stored is the first one that can be removed. Queues can shrink and grow and store any data types.

You can perform two types of operations on queues:

  • Enqueue: The Enqueue command adds new data to the end of a queue. When you add data to a queue, you must also specify the data to add to the end of the queue.
  • Dequeue: The Dequeue command removes data from the beginning of a queue. Because the Dequeue always removes the first item, you only need to specify that you want to remove (dequeue) the first item from the queue.

By using a combination of Enqueue and Dequeue commands, you can add and remove data from a queue.

Making decisions with branching statements

Branching statements, often called if statements, let a program choose between different sets of instructions based on a true or false condition. By using branching statements, programs can make decisions based on ever-changing data.

The simplest branching statement is called an if statement (or an if-then statement). If a Boolean condition is true, then the if (or if-then) statement runs a set of commands; if the Boolean condition is false, then the if (or if-then) statement doesn’t run anything.

If (condition) Then command

If (condition) Then

Commands

End If

if (condition) {

Commands

}

Another variation of the branching statement, called if-else, offers exactly two choices. If a Boolean condition is true, the if-else statement follows one set of commands. If a Boolean condition is false, the if-else statement follows a second set of commands.

If (condition) Then

Commands

Else

Commands

End If

if (condition) {

Commands

} else {

More commands

}

Rather than check a single Boolean condition, the if-elseif statement can check multiple Boolean conditions.

If (condition) Then

Commands

Else If (condition2) then

Commands

End if

if (condition1) {

Commands

} else if (condition2) {

More commands

} else if (condition3) {

Even more commands

}

The if-elseif statement can check multiple Boolean conditions, but the more Boolean conditions there are to check, the harder the entire if-elseif statement can be to read. That’s why programming languages offer an alternative to the if-elseif statement, called a Select or switch statement.

Like the if-elseif statement, the Select or switch statement can also check multiple Boolean conditions, but in a shorter, simpler way that’s easier to read and understand.

Select Case variable

Case value1

Commands

Case value2

Commands

Else

Commands

End Select

switch (variable) {

case value1:

Commands;

break;

case value2:

Commands;

break;

default:

commands;

break;

}

Repeating commands in looping statements

Looping statements let a program repeat one or more instructions. A for loop repeats a fixed number of times where you must define exactly how many times the loop should run.

For variable = startvalue to endvalue

Commands

Next

for (initial variable value, final value, increment) {

commands;

}

Often, you don’t know exactly how many times a loop should run. In those cases, you must use a while loop, which checks a Boolean condition. If this Boolean condition is true, then the while loop runs. As soon as this Boolean condition becomes false, the while loop stops running.

while (condition) {

commands;

}

If a Boolean condition is false from the beginning, a while loop will never run at all. On the other hand, a do-while loop always runs at least once and then checks a Boolean condition. In some programming languages, the do-while loop may be called a repeat-until loop.

do {

commands;

} while (condition);

repeat {

commands;

} until (condition);

Because both a while and do-while loop won’t stop until a condition becomes false, you must change this Boolean condition within the loop so the loop will eventually stop.

Truth tables

Truth tables show specific Boolean values for different Boolean operators: AND, OR, XOR, and NOT. The AND operator (shortened to && in many programming languages) is true only if both Boolean values are true.

The OR operator (shortened to || in many programming languages) is false only if both Boolean values are false. The XOR operator is false only if both Boolean values are either both true or both false. The NOT operator (shortened to ! in many programming languages) simply reverses a true value to false or a false value to true.

Value1 Value2 AND (&&)
True True True
True False False
False True False
False False False

 

Value1 Value2 OR (||)
True True True
True False True
False True True
False False False

 

Value1 Value2 XOR
True True False
True False True
False True True
False False False

 

Value1 Not (!)
True False
False True

Programming compilers and interpreters

Programming compilers and interpreters let you write code in different programming languages and then run your program on a computer. Most compilers and interpreters work on multiple operating systems, but some may work only on one or two operating systems.

To learn programming, you need to practice writing code in a specific programming language to see what you’re doing right and wrong. As long as you use a popular operating system such as Linux, macOS, or Windows, you should have little trouble finding a compiler or interpreter for your favorite programming language.

Here are some compilers and interpreters to consider:

Compiler/Interpreter Chrome OS iPadOS Linux macOS Windows
Android Studio X X X X
Dart X X X
Flutter X X X
GNU Compiler Collection (GCC) X X X
Go X X X
Java X X X
Kotlin X X X
MATLAB X X X
Perl X X X
PHP X X X
R X X X
React Native X X X
Ruby X X X
Rust X X X
Scala X X X
Swift Playgrounds X X
Visual Studio X X X
Xcode X

Free programming editors

Although most programming compilers and interpreters come with an editor, many programmers prefer using a different editor that provides unique features or shortcuts that make writing code faster and easier.

Here are some free programming editors worth checking out (they all work on Linux, macOS, and Windows):

Apache NetBeans

Atom

Eclipse IDE

GNU Emacs

IntelliJ IDEA

Vim

About This Article

This article is from the book:

About the book author:

Wallace Wang is a bestselling author with more than 2.3 million For Dummies books in print. He wrote Beginning Programming For Dummies and moonlights as a stand-up comic.

This article can be found in the category: