Tuesday, January 27, 2009

Friday, Jan 30th , Fun with for loops: part the First

Last semester I showed you how to create a for loop in Visual Basic. It works in a pretty similar fashion in JAVA: you can use it to do the same action over and over, no matter what that action might be. Here is an example of how this works:




public class ForPrint

{

public static void main( String args[ ] )

{

for (int i=1; i < 100; i++)
{

System.out.println( "Hello World!" );

}

//end for

}//end main
}//end class ForPrint






You will notice that we are already starting to use a lot of "{" and "}" This can get confusing, ESPECIALLY if you simply type through a program from top to bottom. Here are two techniques that can help you with this right from the get go:

A) always type in your curly braces FIRST, then go back and type in the code that goes between them



B) If you get confused as to which "{" goes with which "}", then you can use this trick in TextPad --> highlight a curly brace, then use the Ctrl + M keyboard shortcut. It will go to the other curly brace that is associated with the one you just used. If it doesn't go to the curly brace you thought it was supposed to go to, you've messed up somewhere, and you either have too many of them or too few.



Cheers,

Uncle Paulie

No comments:

Post a Comment