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

Conteúdo do Curso

C++ Conditional Statements

C++ Conditional Statements

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

bookNested If Statements

A nested if statement is simply an if statement within another if statement. This structure enables you to evaluate multiple conditions, one after the other, and execute specific code blocks. The outer if statement acts as a gatekeeper, and based on its evaluation, it may open the door to another if statement inside.

Consider a scenario where we want to determine a worker salary based on their performance.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 17; int hours_worked = 37; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // And if the number of hours worked is more than 40 if (hours_worked > 40) { // add an additional 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } } std::cout << current_salary << std::endl; }

This code calculates an employee's new salary based on the number of completed tasks and hours worked, with a 20% increase if the tasks are over 15 and an additional 20% increase if hours worked are over 40. As you can see, the current calculations is 1200. And this can be achieved only by using nested if statements, here some attempts to get same logic without them.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 9; int hours_worked = 41; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } // If the number of hours worked is more than 40 if (hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } std::cout << current_salary << std::endl; }

At first it may seem that it works the same, but in this case the worker will receive an extra 20% raise, regardless of whether he has completed more than 15 tasks. Run the code and look at the output, it shows a value of 1200, even though this time the worker didn't completed more than 15 tasks.

cpp

main

copy
1234567891011121314151617181920212223
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 19; int hours_worked = 39; // If the number of completed tasks is greater than 15 // AND the number of of hours worked is more than 40 if (completed_tasks > 15 && hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; std::cout << current_salary << std::endl; } std::cout << current_salary << std::endl; }

In that case, it may seem like it should operate as intended, but unfortunately, it is also incorrect, the output is 1000. This is because if the worker completes more than 15 tasks but doesn't work more than 40 hours, they won't receive anything. So, we have to use nested if statements in order to get the right implementation.

Nested if statements are really useful for creating decision-making processes and handling complex scenarios in your code. By carefully designing your code structure and maintaining readability, you can harness the power of nested if statements to write efficient and maintainable programs.

Note

Nested if statements are necessary in some cases but not always. In the third section, we'll discuss when, why and how to avoid them.

Tarefa

We have an excess of pink phones in our inventory that we need to sell.

  • Make a 20% discount on pink phones.
  • If a phone's price exceeds $1,000, the discount will be reduced to 10%.
  • For all other phones, increase the price by 10%.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

bookNested If Statements

A nested if statement is simply an if statement within another if statement. This structure enables you to evaluate multiple conditions, one after the other, and execute specific code blocks. The outer if statement acts as a gatekeeper, and based on its evaluation, it may open the door to another if statement inside.

Consider a scenario where we want to determine a worker salary based on their performance.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 17; int hours_worked = 37; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // And if the number of hours worked is more than 40 if (hours_worked > 40) { // add an additional 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } } std::cout << current_salary << std::endl; }

This code calculates an employee's new salary based on the number of completed tasks and hours worked, with a 20% increase if the tasks are over 15 and an additional 20% increase if hours worked are over 40. As you can see, the current calculations is 1200. And this can be achieved only by using nested if statements, here some attempts to get same logic without them.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 9; int hours_worked = 41; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } // If the number of hours worked is more than 40 if (hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } std::cout << current_salary << std::endl; }

At first it may seem that it works the same, but in this case the worker will receive an extra 20% raise, regardless of whether he has completed more than 15 tasks. Run the code and look at the output, it shows a value of 1200, even though this time the worker didn't completed more than 15 tasks.

cpp

main

copy
1234567891011121314151617181920212223
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 19; int hours_worked = 39; // If the number of completed tasks is greater than 15 // AND the number of of hours worked is more than 40 if (completed_tasks > 15 && hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; std::cout << current_salary << std::endl; } std::cout << current_salary << std::endl; }

In that case, it may seem like it should operate as intended, but unfortunately, it is also incorrect, the output is 1000. This is because if the worker completes more than 15 tasks but doesn't work more than 40 hours, they won't receive anything. So, we have to use nested if statements in order to get the right implementation.

Nested if statements are really useful for creating decision-making processes and handling complex scenarios in your code. By carefully designing your code structure and maintaining readability, you can harness the power of nested if statements to write efficient and maintainable programs.

Note

Nested if statements are necessary in some cases but not always. In the third section, we'll discuss when, why and how to avoid them.

Tarefa

We have an excess of pink phones in our inventory that we need to sell.

  • Make a 20% discount on pink phones.
  • If a phone's price exceeds $1,000, the discount will be reduced to 10%.
  • For all other phones, increase the price by 10%.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

bookNested If Statements

A nested if statement is simply an if statement within another if statement. This structure enables you to evaluate multiple conditions, one after the other, and execute specific code blocks. The outer if statement acts as a gatekeeper, and based on its evaluation, it may open the door to another if statement inside.

Consider a scenario where we want to determine a worker salary based on their performance.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 17; int hours_worked = 37; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // And if the number of hours worked is more than 40 if (hours_worked > 40) { // add an additional 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } } std::cout << current_salary << std::endl; }

This code calculates an employee's new salary based on the number of completed tasks and hours worked, with a 20% increase if the tasks are over 15 and an additional 20% increase if hours worked are over 40. As you can see, the current calculations is 1200. And this can be achieved only by using nested if statements, here some attempts to get same logic without them.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 9; int hours_worked = 41; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } // If the number of hours worked is more than 40 if (hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } std::cout << current_salary << std::endl; }

At first it may seem that it works the same, but in this case the worker will receive an extra 20% raise, regardless of whether he has completed more than 15 tasks. Run the code and look at the output, it shows a value of 1200, even though this time the worker didn't completed more than 15 tasks.

cpp

main

copy
1234567891011121314151617181920212223
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 19; int hours_worked = 39; // If the number of completed tasks is greater than 15 // AND the number of of hours worked is more than 40 if (completed_tasks > 15 && hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; std::cout << current_salary << std::endl; } std::cout << current_salary << std::endl; }

In that case, it may seem like it should operate as intended, but unfortunately, it is also incorrect, the output is 1000. This is because if the worker completes more than 15 tasks but doesn't work more than 40 hours, they won't receive anything. So, we have to use nested if statements in order to get the right implementation.

Nested if statements are really useful for creating decision-making processes and handling complex scenarios in your code. By carefully designing your code structure and maintaining readability, you can harness the power of nested if statements to write efficient and maintainable programs.

Note

Nested if statements are necessary in some cases but not always. In the third section, we'll discuss when, why and how to avoid them.

Tarefa

We have an excess of pink phones in our inventory that we need to sell.

  • Make a 20% discount on pink phones.
  • If a phone's price exceeds $1,000, the discount will be reduced to 10%.
  • For all other phones, increase the price by 10%.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

A nested if statement is simply an if statement within another if statement. This structure enables you to evaluate multiple conditions, one after the other, and execute specific code blocks. The outer if statement acts as a gatekeeper, and based on its evaluation, it may open the door to another if statement inside.

Consider a scenario where we want to determine a worker salary based on their performance.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 17; int hours_worked = 37; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // And if the number of hours worked is more than 40 if (hours_worked > 40) { // add an additional 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } } std::cout << current_salary << std::endl; }

This code calculates an employee's new salary based on the number of completed tasks and hours worked, with a 20% increase if the tasks are over 15 and an additional 20% increase if hours worked are over 40. As you can see, the current calculations is 1200. And this can be achieved only by using nested if statements, here some attempts to get same logic without them.

cpp

main

copy
12345678910111213141516171819202122232425
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 9; int hours_worked = 41; // If the number of completed tasks is greater than 15, if (completed_tasks > 15) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } // If the number of hours worked is more than 40 if (hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; } std::cout << current_salary << std::endl; }

At first it may seem that it works the same, but in this case the worker will receive an extra 20% raise, regardless of whether he has completed more than 15 tasks. Run the code and look at the output, it shows a value of 1200, even though this time the worker didn't completed more than 15 tasks.

cpp

main

copy
1234567891011121314151617181920212223
#include <iostream> int main() { int current_salary = 1000; int completed_tasks = 19; int hours_worked = 39; // If the number of completed tasks is greater than 15 // AND the number of of hours worked is more than 40 if (completed_tasks > 15 && hours_worked > 40) { // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; // add an 20% increase to the current salary current_salary = current_salary + current_salary * 0.2; std::cout << current_salary << std::endl; } std::cout << current_salary << std::endl; }

In that case, it may seem like it should operate as intended, but unfortunately, it is also incorrect, the output is 1000. This is because if the worker completes more than 15 tasks but doesn't work more than 40 hours, they won't receive anything. So, we have to use nested if statements in order to get the right implementation.

Nested if statements are really useful for creating decision-making processes and handling complex scenarios in your code. By carefully designing your code structure and maintaining readability, you can harness the power of nested if statements to write efficient and maintainable programs.

Note

Nested if statements are necessary in some cases but not always. In the third section, we'll discuss when, why and how to avoid them.

Tarefa

We have an excess of pink phones in our inventory that we need to sell.

  • Make a 20% discount on pink phones.
  • If a phone's price exceeds $1,000, the discount will be reduced to 10%.
  • For all other phones, increase the price by 10%.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt