Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If 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

bookIf Statement

The if statement is a foundational building block of control flow in most programming languages. It allows a program to make decisions and execute different blocks of code based on whether a given condition is true or false. The idea behind if statements is simple: If a condition is met, do something, otherwise, don’t.

Here is the if statement syntax:

  • condition is a boolean expression that evaluates to either true or false;
  • If the condition is true, the code inside the curly braces { } is executed;
  • If the condition is false, the code inside the if block is skipped, and program execution continues with the next statement after the if block.
cpp

main

copy
123456789101112
#include <iostream> int main() { int age = 33; // declaring and initializing a variable if (age >= 18) // checking whether the age is greater or equal to 18 { // if so, output the message std::cout << "You are an adult" << std::endl; } }

If you have an if statement with only one statement to be executed when the condition is true, you can omit the curly braces { }. For example:

Note

The condition within an if statement requires a bool type. As you may be aware from C++ Data Types course, you can obtain a bool type by performing complex operations using || (logical OR) and && (logical AND). Therefore, the condition in an if statement can also consist of these complex operations.

Tarea
test

Swipe to show code editor

  • Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  • Output Can fit in console if it can.

Once you've completed this task, click the button below the code to check your solution.

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 3
toggle bottom row

bookIf Statement

The if statement is a foundational building block of control flow in most programming languages. It allows a program to make decisions and execute different blocks of code based on whether a given condition is true or false. The idea behind if statements is simple: If a condition is met, do something, otherwise, don’t.

Here is the if statement syntax:

  • condition is a boolean expression that evaluates to either true or false;
  • If the condition is true, the code inside the curly braces { } is executed;
  • If the condition is false, the code inside the if block is skipped, and program execution continues with the next statement after the if block.
cpp

main

copy
123456789101112
#include <iostream> int main() { int age = 33; // declaring and initializing a variable if (age >= 18) // checking whether the age is greater or equal to 18 { // if so, output the message std::cout << "You are an adult" << std::endl; } }

If you have an if statement with only one statement to be executed when the condition is true, you can omit the curly braces { }. For example:

Note

The condition within an if statement requires a bool type. As you may be aware from C++ Data Types course, you can obtain a bool type by performing complex operations using || (logical OR) and && (logical AND). Therefore, the condition in an if statement can also consist of these complex operations.

Tarea
test

Swipe to show code editor

  • Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  • Output Can fit in console if it can.

Once you've completed this task, click the button below the code to check your solution.

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 3
toggle bottom row

bookIf Statement

The if statement is a foundational building block of control flow in most programming languages. It allows a program to make decisions and execute different blocks of code based on whether a given condition is true or false. The idea behind if statements is simple: If a condition is met, do something, otherwise, don’t.

Here is the if statement syntax:

  • condition is a boolean expression that evaluates to either true or false;
  • If the condition is true, the code inside the curly braces { } is executed;
  • If the condition is false, the code inside the if block is skipped, and program execution continues with the next statement after the if block.
cpp

main

copy
123456789101112
#include <iostream> int main() { int age = 33; // declaring and initializing a variable if (age >= 18) // checking whether the age is greater or equal to 18 { // if so, output the message std::cout << "You are an adult" << std::endl; } }

If you have an if statement with only one statement to be executed when the condition is true, you can omit the curly braces { }. For example:

Note

The condition within an if statement requires a bool type. As you may be aware from C++ Data Types course, you can obtain a bool type by performing complex operations using || (logical OR) and && (logical AND). Therefore, the condition in an if statement can also consist of these complex operations.

Tarea
test

Swipe to show code editor

  • Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  • Output Can fit in console if it can.

Once you've completed this task, click the button below the code to check your solution.

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 if statement is a foundational building block of control flow in most programming languages. It allows a program to make decisions and execute different blocks of code based on whether a given condition is true or false. The idea behind if statements is simple: If a condition is met, do something, otherwise, don’t.

Here is the if statement syntax:

  • condition is a boolean expression that evaluates to either true or false;
  • If the condition is true, the code inside the curly braces { } is executed;
  • If the condition is false, the code inside the if block is skipped, and program execution continues with the next statement after the if block.
cpp

main

copy
123456789101112
#include <iostream> int main() { int age = 33; // declaring and initializing a variable if (age >= 18) // checking whether the age is greater or equal to 18 { // if so, output the message std::cout << "You are an adult" << std::endl; } }

If you have an if statement with only one statement to be executed when the condition is true, you can omit the curly braces { }. For example:

Note

The condition within an if statement requires a bool type. As you may be aware from C++ Data Types course, you can obtain a bool type by performing complex operations using || (logical OR) and && (logical AND). Therefore, the condition in an if statement can also consist of these complex operations.

Tarea
test

Swipe to show code editor

  • Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  • Output Can fit in console if it can.

Once you've completed this task, click the button below the code to check your solution.

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 3
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