0
0
C Sharp (C#)programming~20 mins

Do-while loop execution model in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Do-while loop execution model
📖 Scenario: Imagine you are creating a simple program that asks a user to enter a number repeatedly until they enter zero. This is like asking a friend to keep telling you numbers until they say "stop" by typing zero.
🎯 Goal: You will build a program using a do-while loop that keeps asking for numbers and adds them up. The program will stop asking when the user enters zero, then it will show the total sum of all numbers entered.
📋 What You'll Learn
Create an integer variable sum to hold the total.
Create an integer variable number to hold the current input.
Use a do-while loop to ask the user for a number.
Add the entered number to sum inside the loop.
Stop the loop when the user enters zero.
Print the total sum after the loop ends.
💡 Why This Matters
🌍 Real World
Do-while loops are useful when you want to run a task at least once and then repeat it based on a condition, like asking for user input until they want to stop.
💼 Career
Understanding do-while loops helps in many programming jobs where repeated actions are needed, such as menu systems, data entry, or game loops.
Progress0 / 4 steps
1
Create variables for sum and number
Create an integer variable called sum and set it to 0. Also create an integer variable called number and set it to 0.
C Sharp (C#)
Need a hint?

Think of sum as your total score and number as the new score you get each time.

2
Set up the do-while loop
Write a do block that asks the user to enter a number using Console.ReadLine() and converts it to an integer stored in number. Then add number to sum. Use a while condition to continue the loop as long as number is not zero.
C Sharp (C#)
Need a hint?

The do-while loop runs the code inside do first, then checks the while condition.

3
Adjust sum to exclude the last zero
After the do-while loop, subtract number from sum to remove the last zero added.
C Sharp (C#)
Need a hint?

Since zero was added last, subtract it to keep the sum correct.

4
Print the total sum
Write a Console.WriteLine statement to display the text Total sum: followed by the value of sum.
C Sharp (C#)
Need a hint?

Use Console.WriteLine with an f-string style to show the sum.