Tutorial 3 Program Control Flow So far in this series we have seen how to manage simple input/output, introduce variables into a program, and perform simple operations on those variables. In this tutorial we will be talking about control flow. This topic handles the “if-else” type of logic that we encounter when we look algorithms. This area of computer science is essential to solve even the simplest problems. The material here is also language agnostic. This means that even if you want to learn another language, such as Java or Python, the basic logic will not change even if the syntax is slightly different. Conditional statements Conditional statements form the basic “if-then-else” logic that defines almost every real world problem. A simple example is a typical morning routine. Wake up If it is a weekday, prepare a pot of coffee If it is a weekend, watch TV Eat lunch Eventhough this is a simple algorithm with only three steps, it still a conditional, or branching step. All conditional statements must have a variable, and in this example the variable is the day of the week. Another example that we are all familiar with is account creation. If you wanted to create an email account somewhere, you would need to choose a username, password, and also confirm the password. Consider the following code: #include #include using namespace std; int main() { string username; string password; string passwordConfirmation; cout << "Enter a username: "; cin >> username; cout << "Enter a password: "; cin >> password; cout << "Confirm the password: "; cin >> passwordConfirmation; if(password == passwordConfirmation) { cout << "Account created successfully" << endl; } if(password != passwordConfirmation) { cout << "Passwords do not match, account not created" << endl; } return 0; } Once again, we are going to jump ahead of the familar code to the code that is significant to the topic of this tutorial. We’ve already seen how to create and use variables so we’ll look at the code at the bottom of the program. In particular the lines: if(password == passwordConfirmation) { cout << "Account created successfully" << endl; } if(password != passwordConfirmation) { cout << "Passwords do not match, account not created" << endl; } The first line: if(password == passwordConfirmation) Contains the keyword “if”. This keyword tells the compiler that when the condition, which is generally inside of a pair of parenthesis, is true the following segment of code gets executed. If the condition is false, then it does not get executed. In this case, the variable named “password” will be compared to “passwordConfirmation”. The syntax “==” is important to remember. The double equals sign signifies a comparison, while the single equals sign is the assignment operator. If you replace the “==” with a “=” it the compiler will complain and call it an assignment. The next three lines of code: { cout << "Account created successfully" << endl; } Will only execute if those two strings are equal to eachother. At this point we will finally learn the significance of braces in C++. The braces themselves technically do nothing. The only thing they do is provide structure to the program and set aside segments of code into logical blocks. Typically, you will only see a pair of braces following immediately after some control-flow logic. In this case, any code we put inside of the braces will execute if the condition evaluates to true. The next three lines of code if(password != passwordConfirmation) { cout << "Passwords do not match, account not created" << endl; } Works basically in the same way as the last three lines of code. The only major difference is the != operator. In English, this operator translates to “Not equal to”, which means if passward is not equal to passwordConfirmation print “Passwords do not match, account not created”. Compilers think about information in your source code sequentially. Code is read from the beginning of the file to the bottom. It is possible for the compiler to jump around in the file, but not with conditions. Conditions, like our example here, Our if-if chain of comparisons works, but it’s inefficient and hard to read. If a string is equal to another string then it cannot be unequal to that same string. Logicall, A = B and A =/= B cannot be simultaniously true. We can make this more readable by changing this to and if-else statement. If-else statements work in a very similar fashion, except when the if statement is true the else statement is not tested. If we make the following change: #include #include using namespace std; int main() { string username; string password; string passwordConfirmation; cout << "Enter a username: "; cin >> username; cout << "Enter a password: "; cin >> password; cout << "Confirm the password: "; cin >> passwordConfirmation; if(password == passwordConfirmation) { cout << "Account created successfully" << endl; } else(password != passwordConfirmation) { cout << "Passwords do not match, account not created" << endl; } return 0; } When the passwords match the “else” condition is not never tested since we know when the “if” statement is true, the “else” statement cannot be. This might not seem like an important issue, and with a program this simple it shouldn’t cause any major issues. But in larger programs the complexity of this type of statement grows rapidly.