473,408 Members | 1,968 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,408 software developers and data experts.

How to use ? and : in a program

Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement? Any
explanation will be appreciated.

<?
$x=32;
$y=80;
function gcd($a, $b)
{ return ($b>0)?gcd($b,$a%$b):$a;}
$val=gcd($x, $y);
print "The greatest common denominator of $x and $y is $val";
?>

This recursive program is used to find the greatest common denominator of 2
numbers.

TIA
Oct 11 '05 #1
9 1557
Michael Jack wrote:
Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement?
Any explanation will be appreciated.
Hi Michael,

It is just a shorthand notation for an if-then-else statement

eg:
$a = 10;
echo ( ($a<20) ? "$a is smaller then 20!" : "$a is not smaller then 20!");

is the same as:
$a = 10;
if ($a<20) {
echo "$a is smaller then 20!";
} else {
echo "$a is not smaller then 20!";
}

Regards,
Erwin Moller


<?
$x=32;
$y=80;
function gcd($a, $b)
{ return ($b>0)?gcd($b,$a%$b):$a;}
$val=gcd($x, $y);
print "The greatest common denominator of $x and $y is $val";
?>

This recursive program is used to find the greatest common denominator of
2 numbers.

TIA


Oct 11 '05 #2
Many thanks.

"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
Michael Jack wrote:
Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement?
Any explanation will be appreciated.


Hi Michael,

It is just a shorthand notation for an if-then-else statement

eg:
$a = 10;
echo ( ($a<20) ? "$a is smaller then 20!" : "$a is not smaller then 20!");

is the same as:
$a = 10;
if ($a<20) {
echo "$a is smaller then 20!";
} else {
echo "$a is not smaller then 20!";
}

Regards,
Erwin Moller


<?
$x=32;
$y=80;
function gcd($a, $b)
{ return ($b>0)?gcd($b,$a%$b):$a;}
$val=gcd($x, $y);
print "The greatest common denominator of $x and $y is $val";
?>

This recursive program is used to find the greatest common denominator of
2 numbers.

TIA

Oct 11 '05 #3
Erwin Moller said the following on 11/10/2005 10:05:
Michael Jack wrote:

Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement?
Any explanation will be appreciated.

Hi Michael,

It is just a shorthand notation for an if-then-else statement


Not quite. You couldn't use it for something like:

if (6 == $a)
{
for ($i = 0; $i < $a; ++i)
{
...
}
}
else
{
// Some other code
}
It's really a conditional assignment operator.

--
Oli
Oct 11 '05 #4
Many thanks for the prompt reply.

Regards,

"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
Michael Jack wrote:
Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement?
Any explanation will be appreciated.


Hi Michael,

It is just a shorthand notation for an if-then-else statement

eg:
$a = 10;
echo ( ($a<20) ? "$a is smaller then 20!" : "$a is not smaller then 20!");

is the same as:
$a = 10;
if ($a<20) {
echo "$a is smaller then 20!";
} else {
echo "$a is not smaller then 20!";
}

Regards,
Erwin Moller


<?
$x=32;
$y=80;
function gcd($a, $b)
{ return ($b>0)?gcd($b,$a%$b):$a;}
$val=gcd($x, $y);
print "The greatest common denominator of $x and $y is $val";
?>

This recursive program is used to find the greatest common denominator of
2 numbers.

TIA

Oct 11 '05 #5
"Michael Jack" <mi*****@jack2005.com> writes:
Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement? Any
explanation will be appreciated.

<?
$x=32;
$y=80;
function gcd($a, $b)
{ return ($b>0)?gcd($b,$a%$b):$a;}
$val=gcd($x, $y);
print "The greatest common denominator of $x and $y is $val";


You have stumbled on the ternary operator;

But the real reason for this is to obfuscate the program and confuse
everyone who reads it!

No kidding. I quit using ?: years ago and prefer the more verbose
if/then/else. Others may disagree.

HTH

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Oct 11 '05 #6
Oli Filth wrote:
Erwin Moller said the following on 11/10/2005 10:05:
Michael Jack wrote:

Hi,

in the program below, what is the meaning/use of
the question mark (?gcd) and the colon (:$a) after the return statement?
Any explanation will be appreciated.

Hi Michael,

It is just a shorthand notation for an if-then-else statement


Not quite. You couldn't use it for something like:

if (6 == $a)
{
for ($i = 0; $i < $a; ++i)
{
...
}
}
else
{
// Some other code
}
It's really a conditional assignment operator.


True.
:-)

Regards,
Erwin Moller

Oct 11 '05 #7
In article <xY******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:
It's really a conditional assignment operator.


But it isn't an assignment operator.

--
http://yosemitecampsites.com/
Oct 11 '05 #8
el***@no.spam said the following on 11/10/2005 18:34:
In article <xY******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:

It's really a conditional assignment operator.

But it isn't an assignment operator.


Good point.
--
Oli
Oct 11 '05 #9
Oli Filth wrote:
el***@no.spam said the following on 11/10/2005 18:34:
In article <xY******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:

It's really a conditional assignment operator.

But it isn't an assignment operator.


Good point.


Slowly but surely we are getting there!
;-)

Regards,
Erwin Moller
Oct 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.