Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SIZE=7][B]Introduction to Conditionals & Logic[/B][/SIZE]
- Every program we’ve seen so far has only had one possible path of execution — they all execute line by line, from top to bottom. And every time you run one of those programs, it gives you the same exact result. But it’s the twenty-first century, and we like options!
- In this lesson, we will explore how programs make decisions by evaluating conditions and introduce logic into our code!
- We’ll be covering the following concepts:
- [LIST]
- [*][ICODE]if[/ICODE], [ICODE]else if[/ICODE], and [ICODE]else[/ICODE] statements
- [*][ICODE]switch[/ICODE] statements
- [*]Relational operators
- [*]Logical operators
- [/LIST]
- So… [I]if[/I] you’ve already learned these concepts in another language, go to the next lesson — [I]else[/I], prepare yourself and let’s get started!
- [SIZE=7][B]Coin Flip Demo[/B][/SIZE]
- Before we dive deep into the syntax of the if statement, let’s do a demo!
- Here, we have [URL='https://pastebin.com/utxtSVrN'][B]coinflip.cpp[/B][/URL] program that simulates a coin toss:
- [LIST]
- [*]50% of the time, it’s Heads.
- [*]50% of the time, it’s Tails.
- [/LIST]
- [SIZE=7][B]If Statement[/B][/SIZE]
- An [ICODE]if[/ICODE] statement is used to test an expression for truth and execute some code based on it. Here’s a simple form of the [ICODE]if[/ICODE] statement:
- [CODE]if (condition) {
- some code
- }[/CODE]
- If the [I]condition[/I] is [ICODE]true[/ICODE], then the [I]statements[/I] within are executed. Otherwise, the [I]statements[/I] are skipped and the program continues on.
- [CODE]if (flip == 1) {
- std::cout << "Heads\n";
- }[/CODE]
- The [ICODE]if[/ICODE] keyword is followed by a set of parentheses [ICODE]()[/ICODE]. Inside the parentheses [ICODE]()[/ICODE], a condition is provided that evaluates to [ICODE]true[/ICODE] or [ICODE]false[/ICODE]:
- [LIST]
- [*]If the condition evaluates to true, the code inside the curly braces {} executes.
- [*]If the condition evaluates to false, the code won’t execute.
- [/LIST]
- So in the code above, if [ICODE]flip[/ICODE] is equal to 1, the program outputs “Heads”; if it does not, then nothing happens and the program continues.
- [SIZE=7][B]Relational Operators[/B][/SIZE]
- When writing conditional statements, sometimes we need to use different types of operators to compare values. These operators are called [I]relational operators[/I].
- To have a condition, we need relational operators:
- [LIST]
- [*][ICODE]==[/ICODE] equal to
- [*][ICODE]!=[/ICODE] not equal to
- [*][ICODE]>[/ICODE] greater than
- [*][ICODE]<[/ICODE] less than
- [*][ICODE]>=[/ICODE] greater than or equal to
- [*][ICODE]<=[/ICODE] less than or equal to
- [/LIST]
- [SIZE=7][B]Else Clause[/B][/SIZE]
- We can also add an [ICODE]else[/ICODE] clause to an [ICODE]if[/ICODE] statement to provide code that will only be executed if the condition is [ICODE]false[/ICODE]. Here’s a form of an [ICODE]if[/ICODE] statement that includes an [ICODE]else[/ICODE] clause:
- [CODE]if (condition) {
- do something
- } else {
- do something else
- }[/CODE]
- [LIST]
- [*]If [I]condition[/I] is [ICODE]true[/ICODE], [I]statement1[/I] is executed. Then the program [I]skips[/I] [I]statement2[/I] and executes any code statements following the [ICODE]if[/ICODE]/[ICODE]else[/ICODE] clause.
- [*]If [I]condition[/I] is [ICODE]false[/ICODE], [I]statement1[/I] is [I]skipped[/I] and [I]statement2[/I] is executed. After [I]statement2[/I] completes, the program executes any code statements following the [ICODE]if[/ICODE]/[ICODE]else[/ICODE] clause.
- [/LIST]
- [CODE]if (coin == 1) {
- std::cout << "Heads\n";
- }
- else {
- std::cout << "Tails\n";
- }[/CODE]
- So in the code above, if [ICODE]coin[/ICODE] is equal to 1, the program outputs “Heads”; if it does not, then it outputs “Tails”.
- [B]Note:[/B] It’s either or — only one of them will execute!
- [SIZE=7][B]Else If[/B][/SIZE]
- So what happens if you want more than two possible outcomes?
- This is where [ICODE]else if[/ICODE] comes in!
- [CODE]if (condition) {
- some code
- } else if (condition) {
- some code
- } else {
- some code
- }[/CODE]
- The [ICODE]else if[/ICODE] statement always comes after the [ICODE]if[/ICODE] statement and before the [ICODE]else[/ICODE] statement. The [ICODE]else if[/ICODE] statement also takes a condition.
- And you can have more than one of them! Here’s an example with three of them:
- [CODE=cpp]if (grade == 9) {
- std::cout << "Freshman\n";
- }
- else if (grade == 10) {
- std::cout << "Sophomore\n";
- }
- else if (grade == 11) {
- std::cout << "Junior\n";
- }
- else if (grade == 12) {
- std::cout << "Senior\n";
- }
- else {
- std::cout << "Super Senior\n";
- }[/CODE]
- [SIZE=7][B]Switch Statement[/B][/SIZE]
- Now that we know how [ICODE]if[/ICODE], [ICODE]else if[/ICODE], [ICODE]else[/ICODE] work, we can write programs that have multiple outcomes. Programs with multiple outcomes are so common that C++ provides a special statement for it… the [ICODE]switch[/ICODE] statement!
- A [ICODE]switch[/ICODE] statement provides an alternative syntax that is easier to read and write. However, you are going to find these less frequently than [ICODE]if[/ICODE], [ICODE]else if[/ICODE], [ICODE]else[/ICODE] in the wild.
- A [ICODE]switch[/ICODE] statement looks like this:
- [CODE=cpp]switch (grade) {
- case 9:
- std::cout << "Freshman\n";
- break;
- case 10:
- std::cout << "Sophomore\n";
- break;
- case 11:
- std::cout << "Junior\n";
- break;
- case 12:
- std::cout << "Senior\n";
- break;
- default:
- std::cout << "Invalid\n";
- break;
- }[/CODE]
- [LIST]
- [*]The [ICODE]switch[/ICODE] keyword initiates the statement and is followed by [ICODE]()[/ICODE], which contains the value that each case will compare. In the example, the value or expression of the switch statement is [ICODE]grade[/ICODE]. One restriction on this expression is that it must evaluate to an integral type ([ICODE]int[/ICODE], [ICODE]char[/ICODE], [ICODE]short[/ICODE], [ICODE]long[/ICODE], [ICODE]long long[/ICODE], or [ICODE]enum[/ICODE]).
- [*]Inside the block, [ICODE]{}[/ICODE], there are multiple cases.
- [*]The [ICODE]case[/ICODE] keyword checks if the expression matches the specified value that comes after it. The value following the first case is [ICODE]9[/ICODE]. If the value of [ICODE]grade[/ICODE] is equal to [ICODE]9[/ICODE], then the code that follows the [ICODE]:[/ICODE] would run.
- [*]The [ICODE]break[/ICODE] keyword tells the computer to exit the block and not execute any more code or check any other cases inside the code block.
- [*]At the end of each switch statement, there is a [ICODE]default[/ICODE] statement. If none of the cases are [ICODE]true[/ICODE], then the code in the [ICODE]default[/ICODE] statement will run. It’s essentially the [ICODE]else[/ICODE] part.
- [/LIST]
- In the code above, suppose [ICODE]grade[/ICODE] is equal to [ICODE]10[/ICODE], then the output would be “Sophomore”.
- [B]Note:[/B] Without the [ICODE]break[/ICODE] keyword at the end of each case, the program would execute the code for the first matching case and [I]all[/I] subsequent cases, including the [ICODE]default[/ICODE] code. This behavior is different from [ICODE]if[/ICODE] / [ICODE]else[/ICODE] conditional statements which execute only one block of code.
- [SIZE=7][B]Review[/B][/SIZE]
- Congratulations! 🙌
- Here are some of the major concepts:
- [LIST]
- [*]An [ICODE]if[/ICODE] statement checks a condition and will execute a task if that condition evaluates to true.
- [*][ICODE]if[/ICODE] / [ICODE]else[/ICODE] statements make binary decisions and execute different code blocks based on a provided condition.
- [*]We can add more conditions using [ICODE]else if[/ICODE] statements.
- [*]Relational operators, including [ICODE]<[/ICODE], [ICODE]>[/ICODE], [ICODE]<=[/ICODE], [ICODE]>=[/ICODE], [ICODE]==[/ICODE], and [ICODE]!=[/ICODE] can compare two values.
- [*]A [ICODE]switch[/ICODE] statement can be used to simplify the process of writing multiple [ICODE]else if[/ICODE] statements. The [ICODE]break[/ICODE] keyword stops the remaining cases from being checked and executed in a switch statement.
- [/LIST]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement