blah blah blah is here! blah blah » Close

up0down
link

Have a problem to understand for command.
Have a example here
First what is line 1, why those ,
What about line 2. so confusing to me.
Thanks
1. int i, somme, n=10;
2. for (i = 1, somme = 0; i <= n; i = i + 1)
3. somme = somme + i;
4.
5. for (i = 1, somme = 0; i <= n; somme = somme + i, i = i + 1) ;
6.
7. i = 1; somme = 0;
8. while (i <= n) { somme += i; i++; }
9.
10. i = 1; somme = 0;
11. do somme += i++;
12. while (i <= n);

last answered 2 years ago

1 answers

up0down
link

in line 1 you are defining an integer i and somme of default value 0 and defining an integer n of value 10.
in the loop you are starting with i=1 and somme=0. then looping from i 1->10 while adding the numbers from 1->10 to the somme integer which in other words gives you 1+2+3+..+10.

up0down
link

Just one clarification on this.
Unlike VB.Net, C# doesn't have default values for local variables. If you don't give them a value when you declare them, then they're simply 'unassigned' and the compiler will therefore complain if you try to access their value before they have been assigned one.
So, in the above code, 'i' and 'somme' don't have a default value of 0 but are left unassigned until the 'for' statement executes when they're then assigned values of 1 and 0, respectively.

up0down
link

yes, i mixed things with vb, i apologize for the wrong info concerning the default value.

up0down
link

not true. primitives & value types do have default values. int's default value is 0. bool's default value is false.
public struct Foo {
public string A;
public int B;
}
you can do:
public void SomeMethod() {
Foo f;
f.A = "hello world";
f.B = 88;
}
is totally legal, will compile and run.

up0down
link

to further clarify, reference types do not have default values because they are managed pointers. a null pointer does not point to anything and can be "null" as it is in C#, but value types do not live in the managed heap, and must have default values (they are not pointers). therefore if I allocate a variable of type int, it gets zero'd out before you are given access to it, and therefore has a default value of 0. same holds true for boolean (all other numeric primitives follow the same technique as int).

up0down
link

I'm talking here about local variables not having default variables.
Fields, of course, do have default values (all bytes set to \0).
If you try the following:
using System;
public struct Foo {
public string A;
public int B;
}
class Test
{
static void Main()
{
Test t = new Test();
t.SomeMethod();
Console.ReadKey();
}
public void SomeMethod() {
Foo f;
Console.WriteLine(f.A);
Console.WriteLine(f.B);
}
}
then it doesn't compile:
error CS0170: use of possibly unassigned field 'A'
error CS0170: use of possibly unassigned field 'B'
If you change:
Foo f;
to:

Foo f = new Foo();
then it compiles and runs fine, printing out an empty line and then 0 because null and 0 are the default values of the struct's fields.

up0down
link

Sorry, first line should read:
I'm talking here about local variables not having default <b>values</b>.

up0down
link

regardless of scope, value types will always have default values.
same with reference types, regardless of scope, they will by default be null.
but maybe I'm missing what's been being said due to my allergies to vb ;)

up0down
link

I'm not keen on VB either but if you use both then it's something you have to watch. For example, this VB program compiles and works fine, printing 0 to the Console:
Option Explicit
Imports System
Module Test
Sub Main()
Dim i As Integer
Console.WriteLine(i)
Console.ReadKey()
End Sub
End Module
The equivalent program in C#:
using System;
class Test
{
static void Main()
{
int i;
Console.WriteLine(i);
Console.ReadKey();
}
}
fails to compile with a 'use of unassigned local variable' error.
Although the CLR always zeros out memory when allocating a new object on the heap, I think I'm right in saying that it doesn't zero out local variable space when a new stack frame is created.
Behind the scenes, the VB compiler therefore initializes unassigned local variables to their default values but the C# compiler doesn't.

up0down
link

according the the ecma-334 spec:
12.2 Default values
The following categories of variables are automatically initialized to their default values:
• Static variables
• Instance variables of class instances
• Array elements
The default value of a variable depends on the type of the variable and is determined as follows:
121
C# LANGUAGE SPECIFICATION
• For a variable of a value-type, the default value is the same as the value computed by the value-type’s
default constructor (§11.1.2).
• For a variable of a reference-type, the default value is null.
[Note: Initialization to default values is typically done by having the memory manager or garbage collector
initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bitszero
to represent the null reference. end note]
The default value of a nullable type is an instance for which the HasValue property is false. Referencing the
Value property of a default value of a nullable type results in an exception of type
System.InvalidOperationException. The default value is also known as the null value of the
nullable type. An implicit conversion exists from the null type (§11.2.7) to any nullable type, and this
conversion produces the null value of the type.
in the scenario you've shown the compiler won't let you access the variable before it's been initialized (as a general rule), but use reflection to access it and determine whether it has a value, and it will.

up0down
link

But that doesn't cover local variables. It only covers static and instance fields and array elements.
If you check out <a target="_new" href="http://www.csharpfriends.com/Spec/index.aspx?specID=12.1.7.htm">section 12.1.7</a> (3rd para down) of the C# spec on this page, then it says:
"A local variable is not automatically initialized and thus has no default value"
Now, you can't access a local variable using reflection but, if you could, then <i>I think</i> you'd find that it wouldn't necessarily contain the the default value for that type but garbage because I don't think local variable storage is automatically zeroed.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback