Variables and Arithmetic Expressions

We'll begin this section by finding a more elegant way to handle multiple command line arguments of an undetermined number. Toward this end we introduce the concept of a loop. A loop is a section of code that is executed repeatedly until a stopping condition is met. A typical loop may look like:

while there's more data { Read a Line of Data Do Something with the Data } This isn't working code but it does give you an idea of a very typical loop. We have a test condition (Is there more data?) and something we want to do with if the condition is met. (Read a Line of Data and Do Something with the Data.)

There are many different kinds of loops in Java including while , for , and do while loops. They differ primarily in the stopping conditions used.

For loops typically iterate a fixed number of times and then exit. While loops iterate continuously until a particular condition is met. You usually do not know in advance how many times a while loop will loop.

In this case we want to write a loop that will print each of the command line arguments in succession, starting with the first one. We don't know in advance how many arguments there will be, but we can easily find this out before the loop starts using the args.length . Therefore we will write this with a for loop. Here's the code:

// This is the Hello program in Java class Hello { public static void main (String args[]) { int i; /* Now let's say hello */ System.out.print("Hello ); for (i=0; i < args.length; i = i+1) { System.out.print(args[i]); System.out.print(); } System.out.println(); } } We begin the code by declaring our variables. In this case we have exactly one variable, the integer i .

Then we begin the program by saying "Hellojust like before.

Next comes the for loop. The loop begins by initializing the counter variable i to be zero. This happens exactly once at the beginning of the loop. Programming tradition that dates back to Fortran insists that loop indices be named i , j , k , l , m and n in that order. This is purely a convention and not a feature of the Java language. However anyone who reads your code will expect you to follow this convention. If you choose to violate the convention, try to give your loop variables mnemonic names like counter or loop_index .

Next is the test condition. In this case we test that i is less than the number of arguments. When i becomes equal to the number of arguments, ( args.length ) we exit the loop and go to the first statement after the loop's closing brace. You might think that we should test for i being less than or equal to the number of arguments; but remember that we began counting at zero, not one.

Finally we have the increment step, i=i+1 . This is executed at the end of each iteration of the loop. Without this we'd continue to loop forever since i would always be less than args.length . (unless, of course, args.length were less than or equal to zero. When would this happen?).

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india