Do you get these problems? [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Do you get these problems? [SOLVED]

Post by alexrules01 »

Do you get problems sometimes where a while or if statement won't work? I am currently using a while statement, but one of the conditions are not triggering once the variable is changed

I have:

Code: Select all

while ($winner == 0 || $b < 200)
{
if
{
     if
     {
     $winner++;
     }
}
}
Thats the basic structure of it, hopefully you won't need the rest of the code.
The $b variable is fine, it increments by 1 each loop, so there is 200 loops
The trouble I am having is the while statement doesnt seem to be recognising that $winner has changed. I even print the $winner variable after each loop and it prints 1, then maybe 2 and 3 before it cuts off at 200 (sometimes it goes over 200, like to 205)
Does anyone know what is going on?
Last edited by alexrules01 on Fri Jul 22, 2011 2:27 am, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Do you get these problems?

Post by Jackolantern »

Did you remove the conditions on the two IF statements to post the code here, or is there no conditionals on them? If it is the latter, that is a problem, because IF statements need them.

I know you removed some of the code from these statements, but it may be an issue of your IF statements growing too large to really look at and read well.
The indelible lord of tl;dr
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Do you get these problems?

Post by alexrules01 »

Yea the if statements do have conditions which work fine, I just typed that out in the post, but I'll c+p the code if you need it, I just thought it might be a simple fix
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Do you get these problems?

Post by 62896dude »

yeah, show us the whole code for that section
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Do you get these problems?

Post by alexrules01 »

Code: Select all

$winner = 0;
$b = 0;
while ($winner == 0 || $b < 200)
{
	$i = 0; // Sets array value
	$hpstamina = $stamina + $health;
	$hpstamina2 = $stamina2 + $health2;
	if ($playerturn == $name)
	{
		$pinif = $hpstaminamax2 / 5;
		$randpin = rand(1,100);
		print "RandPin: " . $randpin . "<br>";
		print "HPstaminaMAX: " . $hpstaminamax2 . "<br>";
		print "HPStamina " . $hpstamina2 . "<br>";
		print "PinIf: " . $pinif . "<br>";
		if ($randpin < 50 && $hpstamina2 <= $pinif && $player2position == "Grounded")
		{
			print $name . " goes for a KO<br>";
			$pinattempt = rand(1,100) + $cover - $kickout2;
			if ($pinattempt > 50)
			{
				print "The winner is " . $name . "<br>";
				$winner = 1;
			}
			else
			{
				print $wrestler . "gets back up<br>";
				$player2position = "Opening";
			}
		}
$b++;
This is just a snippet of the code, where the winner is set if there is a winner.
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Do you get these problems?

Post by alexrules01 »

I think I have just realised what is happening.
If a winner is set prior to the 200th loop, the loop continues until its completes 200.

If $winner is still 0 by the 200th loop, it keeps going after 200 until $winner = 1
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Do you get these problems?

Post by Jackolantern »

Correct. With no comments, I don't know all of the logic that goes into this, but OR WHILE loops (i.e. ($this == 0 || $a < 200)) are rare. Here you are saying the loop will run while either $winner == 0 or $b < 200.

From the last post you made, it sounds like maybe you meant to have an AND (i.e. &&) in the middle of the WHILE statement. Then the loop will run for up to 200 turns or until someone wins.

And just as a side note, this kind of logic error is called a "short circuit". Part of your code was not being evaluated due to the logic of the loop.
The indelible lord of tl;dr
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Do you get these problems?

Post by alexrules01 »

Oh yes you are correct Jack! Can't believe I didnt realise that lol. I was thinking of it differently, the and works well!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Do you get these problems?

Post by Jackolantern »

Glad you got it fixed :) Can you mark this topic [resolved] or [solved]?
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”