The document describes three types of loops: for, while, and do while, each initialized to iterate from 1 to 10 or a user-defined integer n. It explains how these loops can be used to print numbers, calculate squares, and compute products. Each loop type ensures proper incrementing and condition checking to execute the desired operations effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views12 pages
Conditional Statement
The document describes three types of loops: for, while, and do while, each initialized to iterate from 1 to 10 or a user-defined integer n. It explains how these loops can be used to print numbers, calculate squares, and compute products. Each loop type ensures proper incrementing and condition checking to execute the desired operations effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12
The for loop initializes the variable i to 1.
The loop continues as long as i <= 10.
After each iteration, i increments by 1 using i++. The while loop initializes i to 1. The loop condition checks if i <= 10. It prints the value of i and increments i by 1 in each iteration. The do while loop executes the code block once before checking the condition. The loop runs as long as i <= 10. The program accepts an integer input. A for loop runs from 1 to n and prints the number and its square. Similar to the for loop but using while, which continues looping while i <= n. Uses a do while loop to ensure the code block executes at least once. A for loop calculates the product of numbers from 1 to n. The while loop continues multiplying until i <= n. Ensures at least one multiplication happens. This program uses a for loop to iterate through numbers from 1 to n. For each number i, it calculates the square (i * i) and adds it to sum. The final value of sum is printed after the loop. Here, the while loop starts with i = 1 and continues as long as i <= n. Inside the loop, it calculates i * i and adds it to sum. The value of i is incremented by 1 on each iteration until the condition is no longer met. This solution ensures the loop runs at least once, regardless of the initial value of n. It starts by adding the square of i to sum and then increments i. The loop condition is checked at the end, so it continues to sum the squares as long as i <= n.