Click here to Skip to main content
15,905,144 members
Home / Discussions / C#
   

C#

 
AnswerRe: OOPS, deisgn patterns and principles Pin
GuyThiebaut1-Oct-13 21:47
professionalGuyThiebaut1-Oct-13 21:47 
GeneralRe: OOPS, deisgn patterns and principles Pin
Keith Barrow2-Oct-13 0:32
professionalKeith Barrow2-Oct-13 0:32 
QuestionArrays Pin
Sta_Horse30-Sep-13 23:35
Sta_Horse30-Sep-13 23:35 
AnswerRe: Arrays Pin
Garth J Lancaster30-Sep-13 23:50
professionalGarth J Lancaster30-Sep-13 23:50 
GeneralRe: Arrays Pin
harold aptroot1-Oct-13 1:47
harold aptroot1-Oct-13 1:47 
GeneralRe: Arrays Pin
Garth J Lancaster1-Oct-13 2:10
professionalGarth J Lancaster1-Oct-13 2:10 
AnswerRe: Arrays Pin
Pete O'Hanlon30-Sep-13 23:52
mvePete O'Hanlon30-Sep-13 23:52 
AnswerRe: Arrays Pin
OriginalGriff30-Sep-13 23:52
mveOriginalGriff30-Sep-13 23:52 
You presumably know how a for loop works - but I'll recap just in case:
C#
for (init; test; alter)

Where:
init sets up the initial conditions - in your case, it sets i or j to zero.
test checks to see if the loop should continue - if test is true, it will.
alter allows you to modify the conditions so the the test will eventually become false.

So in your first loop, the value of i changes by one each time (in the second loop, the same thing happens to j) so each time the loop executes, it accesses a different element of teh array, and sets it to a different value.

For the second loop, it outputs each element in sequence.
There is no particular reason why the person who wrote that used separate variables i and j for the two loops - probably just for clarity - they could be the same variable without affecting the rest of the code.

Personally, I would change it slightly and declare the loop guard variable inside the loop:
C#
static void Main(string[] args)
    {
    int[] n = new int[10];
    //initialize elements of array n
    for (int i = 0; i < 10; i++)
        {
        n[i] = i + 100;
        }

    // output each array element's value
    for (int j = 0; j < 10; j++)
        {
        Console.WriteLine("Element [{0}] = {1}", j, n[j]);
        }
    Console.ReadKey();
    }
As this makes it clearer that i and j are only relevant (or indeed available) inside the loop. Again, the name i could be used for both loops:
C#
static void Main(string[] args)
    {
    int[] n = new int[10];
    //initialize elements of array n
    for (int i = 0; i < 10; i++)
        {
        n[i] = i + 100;
        }

    // output each array element's value
    for (int i = 0; i < 10; i++)
        {
        Console.WriteLine("Element [{0}] = {1}", i, n[i]);
        }
    Console.ReadKey();
    }

The only instant messaging I do involves my middle finger.

AnswerRe: Arrays Pin
V.1-Oct-13 2:22
professionalV.1-Oct-13 2:22 
QuestionHow can i do a calculation in datagridview Pin
ganesh89_babu30-Sep-13 20:40
professionalganesh89_babu30-Sep-13 20:40 
AnswerRe: How can i do a calculation in datagridview Pin
Abhinav S30-Sep-13 22:35
Abhinav S30-Sep-13 22:35 
QuestionHow to extract links from a div Pin
faiza.butt8930-Sep-13 14:53
faiza.butt8930-Sep-13 14:53 
GeneralRe: How to extract links from a div Pin
PIEBALDconsult30-Sep-13 15:02
mvePIEBALDconsult30-Sep-13 15:02 
AnswerRe: How to extract links from a div Pin
Abhinav S30-Sep-13 17:15
Abhinav S30-Sep-13 17:15 
GeneralRe: How to extract links from a div Pin
faiza.butt891-Oct-13 4:16
faiza.butt891-Oct-13 4:16 
AnswerRe: How to extract links from a div Pin
Akii Malam1-Oct-13 1:32
Akii Malam1-Oct-13 1:32 
AnswerRe: How to extract links from a div Pin
faiza.butt891-Oct-13 4:20
faiza.butt891-Oct-13 4:20 
QuestionHow to get url Pin
DollieBSP30-Sep-13 11:50
DollieBSP30-Sep-13 11:50 
AnswerRe: How to get url Pin
Ravi Bhavnani30-Sep-13 12:18
professionalRavi Bhavnani30-Sep-13 12:18 
AnswerRe: How to get url Pin
Ravi Bhavnani30-Sep-13 12:21
professionalRavi Bhavnani30-Sep-13 12:21 
GeneralRe: How to get url Pin
DollieBSP30-Sep-13 12:26
DollieBSP30-Sep-13 12:26 
GeneralRe: How to get url Pin
Mycroft Holmes30-Sep-13 12:47
professionalMycroft Holmes30-Sep-13 12:47 
GeneralRe: How to get url Pin
Ravi Bhavnani30-Sep-13 15:09
professionalRavi Bhavnani30-Sep-13 15:09 
QuestionI am getting an error when trying to save an XLS to .DBF file. Pin
Glen Childs30-Sep-13 5:48
Glen Childs30-Sep-13 5:48 
AnswerRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Richard Deeming30-Sep-13 5:59
mveRichard Deeming30-Sep-13 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.