Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Conditional Statements | Introduction to Program Flow
C++ Introduction

bookConditional Statements

The if construct in programming enables your program to make decisions and handle different scenarios. It has two key components: a condition that evaluates to either true or false, and the actions or consequences that follow based on the outcome of that condition.

if_statement.cpp

if_statement.cpp

copy
1234
if (condition) { // Actions to take if the condition is `true` }

The common use case is to compare values and make decisions based on conditions. You can write the program that checks whether the balance meets a certain condition before confirming the transaction. Depending on the value, it prints a different message.

main.cpp

main.cpp

copy
123456789101112131415
#include<iostream> int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } if (balance < 13) { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

There are also the else construct in programming. It is used in conjunction with an if statement to define an alternative set of actions that should be executed when the condition in the if statement is false.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

You can have additional if...else statements nested inside another if...else block. This is known as nested if...else. This allows for more complex decision-making, where multiple conditions can be checked sequentially and different actions can be taken based on these conditions.

main.cpp

main.cpp

format_example.cpp

format_example.cpp

copy
12345678910111213141516171819202122
#include<iostream> int main() { int balance = 25; if (balance >= 13) // Check if balance β‰₯ 13 { if (balance >= 20) // Check if balance β‰₯ 20 { std::cout << "Balance β‰₯ 20: Approved" << std::endl; } else { std::cout << "Balance 13–19: Okay" << std::endl; } } else { std::cout << "Balance < 13: Not okay" << std::endl; } }
Note
Note

If there is only one statement to execute within an if or else block, you can omit the curly braces. This can make the code more concise, but it also reduces clarity, especially in more complex conditions.

question mark

When does the else block in an if...else statement execute?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of how to use if and else in code?

What are some common mistakes to avoid when using if...else statements?

How does nesting if...else statements affect program readability?

Awesome!

Completion rate improved to 3.85

bookConditional Statements

Swipe to show menu

The if construct in programming enables your program to make decisions and handle different scenarios. It has two key components: a condition that evaluates to either true or false, and the actions or consequences that follow based on the outcome of that condition.

if_statement.cpp

if_statement.cpp

copy
1234
if (condition) { // Actions to take if the condition is `true` }

The common use case is to compare values and make decisions based on conditions. You can write the program that checks whether the balance meets a certain condition before confirming the transaction. Depending on the value, it prints a different message.

main.cpp

main.cpp

copy
123456789101112131415
#include<iostream> int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } if (balance < 13) { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

There are also the else construct in programming. It is used in conjunction with an if statement to define an alternative set of actions that should be executed when the condition in the if statement is false.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

You can have additional if...else statements nested inside another if...else block. This is known as nested if...else. This allows for more complex decision-making, where multiple conditions can be checked sequentially and different actions can be taken based on these conditions.

main.cpp

main.cpp

format_example.cpp

format_example.cpp

copy
12345678910111213141516171819202122
#include<iostream> int main() { int balance = 25; if (balance >= 13) // Check if balance β‰₯ 13 { if (balance >= 20) // Check if balance β‰₯ 20 { std::cout << "Balance β‰₯ 20: Approved" << std::endl; } else { std::cout << "Balance 13–19: Okay" << std::endl; } } else { std::cout << "Balance < 13: Not okay" << std::endl; } }
Note
Note

If there is only one statement to execute within an if or else block, you can omit the curly braces. This can make the code more concise, but it also reduces clarity, especially in more complex conditions.

question mark

When does the else block in an if...else statement execute?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
some-alt