Introuction to Loops in C#
Using Selection and Iteration (loop)
Example-1
Write an algorithm for an application that
will display numbers from 1 to 10 and at the same time will display if the number is a multiple of 3.
Use a for loop from 1 to 10 and Inside the body of the for loop:
Check if the number is multiple of 3 (number % 3 = 0) then display the number like this: 6 is a multiple of 3
otherwise just display the number only, for example: 7
The algorithm/pseudocode of example-1
for counter=1 , counter ≤ 10 , counter = counter +1
if counter % 3 = 0
Display counter is multiple of 3
else
Display counter
Example-2
Write an algorithm for an application that that will ask the user to enter his salary.
- If the salary textbox is empty the program displays a message asking the user to enter his salary
- Otherwiser, The program reads the salary from the textbox
- The program uses a while loop to calculate the salary for 3 years, each year the employee gets 500 AED increment.
The program displays the year and the salary.
The algorithm/pseudocode of example-2
if salary text box is empty
Display an error message
else
Get salary
Set counter to 1
while counter ≤ 3
Calculate salary as salary + 500
Display counter and salary
counter = counter +1
Here is a scenario of the output:
For more details, please contact me here.