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
1234if (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
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
without_braces.h
1234if (condition) { statement; }
Swipe to start coding
- Check if a two dimensional square with
x
(length) andy
(height) can fit into another square with dimensionsx1
(length) andy1
(height). - Output
Can fit
in console if it can.
Ratkaisu
solution.cpp
Kiitos palautteestasi!
single
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 7.69
If Statement
Pyyhkäise näyttääksesi valikon
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
1234if (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
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
without_braces.h
1234if (condition) { statement; }
Swipe to start coding
- Check if a two dimensional square with
x
(length) andy
(height) can fit into another square with dimensionsx1
(length) andy1
(height). - Output
Can fit
in console if it can.
Ratkaisu
solution.cpp
Kiitos palautteestasi!
Awesome!
Completion rate improved to 7.69single