Print numbers 1–10 using for loop
Table of Contents
Introduction
Loops are fundamental constructs in programming that allow a block of code to execute repeatedly based on certain conditions. In this lesson, we will focus on the for loop, which is widely used when the number of iterations is known. Learning loops is crucial for automating repetitive tasks, performing calculations efficiently, and building scalable programs. By the end of this lesson, students will not only understand how loops work but also be able to implement them in real-world programming scenarios.
Objectives
This lesson is aligned with the Outcomes-Based Education (OBE) framework and focuses on four core objectives:
- Understand: Recognize the structure, syntax, and purpose of a for loop in programming.
- Learn: Identify the components of a loop, including initialization, condition, and iteration expression.
- Practice: Write and execute simple programs that use loops to perform repetitive tasks.
- Apply: Implement loops in problem-solving scenarios to enhance programming efficiency and logic.
Through these objectives, learners will gradually build confidence in using loops and integrate them into larger programming projects.
Beginner-Friendly Source Code
Below is a simple C# example that prints numbers from 1 to 10 using a for loop. The example includes clear instructions to help beginners follow along.
using System;
class Program
{
static void Main()
{
// This program prints numbers 1 to 10 using a for loop
Console.WriteLine("Numbers from 1 to 10:");
// For loop starts here
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
// End of program
Console.WriteLine("Loop finished!");
}
}
Instructions:
- Open your C# development environment (Visual Studio, Visual Studio Code, or an online compiler like .NET Fiddle).
- Copy and paste the code into a new project or file.
- Run the program.
- Observe the output, which prints numbers from 1 to 10 line by line.
Example Output:
Numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10
Loop finished!
Summary
In this lesson, learners explored the for loop, a powerful tool in programming for repeating tasks. We discussed the loop’s syntax, components, and practical applications. By writing a simple program to print numbers 1 to 10, students practiced implementing loops and gained confidence in controlling program flow. Understanding loops is essential for building more complex programs and optimizing repetitive tasks efficiently.
Multiple Choice Questions
1. What is the correct syntax to start a for loop in C#?
A) for(int i=0; i<10; i++)
B) for(i=1 to 10)
C) loop(i=1; i<=10)
D) while(i<10)
2. What will the following code output?
for(int i=1; i<=5; i++)
{
Console.WriteLine(i);
}
A) 1 2 3 4 5
B) 0 1 2 3 4
C) 1 2 3 4 5 6
D) 5 4 3 2 1
3. Which of the following modifications will make the loop print only even numbers from 1 to 10?
A) for(int i=2; i<=10; i+=2)
B) for(int i=1; i<=10; i++)
C) for(int i=1; i<=10; i-=2)
D) for(int i=0; i<10; i++)
4. What happens if the loop condition is written as i<10 instead of i<=10?
A) The loop will print 1 to 10
B) The loop will print 1 to 9
C) The loop will print 0 to 9
D) The loop will not run
5. Which scenario is most appropriate for using a for loop instead of a while loop?
A) When the number of iterations is known
B) When waiting for user input indefinitely
C) When processing data until an error occurs
D) When breaking a loop randomly
Suggested Exercises and Assessment
Introduction
Practice is essential to solidify the understanding of loops. The following exercises and assessment tasks aim to enhance problem-solving skills and improve students’ ability to write efficient loop-based programs.
Exercises
- Modify the program to print numbers 10 to 1 in descending order.
- Print all odd numbers from 1 to 20 using a for loop.
- Write a program that calculates the sum of numbers from 1 to 50.
- Nested loops: Print a multiplication table from 1 to 5.
Assessment
- Write a program that takes a number
nfrom the user and prints all numbers from 1 tonusing a for loop.
Lab Exam
- Create a program using a for loop that prints the first 10 Fibonacci numbers.
- Implement input validation using a loop to ensure the user enters a positive integer.
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.