Monday, June 13, 2022

Logic Control

The main part of programming is learning how to make your code do something, primarily through a variety of logic controllers. These controllers handle if-then conditions, reiterative processing through loops, and dealing with errors. While there are other ways of working with code, these are the most important ones for new programmers to learn.

When dealing with logic control, a developer needs to be aware of how data is being transferred, particularly when working with user input, network connections, or filesystem access. Python has three data streams for input/output (I/O). sys.stdout is the standard output stream; it handles the output of print() and Python expressions. sys.stdin is the standard input stream; it is used for all interactive input. sys.stderr is the standard error stream; it only takes errors from the program, but also handles the interpreter's own prompts.

One thing to recognize is that, depending on the OS environment, information that you would consider going to stdout is actually sent to stderr, because stderr normally goes to the same location as stdout, by default you'll usually see it on the screen as well. However, you won't know that a response is actually going to stderr without testing. If the particular environment routes stderr to another location, such as a log file, you won't know until you need to troubleshoot. This is important to note because, sometimes, you may not be seeing the information you expect because it's not a normal stdout message. These data streams are considered regular text files and can be accessed and interacted with just like normal files.

One of the most common control structures you'll use, and run into in other programs, is the if...else conditional block. Simply put, the program asks a yes or no question; depending on the answer, different things happen.

If you've programmed in other languages, the if...else statement works the same way. The key  difference is that, in Python, the elseif statement is written as elif for checking multiple conditions, as shown in following screenshot:


In the preceding example, the preference() function is used to hold the main code logic. The input() function prints the string within parentheses to the user (normally a question), and accepts the user's input, and that input is assigned to the answer variable.

When checking for a yes or no condition, the only required part is the if statement. The elif (else/if) and the else statement aren't necessary. Having the else statement as a catch-all, default case is useful, especially if used with a print() command to indicate when an unexpected condition is received.

An if statement can be standalone, as shown here:

x = True
y = False
if x == True:
y = True
print(y)

More common is an if...else block, to have two different options depending on the condition, as shown here:

x = True
y = False
if x == True:
y = True
print(y)
else:
print("'x' is not True")

Python doesn't have a switch or case statement, unlike other languages. A switch statement is a type of control device that allows a single variable to determine the rest of the program execution based on the
variable's value. An example of the switch statement from the C language is shown in the following example:

switch (grade) {
case "A":
printf("Outstanding!");
break;
case "B":
printf("Good job!");
break;
case "C":
printf("Satisfactory performance.");
break;
case "D":
printf("You should try harder.");
case "F":
printf("You failed.");
break;
default:
printf("Invalid grade");
}

While this is somewhat simplistic, you can probably see that a more complicated example could provide different branches to the rest of the program, if desired. The key point is that a single variable is
tested, and the results of that test are compared to a variety of options; the option that matches dictates how the program continues.

You can get the same functionality of switch statements by using if...elif tests, searching within lists, or indexing dictionaries. Since lists and dictionaries are built at runtime, they can be more flexible. Following screenshot demonstrates how a dictionary can be used to perform the same functionality as a switch statement:



Obviously, this isn't the most intuitive way to write this program. A better way would be to create the dictionary as a separate object, and then use a dictionary method such as key in dict to find the value
corresponding to your choice. In this case, you could use "spam" in choice.

However, it's more common to use if...else statements to perform this operation, as it looks similar to the normal switch choices and is the easiest way to deal with choices. The benefit to using a dictionary is that dynamic programs can create these data structures relatively easily. With if...else statements, they have to be written by the programmer prior to running the program, whereas dictionaries can be populated and tested programmatically during runtime.

Share:

0 comments:

Post a Comment