Control Flow

Control flow – how a program executes. With loops, if/then, while, switch/case, and so on, the control flow statements determine the control flow. Most complex programs don’t just run from beginning to end. They jump around a lot, based on various things. Only a simple program would run start to finish exactly. Without control flow statements, you can only have a very basic program that doesn’t accomplish much.

Loops – when you want to do the same thing over and over again, you don’t just copy the code multiple times. Instead, you use an important feature of control flow: loops. There are two main kinds of loops: iteration and recursion.

Iteration would be something like this in Java:

for (int i = 1; i <= 100; i++){

System.out.println(i);

}

The above example prints the numbers 1 to 100.

By contrast, recursion uses smaller copies of itself. A function that calls itself is recursive.

Aside from recursive and iterative, there are other ways to categorize loops. There’s for and while, for example.

Here’s a while loop:

int x = 0;

while (x <= 5){

System.out.println(“hello”);

x++;

}

And a do while:

int x = 0;

do {

System.out.println(“How’s it going?”);

x++;

} while (x <= 5);

If you need an infinite loop (which I don’t recommend), then use something like this (or whatever the implementation is in your preferred programming language):

while (true) {

System.out.println(“This goes on forever!”);

}

For loop – my favorite kind of loop. It’s the kind I use most of the time.

A for loop starts with an iterator with a default value, such as 0 or maybe 1 (but it can be anything), then a condition when the loop will end, and then how much the iterator should be incremented. It’s common for an iterator to be incremented by 1, but you can increment by any amount if you really feel like it.

for (int i=0; i < 100; i++){

if (i % 2 == 0){

System.out.print(String.valueOf(i) + ” is an even number”);

}

}

While loop – a loop that will run as long as a condition is true. However, it won’t run the code even once if the condition isn’t true initially.

int i = 0;

while (i < 10){

System.out.println(i);

i = i + 1;

}

Do while loop – a variation of a while loop. It will always run once, and then it checks if a condition is true or false. If it’s true, it will run the loop once more. This is different from the while loop because it will always run once even when the condition is not met.

do {

System.out.println(“what’s up?”);

} while (1 == 2); //evaluates to false

Conditional operator ?: – The conditional operator is a way of choosing between two things based on the result of a Boolean evaluation.

name == “Bob” ? System.out.println(“Hi. Bob”) : System.out.println(“Who are you?”);

In the above example, If the variable name is equal to “Bob”, then they will be greeted with “Hi, Bob” and if not, someone will ask what their name is. Following something that evaluates to a Boolean and a question mark, the first thing you put is what will happen if the thing is true. Then it’s separated with a colon, and then you write what you want to happen if something happens to be false.

Switch/case – one alternative to if/else if/else is to use a switch/case. Not all languages support it. You can use switch/case in Java, but not Python.

Here is an example of a switch/case from one of my coding projects on GitHub:

switch (tempDirection) {

case “up”:

player.characterView = new ImageView(new Image(“/img/character_up3.png”));

break;

case “down”:

player.characterView = new ImageView(new Image(“/img/character_down3.png”));

break;

case “left”:

player.characterView = new ImageView(new Image(“/img/character_left3.png”));

break;

case “right”:

player.characterView = new ImageView(new Image(“/img/character_right3.png”));

break;

default:

System.out.println(“odd, tempDirection is: ” + tempDirection);

break;

You will notice that some code examples are Java and others are Python. This is to get you to learn general concepts instead of only thinking about language-specific things. That being said, Python is probably easier to start with.

A switch is used to see the value of something. In this case, we’re examining tempDirection, which is part of my code for a 2D RPG game engine I wrote in Java. It’s very similar to if/else if/else. The case means what to do when tempDirection is a particular value. You can have as many cases as you want. The last part, default, is what happens if none of the above cases apply. break means to get out of that block of code.

If, else if, and else – conditional statements. If a condition is true, do something. Else if something else is true, do that. Else, do something else. Some languages use elif instead of else if. Some languages use fi to wrap up if/else stuff. In Java, you’d use if/else. But in bash, you use if, then, else, and fi.

Something only happens if specific criteria is met, aside from a final else, which acts as a catch-all clause.

If (mood == “happy”) {

System.out.println(“You’re in a good mood today, huh?”);

} else if (mood == “sad”) {

System.out.println(“Cheer up, it’s a wonderful day!”);

} else {

System.out.println(“Hello!”);

}

Code block – an area of program execution. When you leave a particular block, you go to the next level up of blocks. Blocks pertain to how programs run and locality of variables. A local variable might only be accessible within an individual block of code. A loop will loop through the loop block, going from top to bottom until the loop ends. You can have multiple blocks within a block.

You can tell when something is a block of code based on its level of indentation or surrounding curly braces, at least for most situations (though not all languages use curly braces for demarcation). Blocks can be nested within one another.

Indentation – adding tabs or spaces to your code can make it easier to read. In many languages, indentation is optional but highly recommended. In Python, indentation is mandatory. However, you will encounter problems in Python if you mix up tabs and spaces. Some people prefer tabs, while other people prefer spaces (usually 4).

Whitespace – line breaks, tabs, and spaces are considered whitespace. They are often used for spacing out your code to make it more readable. Some languages ignore whitespace, but others do not.

Iterator – a variable used for counting. Often associated with loops. Usually, you will start with a name like i for an iterator, but if you have multiple iterators/loops in the same block (such as with nesting), you might need to use different letters, like j and k, for example.

Recursion – a function that calls itself. The most straightforward example of recursion is a factorial. The factorial of 5 is 5 times the factorial of 4. The factorial of 4 is 4 times the factorial of 3. The factorial of 3 is 3 times the factorial of 2. And so on. In this case, the way to solve a factorial of a bigger number is to compute many smaller factorials, calling a single function multiple times for one result.

Recursion involves something, such as a function, that is made of smaller and smaller versions of itself.

Iteration is the same thing every time until a particular condition is met. Recursion is smaller versions of the same thing.

continue – the break keyword will get out of a block of code, but the continue keyword will go along with the rest of it.

pass – do nothing. In Python, if you are writing a basic structure of your code, like if you know you want to write a function eventually but don’t want to write it right away, you can make a function with pass as the body so that you can come back to it later.

Congratulations on completing section 2!

You’ve mastered the basics of computer science and coding concepts!

← Previous | Next →

Basic CS Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *