473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

When does the pointer move to if or else in the given code ?

gautamz07
Expand|Select|Wrap|Line Numbers
  1.         <?php
  2.     $headCount = 0;
  3.     $flipCount = 0;
  4.     do {
  5.         $flip = rand(0,1);
  6.         $flipCount ++;
  7.         if ($flip ){
  8.             $headCount ++;
  9.             echo "<div class=\"coin\">H</div>";
  10.         }
  11.         else {
  12.             $headCount = 0;
  13.             echo "<div class=\"coin\">T</div>";
  14.         }
  15.     }while ($headCount < 3);
  16.     echo "<p>It took {$flipCount} flips!</p>";
  17.  
  18.  
  19.     ?>
  20.  
  21.  
  22.  
When is the if statement executed and when is the else statement executed ?
Apr 14 '14 #1

✓ answered by techboy

rand() generates random value between 0 and 1 inclusive thus
whenever non zero is generated it moves to the if part and else when value of $flip=0.Same way if it goes into if part thrice than your $headcount becomes 3 which fails the do while loop and comes out of it.
Due to rand() you cant decide how many times either of if or else part would get executed!

2 1063
techboy
28
rand() generates random value between 0 and 1 inclusive thus
whenever non zero is generated it moves to the if part and else when value of $flip=0.Same way if it goes into if part thrice than your $headcount becomes 3 which fails the do while loop and comes out of it.
Due to rand() you cant decide how many times either of if or else part would get executed!
Apr 14 '14 #2
Actually Found the answer now ..

When you use a number in a condition, as we did earlier, PHP understands 1 to mean true and 0 to mean false.
Apr 15 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Arun | last post by:
Hi, I've got a managed class , inside which I'm instantiating an unmanaged class. If that class has some virtual methods, then while debugging I couldn't jump into the source of that virtual...
0
by: Max | last post by:
I'm having some problems using SqlDataReader in the Microsoft.ApplicationBlocks.Data.SqlHelper class. In some cases it seems that the cursor does not move, and I'm not sure what is causing this. ...
2
by: mswc.net | last post by:
I have put code inside this method (Global.asax) but not even after re-starting my machine or the aspnet_wp.exe service it seems to execute the code inside of it. When does this methods really...
1
by: the friendly display name | last post by:
I am using .net 1.1 In the global.asax.cs file, there is this entry: protected void Application_AuthenticateRequest(Object sender, EventArgs e) as far as I know, it is wired with the ...
1
by: petermichaux | last post by:
Hi, setTimeout(code, delay) When the delay of a setTimeout expires it is time to execute the code. If the browser is executing some other JavaScript will the setTimeout code interupt or wait...
0
by: codejunkie | last post by:
Hello, I have a C# program that displays related intellisence (list box) every time i type in the rich text editor of the form. Unfortunately, the intellisense window does not move with the main...
1
by: Slain | last post by:
I am a new hire in a company and have started working for the development team. Ofcourse, I took some C++ courses in my grad school, but there is such a big difference in the programs you encounter...
2
by: Eng Teng | last post by:
Where is the part to set when my mouse pointer move to textbox or label or buton it will display text description ? Regards, Tee
7
by: Andrew Poulos | last post by:
If I have code that looks like this ajax = function(str) { var val = ""; // do some stuff here return val; }; var foo = ajax("string");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.