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

confused about ? and : in a line of code for lcm

1
I am researching ways to calculate lcm and came across this line and am trying to understand what it is saying, or possibly a more basic way to write it

lcm = (userNum1 > userNum2) ? userNum1 : userNum2;

The code runs fine, but if I don't understand it, I'm not going to be able to comprehend how to use it later.
Mar 26 '17 #1

✓ answered by weaknessforcats

The ?: is the ternary operator from C. It's the same thing as

Expand|Select|Wrap|Line Numbers
  1. if (userNum1 > userNum2)
  2. {
  3.     icm = userNum1;
  4. }
  5. else
  6. {
  7.     icm = userNum2;
  8. }
The theory is that the less code you write the less assembly code will be generated by the compiler. So a ?: will be the same as if/else but will produce less object code than if/else.

Now this was all true in 1968 with an 8-bit word on a machine with 32K memory.

With today's processors you can ignore any benefit obtained by ?:. I wouldn't use it myself.

3 1019
weaknessforcats
9,208 Expert Mod 8TB
The ?: is the ternary operator from C. It's the same thing as

Expand|Select|Wrap|Line Numbers
  1. if (userNum1 > userNum2)
  2. {
  3.     icm = userNum1;
  4. }
  5. else
  6. {
  7.     icm = userNum2;
  8. }
The theory is that the less code you write the less assembly code will be generated by the compiler. So a ?: will be the same as if/else but will produce less object code than if/else.

Now this was all true in 1968 with an 8-bit word on a machine with 32K memory.

With today's processors you can ignore any benefit obtained by ?:. I wouldn't use it myself.
Mar 26 '17 #2
donbock
2,426 Expert 2GB
The ternary operator offers a potential marginal benefit when used like the OP code example; you only type the left-side variable name once so you don't have a chance to accidentally type different names in the two legs of the if-else.
Mar 27 '17 #3
donbock
2,426 Expert 2GB
The ternary operator is one of the goto-constructs in The Obfuscated C contest:
http://scrammed.blogspot.com/2012/04...-its-best.html
Mar 27 '17 #4

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

Similar topics

4
by: STom | last post by:
Here is the code I have: String strCmd = ""; strCmd = strCmd + "CREATE PROCEDURE GetAllModels2" + "\r\n"; strCmd = strCmd + "AS"; strCmd = strCmd + "BEGIN" + "\r\n"; strCmd = strCmd +...
2
by: Fred Armitage | last post by:
I have a fairly complex application written using a vb code-behind dll. However, I'd like to write one particular aspx page utilising in-line code so that it can easily be modified as needed by the...
1
by: redhair | last post by:
In a asp.net page (.aspx) below, I'd like to know the in-line code ("<%Output()%> ) will be executed in which page process lifecycle? thanks. <Script Language="VB" Runat="Server"> Sub...
2
by: Tim | last post by:
Hi, I want to run some command line instructions from within my C# windows forms application. Basically when the application closes I want to logoff the session if it was started remotely. So...
0
by: Jon Paal | last post by:
what would be the command line code to add an image as a resource to a dll ?
1
by: gdarian216 | last post by:
okay I had a code that asked user to input a file name and instead i changed it to take the input from the command line. I now want to output results of some functions to an output file. I have...
3
by: Boki | last post by:
Hi All, We can assign multi object ( ex: textbox )'s double click point to a same function. ex: private void all_boki_textbox_point_here_DoubleClick(object sender, EventArgs e) { ...
2
by: tesa | last post by:
I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any...
1
by: rahullko05 | last post by:
one line code to replace an image without page refresh
1
by: rahullko05 | last post by:
Can anybody help me? to write one line code to render html inside a div element. Thanks.
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
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
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
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
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.