little code in java studies

Java programming topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

little code in java studies

Post by Face » Tue Aug 10, 2010 11:11 pm

Bro I started studding basics again.because I have to learn switch block to correct my code calc I posted.So I made this with the hope of some execution in my mind..See my little code..

Code: Select all

//I am going to write a program to test the for loop in my studies.

class ForLoop{
	public static void main(String[] args){
		int x;
		
		x=1;
		
		for(x=1;x<5;x++);
			System.out.println("value of the X is - "+x);
		
		}
	}
I thought that I will get this kind of execution..

Code: Select all

value of the X is - 1
value of the X is - 2
value of the X is - 3
value of the X is - 4
But I got this line as out put.

Code: Select all

value of the X is - 5
with my idea I wrote this to run the loop for x<5..
1.)According to my idea the loop should stop in 4.right?..Then what happen here.It runs for x=5..
2.)I thought that loop will give me $ line out put.But It gave me one line..What happen there...?

Bros..I am a beginner.I am studding by you & some PFDs & you tube .So I don't know the basics also.I am analyzing others codes & try to study them..That is the way I study.....

Don't think I am a stupied..he he
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: little code in java studies

Post by Face » Tue Aug 10, 2010 11:19 pm

Oh Bro I found the error..yepeeee

Code: Select all

    //I am going to write a program to test the for loop in my studies.

    class ForLoop{
       public static void main(String[] args){
          int x;
          
          x=1;
          
          for(x=1;x<5;x++){
             System.out.println("value of the X is - "+x);
              }
          }
       }
happy happy
Nice out put..he he

Code: Select all

>java -cp . ForLoop
value of the X is - 1
value of the X is - 2
value of the X is - 3
value of the X is - 4
>Exit code: 0
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: little code in java studies

Post by Face » Tue Aug 10, 2010 11:20 pm

SORRY IF I DISTURB ANY ONE FRIENDS..... ;)
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: little code in java studies

Post by Herath » Tue Aug 10, 2010 11:25 pm

Oh. It's good. :D

I just wrote an explanation of the mistake. Glad that you have found the error. Anyway, I will attach my answer too. Just in case you like to have a look. :)

Code: Select all

for(x=1;x<5;x++);
See that semi colon after then for loop declaration. That tells the compiler that the line has ended. Thus, your for loop will have no body. It will just run. all you have to do is removing the semicolon.
And remember, you can't use more than one code line if you do not use curly brackets to group the codes that should be within the for loop.

So the best practice would be,

Code: Select all

      for(x=1;x<5;x++){
         System.out.println("value of the X is - "+x);
}
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: little code in java studies

Post by Face » Tue Aug 10, 2010 11:32 pm

Thank you Bro Herath1..Thank you very much..Christel clear explanation for me..I got the Idea..

I am gaining basics of programming JAVA...your & Neo's help is great..I have no word to thank you...I will post my little questions I have on my studies...
Post Reply

Return to “Java Programming”