Challenge: Else Statement
The else statement is a crucial element in controlling the program flow. It complements the if statement by allowing a program to take different actions depending on whether a given condition is true or false. Essentially, it enables the program to choose between two alternative paths.
else.h
12345678if (condition) { // Code to be executed if the condition is true } else { // Code to be executed if the condition is false }
If the condition is false, the code inside the first block (the one following if) is skipped, and the code inside the second block (the one following else) is executed.
When executed, it appears to be functioning as expected. Since the variable x is less than 10, the output correctly states: X is not greater than 10.
main.cpp
12345678910111213#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } std::cout << "X is not greater than 10" << std::endl; }
But try to modify the value of a variable x to a number greater than 10, and then execute the provided code snippet once more. As you can see there are two distinct messages: one indicating that X is greater than 10, and the other specifying that X is not greater than 10.
The key thing to understand is that the second std::cout statement is not part of the if block, it is always be executed unconditionally after the if block resulting in both messages being printed. Of course this has to be fixed and this issue can be resolved by using an else keyword.
main.cpp
123456789101112131415#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } else { std::cout << "X is not greater than 10" << std::endl; } }
Now everything works as expected and only one of two instructions will be executed depending on the value of the x.
Note
The
elsekeyword can be followed by any statement, including anotherifblock, and this chain can be extended as necessary, but it's important not to overuse this construct because it can make it challenging to comprehend the program's flow.
Swipe to start coding
You are developing a shipping calculator that determines the cost of delivery based on the weight of a package.
- Inside the
calculateShippingCostfunction, create a variablecostto store the shipping price. - Use conditional statements to determine the cost:
- If
weightKgis less than or equal to 2, set the cost to 5.0. - If
weightKgis less than or equal to 5, set the cost to 10.0. - Otherwise, set the cost to 20.0.
- If
- Return the
costfrom the function.
Example
calculateShippingCost(1.5) → 5.0
calculateShippingCost(4.0) → 10.0
calculateShippingCost(6.8) → 20.0
Oplossing
solution.cpp
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show an example of how to use `else if` in this context?
What happens if I omit the `else` statement entirely?
Can you explain why overusing `else` and `else if` can make code harder to read?
Awesome!
Completion rate improved to 7.69
Challenge: Else Statement
Veeg om het menu te tonen
The else statement is a crucial element in controlling the program flow. It complements the if statement by allowing a program to take different actions depending on whether a given condition is true or false. Essentially, it enables the program to choose between two alternative paths.
else.h
12345678if (condition) { // Code to be executed if the condition is true } else { // Code to be executed if the condition is false }
If the condition is false, the code inside the first block (the one following if) is skipped, and the code inside the second block (the one following else) is executed.
When executed, it appears to be functioning as expected. Since the variable x is less than 10, the output correctly states: X is not greater than 10.
main.cpp
12345678910111213#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } std::cout << "X is not greater than 10" << std::endl; }
But try to modify the value of a variable x to a number greater than 10, and then execute the provided code snippet once more. As you can see there are two distinct messages: one indicating that X is greater than 10, and the other specifying that X is not greater than 10.
The key thing to understand is that the second std::cout statement is not part of the if block, it is always be executed unconditionally after the if block resulting in both messages being printed. Of course this has to be fixed and this issue can be resolved by using an else keyword.
main.cpp
123456789101112131415#include <iostream> int main() { int x = 5; if (x > 10) { std::cout << "X is greater than 10" << std::endl; } else { std::cout << "X is not greater than 10" << std::endl; } }
Now everything works as expected and only one of two instructions will be executed depending on the value of the x.
Note
The
elsekeyword can be followed by any statement, including anotherifblock, and this chain can be extended as necessary, but it's important not to overuse this construct because it can make it challenging to comprehend the program's flow.
Swipe to start coding
You are developing a shipping calculator that determines the cost of delivery based on the weight of a package.
- Inside the
calculateShippingCostfunction, create a variablecostto store the shipping price. - Use conditional statements to determine the cost:
- If
weightKgis less than or equal to 2, set the cost to 5.0. - If
weightKgis less than or equal to 5, set the cost to 10.0. - Otherwise, set the cost to 20.0.
- If
- Return the
costfrom the function.
Example
calculateShippingCost(1.5) → 5.0
calculateShippingCost(4.0) → 10.0
calculateShippingCost(6.8) → 20.0
Oplossing
solution.cpp
Bedankt voor je feedback!
single