Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Why Programs Need Decisions and Repetition | Understanding Program Flow and Decisions
Conditional Statements and Loops in JavaScript

bookWhy Programs Need Decisions and Repetition

Imagine you are deciding what to wear in the morning. If it is raining, you choose a raincoat. If it is sunny, pick a T-shirt. This decision is based on the weather. After making your choice, you might brush your teeth, eat breakfast, and pack your bag, actions you repeat every day. Programs work similarly. They make decisions and repeat actions, allowing them to solve problems and handle many situations automatically.

12345678910
// Making a decision with if let weather = "rainy"; if (weather === "rainy") { console.log("Take an umbrella!"); } // Repeating an action with a for loop for (let day = 1; day <= 3; day++) { console.log("Brush your teeth. Day: " + day); }
copy

The code above shows how a computer can make a choice—telling you to take an umbrella if it is rainy—and how it can repeat an action, such as reminding you to brush your teeth for three days in a row. This mirrors the way you make decisions and follow routines in your daily life.

question mark

Which part of the code makes a decision, and which part repeats an action?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how the if statement works in this example?

What does the for loop do in the code?

Can you give more examples of decisions and repetitions in programming?

Awesome!

Completion rate improved to 7.69

bookWhy Programs Need Decisions and Repetition

Sveip for å vise menyen

Imagine you are deciding what to wear in the morning. If it is raining, you choose a raincoat. If it is sunny, pick a T-shirt. This decision is based on the weather. After making your choice, you might brush your teeth, eat breakfast, and pack your bag, actions you repeat every day. Programs work similarly. They make decisions and repeat actions, allowing them to solve problems and handle many situations automatically.

12345678910
// Making a decision with if let weather = "rainy"; if (weather === "rainy") { console.log("Take an umbrella!"); } // Repeating an action with a for loop for (let day = 1; day <= 3; day++) { console.log("Brush your teeth. Day: " + day); }
copy

The code above shows how a computer can make a choice—telling you to take an umbrella if it is rainy—and how it can repeat an action, such as reminding you to brush your teeth for three days in a row. This mirrors the way you make decisions and follow routines in your daily life.

question mark

Which part of the code makes a decision, and which part repeats an action?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt