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

Kursusindhold

C++ Conditional Statements

C++ Conditional Statements

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

book
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.

h

if

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.

cpp

main

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

h

with_braces

h

without_braces

copy
1234
if (condition) { statement; }
Opgave

Swipe to start coding

  1. Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  2. Output Can fit in console if it can.

Løsning

cpp

solution

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
toggle bottom row

book
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.

h

if

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.

cpp

main

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

h

with_braces

h

without_braces

copy
1234
if (condition) { statement; }
Opgave

Swipe to start coding

  1. Check if a two dimensional square with x (length) and y (height) can fit into another square with dimensions x1 (length) and y1 (height).
  2. Output Can fit in console if it can.

Løsning

cpp

solution

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
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt