While () and do...while() loop

The while() and do…while() iteration statements.
These statements operate same way that for() statements operate. The main difference is that a for() statement is best used when there are a particular number of iterations needed through a code block, but while() and do…while() are the best when a single expression needs to be evaluated and continuing the loop so long as it evaluates as true.


While() loop syntax
While(true)
{
// continue doing something while the above expression is true.
}

do...while() loop syntax:

do
{
// do something atleast once.
// while the expression below is true.
}
While(true);

Let's us consider ToluVsMoniGame: Here I will show how both while(),and do while() loops could be use.

Game purpose:
1.This should allow users inputs.
2.Substract user input A from random number.
3.Store user inputs in as a string.
4.Check if input A and B after some evaluations is still greater than 0, if yes do some more evaluations.
5.Set up the battle logic.

Set up the programmatic ID:
1.toluTextbox
2.moniTextbox
3.toluCheckbox
4.moniCheckbox
5.okButton
6.resultLabel

Code: ToluVsMoniGame:


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ToluVsMoniGame
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void okButton_Click(object sender, EventArgs e)
{
if (toluCheckBox.Checked && moniCheckBox.Checked)
{
int totalwinner = 0;
string result = "";
Random random = new Random();
int toluHealth = int.Parse(toluTextBox.Text);
int moniHealth = int.Parse(moniTextBox.Text);
// Tolu get first bonus
moniHealth -= random.Next(1, 100);
int round = 0;
result += "Round:" + round;
result += string.Format("<br/>Tolu got first attacks, leaving ,Moni with Health{0}", moniHealth);
//Battle logics here
do
{
int toluDamage = random.Next(1, 10);
int moniDamage = random.Next(1, 20);
toluHealth -= moniDamage;
moniHealth -= toluDamage;
result += "<br/>Round:" + ++round; // Try ++round and round++
result += string.Format("<br/> Tolu causes {0} damage, leaving moni with {1} Health.", toluDamage, moniHealth);
result += string.Format("<br/> moni causes {0} damage, leaving Tolu with {1} Health.", moniDamage, toluHealth);
}
while (toluHealth > 0 && moniHealth > 0);
if(toluHealth > 0)
{
result += "<br/> Tolu wins!";
}
else
{
result+= "<br/> Moni wins!";
}
resultLabel.Text = string.Format( "{0} <br/> Total win: {1}", result, totalwinner);
}
else
{
resultLabel.Text = "Please check the boxes";
}
}
}
}
Run the program:
https://www.maxybyte.com/2017/08/while-and-dowhile-loop.html
ToluMoni A

Now change the do..... while() expression to while() and run the program, you can as well set a break point and watch the flow at debug mode:

while (toluHealth > 0 && moniHealth > 0)
{
int toluDamage = random.Next(1, 10);
int moniDamage = random.Next(1, 20);
toluHealth -= moniDamage;
moniHealth -= toluDamage;
result += "<br/>Round:" + ++round; // Try ++round and round++
result += string.Format("<br/> Tolu causes {0} damage, leaving moni with {1} Health.", toluDamage, moniHealth);
result += string.Format("<br/> moni causes {0} damage, leaving Tolu with {1} Health.", moniDamage, toluHealth);
}

Here one major thing that will happen is that if moniHealth came up as negative once this line of code "moniHealth -= random.Next(1, 100);"
is executed, then our while() loop block of code would not be executed at all but reverse is the case with do.....while() loop.
This actually makes the do....while() loop suitable for this application. Because even at moniHealth negativity we can still do some evaluations
inside the do{} block before we call while(true) the condition is true, continue doing it until the condition is false. In this case
while (toluHealth > 0 && moniHealth > 0) continue with the loop else break out from the loop.

Note: while (this thing here is true keep doing it until it turns false ) is different from if (this thing here is true do it once and go ahead);
for instance:

while (toluHealth > 0 && moniHealth > 0) will not give same result as if (toluHealth > 0 && moniHealth > 0).


2 comments:

Note: only a member of this blog may post a comment.