The sequence structure is a logical design that controls the order in which a set of statements execute. A sequence can be thought of as a list of actions that must happen in order.
Loops are a common programming structure that cause a set of statements to execute multiple times. There are several kinds of loops, but they all have the same purpose: to execute something more than once.
The most common loop is the for loop. This loop has four main clauses that must be defined: the initialization clause, the comparison clause, the update clause, and the termination clause.
The initialization clause is where you define what you are initializing and what value it will have. The comparison clause is where you define what you are comparing and what value it will have. The update clause is where you define what you are updating and what value it will have. The termination clause is where you define when the loop ends.
These clauses all have to match a specific kind of loop depending on how many times the statements in the loop initialize, compare, update, and terminate.
The if else statement
The if else statement is one of the most fundamental statements in programming. If else statements are used to execute one or more statements depending on the value of a condition.
If the condition is true, then the statements under the if clause are executed. If the condition is false, then the else clause statements are executed. If no else clause is present, then no statements are executed.
If clauses can be added or omitted as needed. If clauses can also be replaced with other conditions such as equal to, greater than, less than, equals to a certain value, etc. else can also be changed to yet another statement.
The order in which the if and else clauses appear does not matter unless there is more than one of each. In that case, the ones that occur first must be evaluated before later ones.
Switch case statement
A switch case statement is a control structure that directs the computer to execute a set of statements depending on which case it matches. The computer checks which case it matches in a certain order, starting with the first one.
If the statement matches a case, then the statements under that case are executed. If it does not match any cases, then the default statement is executed. There can be more than one of these.
This is helpful for executing a set of statements based on different conditions. For example, your program could change what it does depending on what happens.
Programs often have built-in commands that can replace your own command with its own functionality. A switch case statement can be used to tell the computer whether to use yours or its own.
Understanding control structures is important
Control structures are a key part of programming. They are what make programming languages more than just a way to manipulate data.
Control structures can be categorized into two main groups: sequential and selection. These categories encompass all of the different control structures that can be used.
Sequential structure is simply the order in which statements execute. For example, if you had two statements, A and B, statement A would execute first then statement B. This is simply how programming languages work at their core.
Selective structure is when the computer decides whether to execute a statement or not based on a condition. If the condition is true, then the statement executes.
Control structures control the order in which a set of statements execute
All programming languages have some form of control structure. Some languages have more sophisticated control structures than others. For example, some languages do not have loops, while other languages have different types of loops.
The if statement is a control structure most beginners learn early. If statements are used to execute a statement or set of statements only when a certain condition is met. If the condition is not met, then no statements are executed.
If statements can be written in two ways: with an “if” then “then”, or with just an “if”. The latter is called a short-circuit if statement and only works in some programming languages. Check with your instructor which type you can use!
Loops are also common beginner concepts. There are many different types of loops, but they all loop (again) through a set of statements until a certain condition is met.
A looping method is a logical design that controls the order in which a set of statements execute by repeating a certain number of times. Looping methods include for loops, while loops, and do while loops.
For loops are the most common looping method. For loops execute a statement or set of statements multiple times and then repeat this process until a certain condition is met. For example, for every number from one to ten, say the number aloud.
While loops work in the same way as for loops, except that the condition that must be met to end the loop is different. While loops typically loop until a certain situation changes or is met.
Do while loops are similar to while loops except that they check if the conditional statement is true or not before repeating the statements in the loop.
If else statements
If else statements are one of the most commonly used logical design structures. If else statements create a condition and execute a set of statements in response to that condition being true or false.
If else statements typically have a then statement following them. Then is the response to the condition being true. Else is the response to the condition being false.
If else statements can have either one then statement or two: then and else. Then is what executes if the condition is true, and else is what executes if the condition is false.
If else statements can also be inverted: then or else first, and then the other follows. This has the same effect as having only one of each- only one will execute.
Switch case statements
Switch case statements are another common logical design. A switch case statement essentially has a branching structure that has several possible paths.
The most prominent feature of the switch case statement is the use of possible values (also called values sets). These values can be anything: numbers, words, sentences, etc.
A switch case statement first declares a variable then begins a case block for each possible value. Within each case block, a statement or set of statements is executed. These statements can be simple or complex, but there must be at least one.
When a new value is encountered, the switch case statement shifts to the corresponding case block and executes the statements contained within it. Once all of the cases have been executed, the program continues execution where it left off.
This logical design is very useful for changing different aspects of a program using different conditions.