Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Else Statement | Introduction to Conditional Statements
C++ Conditional Statements
course content

Contenido del Curso

C++ Conditional Statements

C++ Conditional Statements

1. Introduction to Conditional Statements
2. Conditional Control Flow Practice
3. Advanced Topics

bookElse 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.

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.

Let's examine the provided code. 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.

cpp

main

copy
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, like this:

cpp

main

copy
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 else keyword can be followed by any statement, including another if block, 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.

Tarea
test

Swipe to show code editor

  • Examine the x and y variables using if and else keywords.
  • Display the results in the console:
    • x is greater than y, if x > y;
    • x is less than y, if x > y;
    • x is equal to y, if x == y.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
toggle bottom row

bookElse 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.

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.

Let's examine the provided code. 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.

cpp

main

copy
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, like this:

cpp

main

copy
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 else keyword can be followed by any statement, including another if block, 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.

Tarea
test

Swipe to show code editor

  • Examine the x and y variables using if and else keywords.
  • Display the results in the console:
    • x is greater than y, if x > y;
    • x is less than y, if x > y;
    • x is equal to y, if x == y.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
toggle bottom row

bookElse 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.

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.

Let's examine the provided code. 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.

cpp

main

copy
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, like this:

cpp

main

copy
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 else keyword can be followed by any statement, including another if block, 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.

Tarea
test

Swipe to show code editor

  • Examine the x and y variables using if and else keywords.
  • Display the results in the console:
    • x is greater than y, if x > y;
    • x is less than y, if x > y;
    • x is equal to y, if x == y.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

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.

Let's examine the provided code. 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.

cpp

main

copy
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, like this:

cpp

main

copy
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 else keyword can be followed by any statement, including another if block, 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.

Tarea
test

Swipe to show code editor

  • Examine the x and y variables using if and else keywords.
  • Display the results in the console:
    • x is greater than y, if x > y;
    • x is less than y, if x > y;
    • x is equal to y, if x == y.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt