 Why Programs Need Decisions and Repetition
Why 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); }
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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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 Why Programs Need Decisions and Repetition
Why 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); }
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.
Takk for tilbakemeldingene dine!