473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a for loop to determine maximum value of an int variable

I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{

j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)

}
}
}
}

q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?

q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?

q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?

Thankyou,

Gary.

Dec 5 '06 #1
29 5082
I'm trying to investigate the maximum size of different variable
types. I'm using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.
Actually, that's the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.
But i'm trying to write a program to test this, assuming I didn't know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?

using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?
sure. i += 10000
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
If you're in .NET 2.0, try the System.Diagnost ics.Stopwatch class.
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
Well, you shouldn't actually need to do this. It'll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:

static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 5 '06 #2

ga********@mywa y.com wrote:
I'm trying to investigate the maximum size of different variable types.
If you're trying to figure out which data types are best for storing
certain data, try this reference table:
http://msdn2.microsoft.com/en-us/library/exx3b86w.aspx

Dec 5 '06 #3
Thankyou very much Dustin your answers have answerd 90% of my questions
and have really helped me alot.

Although changing the for interation from 1, to a higher number works
for this test it doesn't really answer my for loop question, which was
probabally because I didn't put my question right in the first
instance! I was intrigued about the question when it came to me in this
example and still dont really know the answer.

My First Question is: -

With a for loop like so: -

for(i=0; i<9999999999; i++)
{

}

how can I do an action every nth interations of the loop? so for
instance everytime the loop has executed 10,000 times do something?

Also another question has come to me now as I have worked through your
answers.
My code is now displaying the value of of the integer variable to
screen and I'm not having to wait as long as i am incrementing it's
value by 500,000 every iteration. However. When the value gets to:
2145,000,000 all future increments don't generate an error.
Instead each future increment is displayed as: -2147483648.

I'm just wondering why an error isn't generated. And why the value
becomes negative, with a numerical value one greater than the actual
numerical value that the variable is permitted to hold?

Thanks!

Gary-

Dustin Campbell wrote:
I'm trying to investigate the maximum size of different variable
types. I'm using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.

Actually, that's the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.
But i'm trying to write a program to test this, assuming I didn't know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?

using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{
j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?

sure. i += 10000
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?

If you're in .NET 2.0, try the System.Diagnost ics.Stopwatch class.
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?

Well, you shouldn't actually need to do this. It'll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:

static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 5 '06 #4
mc
how about: -

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne("Int32 Max= "+Int32.MaxValu e.ToString());
}
}
}

I know it's a bit tongue and cheek, but surely this is all you trying to
achieve?

The MaxValue and MinValue constants are available for all sorts of
different variable types.

MC

ga********@mywa y.com wrote:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{

j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)

}
}
}
}

q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?

q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?

q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?

Thankyou,

Gary.
Dec 5 '06 #5
ga********@mywa y.com wrote:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i'm
trying to write a program to test this, assuming I didn't know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 999999999999999 9999; i++)
{

j = (int)i;
Console.WriteLi ne(j.ToString() );
///on error console.write(t here was an error... this
was the error code)

}
}
}
}
To start with, System.Int16, Int32 and Int64 all have a static MaxValue
property. Accessing that is much faster than looping until an exception is
thrown.
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?
if (j%10000 == 0)Console.Write Line(j.ToString ());
q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?
As Dustin responded, System.Diagnost ics.Stopwatch should help. If not .NET
2.0, you could create a DateTime object at the start of the loop, another at
the end, set both values to the static DateTime.Now and subract end from
start once the loop is finished. Ex:

DateTime start = DateTime.Now;
for ()
{
....
}
Console.WriteLi ne (DateTime.Now - start);
q.3
I'd like to output an error message to the screen when the variable
crashes because it's too small to hold the number being allocated to
it. How do i do this?
The problem here is it won't crash. if you try to cast a value that is too
large for an Int32 into an Int32, it simply loops. Try the following in
code:

double d = double.MaxValue
int x = (int)d;

This will not cause an exception and will instead set the value of x
to -2147483648. So you would need to check for a negative number and then
behave accordingly. Ex:

if (j < 0)
{
Console.WriteLi ne ("Max value is " + (i - 1));
break;
}

But again, System.Int32.Ma xValue is the better way to get this value.
--
Tom Porterfield

Dec 5 '06 #6
Thankyou very much Dustin your answers have answerd 90% of my
questions and have really helped me alot.

Although changing the for interation from 1, to a higher number works
for this test it doesn't really answer my for loop question, which was
probabally because I didn't put my question right in the first
instance! I was intrigued about the question when it came to me in
this example and still dont really know the answer.

My First Question is: -

With a for loop like so: -

for(i=0; i<9999999999; i++)
{
}

how can I do an action every nth interations of the loop? so for
instance everytime the loop has executed 10,000 times do something?
Use modular arithmetic like this:

for(i=0; i<9999999999; i++)
{
if (i % 10000 == 0)
// do something...
}
Also another question has come to me now as I have worked through your
answers.
My code is now displaying the value of of the integer variable to
screen and I'm not having to wait as long as i am incrementing it's
value by 500,000 every iteration. However. When the value gets to:
2145,000,000 all future increments don't generate an error.
Instead each future increment is displayed as: -2147483648.
I'm just wondering why an error isn't generated. And why the value
becomes negative, with a numerical value one greater than the actual
numerical value that the variable is permitted to hold?
See Tom's excellent post on this thread.

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 5 '06 #7
Thankyou these are exactly the answers I was looking for. I have two
final questions on the subject maybe someone would be kind enough to
answer them for me.

1.
Why isn't an error generated? Is this a floor with the .net error
checking?

2.
When I experiment with a uint instead of an int, and increment it by
500,000 at a time, i notice the value outputted to the console doesn't
steadily increase, but increases, then decreases, then increases etc...
why is this? what is going on?

Thanks,

Gary-

Dec 5 '06 #8
how can I do an action every nth interations of the loop?

for(...) {
if(i % 10000==0) {
DoSomethingInte resting();
}
}
// perhaps DoSomethingInte resting() here if you always
// want it to fire at the end even if partly through a block

Marc
Dec 5 '06 #9
Thankyou these are exactly the answers I was looking for. I have two
final questions on the subject maybe someone would be kind enough to
answer them for me.

1.
Why isn't an error generated? Is this a floor with the .net error
checking?
Try wrapping the expression with the "checked" keyword. This will cause the
runtime to throw an OverflowExcepti on.

j = checked((int)i) ;
2.
When I experiment with a uint instead of an int, and increment it by
500,000 at a time, i notice the value outputted to the console doesn't
steadily increase, but increases, then decreases, then increases
etc...
why is this? what is going on?
The uint variable is overflowing and then being set to 0 + the overflow amount.
For example:

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 5 '06 #10

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

Similar topics

2
4873
by: Nathan Sokalski | last post by:
I want to determine the operating system using ASP (or VBScript inside of ASP). I tried to get it using the Request. ServerVariables(EnvironmentVariable) method. On the web page about ASP in which I found this method, it gave a list of possible values for the EnvironmentVariable parameter. One of these, which is the one that I tried to use, was HTTP_UA_OS. However, this value did not seem to work, and when I displayed a list of possible...
11
6582
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
0
3924
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
18
4334
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
3
3676
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use by another process. This occurs even if I release the object using fs.Close() and fs = Nothing. Please help (my code is given below) Best regards
6
17160
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
73
4585
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
13
12783
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new scripting language that makes writing automated tests simpler. Really, I'm looking to kind of abstract the power of the C# language into a simpler language that's easier to learn. The script files would be interpreted by a script interpreter...
1
1905
by: jombloboy | last post by:
I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this: double num1 = 1; double num2 = 2; double fib = 0; { while ( fib >= 0 ) { fib = num1 + num2;
0
8063
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, 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...
0
8496
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, 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...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
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,...
1
6013
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 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...
0
5475
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4024
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.