This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes for loop logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop logic" Watch "for loop logic" New topic
Author

for loop logic

Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
hi there .. am trying to print table of 2 .. below is my program


public class Statements


{
public static void main (String [] args) {

int a,c;
int b=2;
{
for (a=1;a<10;a++)

c = b * a;


System.out.println("table of 2: " + c);

}

}

}

this is the output am getting -

-------------

table of 2: 2
table of 2: 4
table of 2: 6
table of 2: 8
table of 2: 10
table of 2: 12
table of 2: 14
table of 2: 16
table of 2: 18

can some one point the mistake in logic ?!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What specific problem are you having?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Abid Ramay wrote:this is the output am getting -

Your real program must be different than what you posted here, because when I try to compile your code above, I get a compiler error: the local variable c might not have been initialized. PostRealCode

I suspect your real program has an extra set of { } and looks like this:

The logic is really simple, and it's very easy to see that you get the output that you say you get.

You seem to think there is something not right with the output, but you didn't say what you expected, and how the actual output is different from what you expected. If you want more help, you have to at least explain what you expected.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kenneth Van Gysegem
Ranch Hand

Joined: Mar 30, 2013
Posts: 33
Hi,
Maybe you want your table of 2 to go until 2*10 = 20?

In that case the argument in the for-loop, a<10, is a bit wrong, because 10 is not smaller than 10, if you get what i mean.
The count goes from 1 until 9.
If you want it to go until 10, it should be a<11 or a<=10
as in

I hope this helped,
Kenneth
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 133

You will get that output because in the for-loop you used local variable(or something like that) named 'a'.
so when for-loop executes it performs this expression c=2*a; this a is the local variable not 'a' in main method.
following code will explain


when
a = 1, c = 2
a = 2, c = 4
..................
..................
..................
a=9, c = 18

B and a isn't the SAME

Special thanks to Jesper de Jong for setting up code


Are you better than me? Then please show me my mistakes..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Supun Lakshan Dissanayake wrote:



Umm...this code doesn't actually compile, so it's not clear what you were trying to do. In the OP's code, there is only one variable named 'a' - it is not local to the for-loop.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
Hi Every one .. thanks for your help .. i'll try to answer each question .

1) @Jeff Verdegan -> i'll go through this -> UseCodeTags(←click), it's my first time here ..
2) @Jesper de Jong & Jeff Verdegan -> code is exactly what i've .. am using text editor not IDE ..

and it's very weired .. now when i re - compile it .. am getting the same error ->I get a compiler error: the local variable c might not have been initialized.

as far as i know , i can declare two variabes on the same line
int a,c;

let me know if i am wrong ?!!
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
@Kenneth Van Gysegem -> i was not really worried about the last part .. table of 2 goes like this

2 x 1 = 2
2 x 2 = 4
...
...
...
2 x 10 = 20 ->

@Supun Lakshan Dissanayake -> am not sure if i understand your logic, but will definetly give it a try : what i was trying was

int b = 2 (so it remains same)
ran a loop (1-10) to generate numbers
c= a*b; -> would store the result in c and then print it ;

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Abid Ramay wrote:
and it's very weired .. now when i re - compile it .. am getting the same error ->I get a compiler error: the local variable c might not have been initialized.


If you provide the current version of the code, with code tags, and indicate clearly which line is causing the problem, somebody can help you fix it. That error means exactly what it says though--it's possible to get to that line where you're trying to read a value from c but without having first written a value to it.

as far as i know , i can declare two variabes on the same line


Yes, you can, but you shouldn't. It's a bad practice. And whether you do or don't, it has nothing to do with the error you're getting.
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
@fred rosenberger -> thanks for trying it out
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
Jeff Verdegan wrote:
Abid Ramay wrote:
and it's very weired .. now when i re - compile it .. am getting the same error ->I get a compiler error: the local variable c might not have been initialized.


If you provide the current version of the code, with code tags, and indicate clearly which line is causing the problem, somebody can help you fix it. That error means exactly what it says though--it's possible to get to that line where you're trying to read a value from c but without having first written a value to it.


a->

just to reduce the confusion, i've changed int b= 3; -> now table of three , same logic.
Yes, you can, but you shouldn't. It's a bad practice. And whether you do or don't, it has nothing to do with the error you're getting.


ok - (bad practice) , so we don't know what the compile error is ?!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Abid Ramay wrote:


The compiler doesn't know that c = b * a; will ever be executed. It doesn't work through your loop logic. That happens at runtime. A loop can be executed any number of times, including 0 or infinity. If it's executed 0 times, c will never be set.

Also note that, since you have no braces around the loop body, said loop body consists only of the c = b * a; line. The println() line will only be executed once.

In the future, in addition to using code tags, please use proper indentation in your code.

Yes, you can, but you shouldn't. It's a bad practice. And whether you do or don't, it has nothing to do with the error you're getting.


ok - (bad practice) , so we don't know what the compile error is ?!!


Yes, we do know where the error is.

The error has nothing to do with the bad practice and nothing to do with whether you declare multiple variables on one line. It's bad practice simply because it makes your code harder to read.
Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
Jeff Verdegan wrote:
Abid Ramay wrote:


The compiler doesn't know that c = b * a; will ever be executed. It doesn't work through your loop logic. That happens at runtime. A loop can be executed any number of times, including 0 or infinity. If it's executed 0 times, c will never be set.

Also note that, since you have no braces around the loop body, said loop body consists only of the c = b * a; line. The println() line will only be executed once.

In the future, in addition to using code tags, please use proper indentation in your code.

Yes, you can, but you shouldn't. It's a bad practice. And whether you do or don't, it has nothing to do with the error you're getting.


ok - (bad practice) , so we don't know what the compile error is ?!!


Yes, we do know where the error is.

The error has nothing to do with the bad practice and nothing to do with whether you declare multiple variables on one line. It's bad practice simply because it makes your code harder to read.

i understand your point about bad practice, and code harder to read, can you please give an example of the same declarations as a good practice. ?!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Abid Ramay wrote:i understand your point about bad practice, and code harder to read, can you please give an example of the same declarations as a good practice. ?!


Abid Ramay
Ranch Hand

Joined: Apr 06, 2013
Posts: 43
special thanks to @ Jeff Verdegan ... solution worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop logic