I have a for loop and i want to do different things depending on whether
the counter is even or odd.
I know there is probably some really simple math thing that can do this
but my maths is a bit hazy. I did check the MS C# library site but had
no luck.
Any help appreciated.
--
Eps http://ukcomment.blogspot.com/
A UK political Blog 8 39203
Hi,
You can use System.Math.Div Rem method. It takes numbers as input and
returns quotient and gives remainder as out parameter.
Reshma
"Eps" <ep*@mailinator .com> wrote in message
news:d3******** **@domitilla.ai oe.org... I have a for loop and i want to do different things depending on whether the counter is even or odd.
I know there is probably some really simple math thing that can do this but my maths is a bit hazy. I did check the MS C# library site but had no luck.
Any help appreciated.
-- Eps
http://ukcomment.blogspot.com/
A UK political Blog
The other suggestions for using a modulus operator certainly work, but I've
always done it with a bitwise operation, since any odd number will have the
low order bit set:
if ( (n & 1) == 0 )
Console.WriteLi ne( "Odd" );
else
Console.WriteLi ne( "Even" );
Looked at objectively, the bitwise operation is probably more obscure than
the modulus operation, but to me it has always been one of those common
idioms that every (by which I mean everyone I work with) C programmer has in
common.
"Eps" <ep*@mailinator .com> wrote in message
news:d3******** **@domitilla.ai oe.org... I have a for loop and i want to do different things depending on whether the counter is even or odd.
I know there is probably some really simple math thing that can do this but my maths is a bit hazy. I did check the MS C# library site but had no luck.
Any help appreciated.
-- Eps
http://ukcomment.blogspot.com/
A UK political Blog
"Joe Fromm" <Jo*******@Pear son.com> schrieb im Newsbeitrag
news:ep******** ******@TK2MSFTN GP14.phx.gbl... The other suggestions for using a modulus operator certainly work, but I've always done it with a bitwise operation, since any odd number will have the low order bit set:
if ( (n & 1) == 0 ) Console.WriteLi ne( "Odd" ); else Console.WriteLi ne( "Even" );
Little but fatal error in your code :-)
The number is odd, if the low order bit is set:
Right version:
if ( (n & 1) == 0 )
Console.WriteLi ne( "Even" );
else
Console.WriteLi ne( "Odd" );
Or rather, test for 1 or swap the "Odd" and "Even" :)
if ( (n & 1) == 1 ) // Odd
if ( (n & 1) == 0 ) // Even
On Thu, 14 Apr 2005 08:36:23 -0500, Joe Fromm <Jo*******@Pear son.com>
wrote: The other suggestions for using a modulus operator certainly work, but I've always done it with a bitwise operation, since any odd number will have the low order bit set:
if ( (n & 1) == 0 ) Console.WriteLi ne( "Odd" ); else Console.WriteLi ne( "Even" );
Looked at objectively, the bitwise operation is probably more obscure than the modulus operation, but to me it has always been one of those common idioms that every (by which I mean everyone I work with) C programmer has in common.
"Eps" <ep*@mailinator .com> wrote in message news:d3******** **@domitilla.ai oe.org... I have a for loop and i want to do different things depending on whether the counter is even or odd.
I know there is probably some really simple math thing that can do this but my maths is a bit hazy. I did check the MS C# library site but had no luck.
Any help appreciated.
-- Eps
http://ukcomment.blogspot.com/
A UK political Blog
--
Happy Coding!
Morten Wennevik [C# MVP]
Hmmm... instead of:
for (int i = 0; i < 10; ++i)
{
if (i % 2 ==0 )
Console.Write(" {0} is Even", i);
else
Console.Write(" {0} is Odd", i);
}
how 'bout we try:
for (int i = 0; i < 10; i = i + 2)
{
Console.Write(" {0} is Even", i);
Console.Write(" {0} is Odd", i+1);
}
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Eps" <ep*@mailinator .com> wrote in message
news:d3******** **@domitilla.ai oe.org... I have a for loop and i want to do different things depending on whether the counter is even or odd.
I know there is probably some really simple math thing that can do this but my maths is a bit hazy. I did check the MS C# library site but had no luck.
Any help appreciated.
-- Eps
http://ukcomment.blogspot.com/
A UK political Blog This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mr_burns |
last post by:
hi,
if i have a datebase field like so:
$username = $row; //after SELECT and mysql_query etc.
how do i check what file type it is? i have tried using is_string to
check it but it returns all fields back as strings, even the ones i
know are integers. i would like something that would tell me the data
tyle (integer, varchar, text etc). are there any other ways or am i
|
by: collincm |
last post by:
Hi,
I am trying to optimize a table for inserts. Half of the timeron cost is in
the FK lookup!
These tables for example
CREATE TABLE FOO2(
FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK PRIMARY KEY,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);
|
by: Ed Bitzer |
last post by:
Appreciate some help. Want to continuously check clipboard and if text
present unhide and display in a text. The following code can find nothing
in the clipboard even though I paste information there from Notepad.
\\
Public Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
Dim t As New System.Timers.Timer(2000)
AddHandler t.Elapsed, AddressOf TimerFired
t.Enabled = True
End Sub
|
by: nephish |
last post by:
Hello there,
i need a way to check to see if a certain value can be an integer. I
have looked at is int(), but what is comming out is a string that may
be an integer. i mean, it will be formatted as a string, but i need to
know if it is possible to be expressed as an integer.
like this
var = some var passed to my script
if var can be an integer :
|
by: user |
last post by:
Hi,
Let's say I have 2 dates in the b/m format:
Date 1 and date 2
How do I check whether Date2 is later than Date 1?
Date1. 21-Nov-2006 09:00:00 PM
| |
by: Klaas Vantournhout |
last post by:
Hi,
I have a question, which is just out of interest.
What is the fastest way to do an odd/even check with c++ and if needed
assembler.
Assume n is an unsigned integer like type (unsigned int, unsigned long
int), what is the fastest?
using the modulo operator
|
by: Dan Henry |
last post by:
I need a sanity check. The following is an exchange on
comp.arch.embedded with CBFalconer in a rather long MISRA related
thread. Since my little section of that thread and area of interest
was never really about MISRA, but rather the one's complement
representation of integer constant 0 and now how to zero memory
portably, I am bringing the topic to this group. Try to ignore CBF's
errors in the 1's complement -1 and sign magnitude -1...
|
by: yhlove |
last post by:
Hi
I declared an array as follows:
dim a as integer()
somewhere else in my program I have the following command:
redim preserve a(size)
I want to check the array length like that
if a.length 1 then
|
by: Frinavale |
last post by:
Filtering user input is extremely important for web programming.
If input is left unfiltered users can input malicious code that can cripple your website.
This article will explain how to make sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
|
by: Matthias |
last post by:
Dear newsgroup.
I want to write a template function which accepts either integer or
floating point numbers.
If a certain result is not a whole number and if the template
parameter is an integer, it should return false, but it should work
normally if the parameter is a float.
To illustrate this, I attached a minimal example.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |