0
0
C++programming~15 mins

If statement in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
If statement
📖 Scenario: You are creating a simple program that checks if a number is positive or not. This is like checking if the temperature outside is above zero degrees to decide if you need a jacket.
🎯 Goal: Build a program that uses an if statement to check if a number is positive and prints a message accordingly.
📋 What You'll Learn
Create an integer variable called number with the value 10
Create a boolean variable called isPositive that will store if the number is positive
Use an if statement to set isPositive to true if number is greater than 0
Print "The number is positive" if isPositive is true
💡 Why This Matters
🌍 Real World
Checking conditions like this helps programs make decisions, such as turning on a heater if the temperature is cold.
💼 Career
Understanding if statements is essential for any programming job because it controls the flow of decisions in software.
Progress0 / 4 steps
1
DATA SETUP: Create the number variable
Create an integer variable called number and set it to 10.
C++
Need a hint?

Use int number = 10; to create the variable.

2
CONFIGURATION: Create the isPositive variable
Create a boolean variable called isPositive and set it to false.
C++
Need a hint?

Use bool isPositive = false; to create the variable.

3
CORE LOGIC: Use if statement to check positivity
Use an if statement to check if number is greater than 0. Inside the if, set isPositive to true.
C++
Need a hint?

Write if (number > 0) { isPositive = true; } to check and update the variable.

4
OUTPUT: Print the result if number is positive
Write a std::cout statement to print "The number is positive" only if isPositive is true.
C++
Need a hint?

Use if (isPositive) { std::cout << "The number is positive" << std::endl; } to print the message.