Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: If Statement | Introduction to Conditional Statements
C++ Conditional Statements

bookChallenge: If 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.

if.h

if.h

copy
1234
if (condition) { // Code to be executed if the condition is true }

A condition is a boolean expression that evaluates to either true or false. If it is true, the code within the curly braces { } is executed; otherwise, if the condition is false, the code inside the block is skipped, and the program continues with the next statement following the if block.

main.cpp

main.cpp

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 { }.

with_braces.h

with_braces.h

without_braces.h

without_braces.h

copy
1234
if (condition) { statement; }
Opgave

Swipe to start coding

You are building a shopping system where customers may receive a discount based on the total amount of their purchase.

The function calculateDiscount takes a double parameter totalPurchase representing the total sum of the customer's purchase.

  1. Initialize a variable discount with 0.0.
  2. Check if totalPurchase is greater than or equal to 100.
    • If it is, set the discount to 30 percent of the total purchase. Multiply totalPurchase by 0.3
  3. Return the value of discount from the function.

Example

calculateDiscount(50)0.0
calculateDiscount(100)30.0
calculateDiscount(200)60.0

Løsning

solution.cpp

solution.cpp

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you give me an example of an if statement with and without curly braces?

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

How do I use if statements with multiple conditions?

close

Awesome!

Completion rate improved to 7.69

bookChallenge: If Statement

Stryg for at vise menuen

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.

if.h

if.h

copy
1234
if (condition) { // Code to be executed if the condition is true }

A condition is a boolean expression that evaluates to either true or false. If it is true, the code within the curly braces { } is executed; otherwise, if the condition is false, the code inside the block is skipped, and the program continues with the next statement following the if block.

main.cpp

main.cpp

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 { }.

with_braces.h

with_braces.h

without_braces.h

without_braces.h

copy
1234
if (condition) { statement; }
Opgave

Swipe to start coding

You are building a shopping system where customers may receive a discount based on the total amount of their purchase.

The function calculateDiscount takes a double parameter totalPurchase representing the total sum of the customer's purchase.

  1. Initialize a variable discount with 0.0.
  2. Check if totalPurchase is greater than or equal to 100.
    • If it is, set the discount to 30 percent of the total purchase. Multiply totalPurchase by 0.3
  3. Return the value of discount from the function.

Example

calculateDiscount(50)0.0
calculateDiscount(100)30.0
calculateDiscount(200)60.0

Løsning

solution.cpp

solution.cpp

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

some-alt