0
int a=25: 
for (double i=1;i<=a;i++) 
{
    int b=5*i; 
    boolean value= b==a; 
    System.out.println(value);
 }

This method is true when i=5 but false otherwise. So the value can be true at i=5 but my program will print for me : false-false-false-false-TRUE-false-false-false... how can I make this program to print just TRUE for me. PS: I know that false or false or TRUE or false = True.. but how can I use it in the for loop?

K Mass
  • 53
  • 2
  • 3
  • 9

4 Answers4

4

Maybe this?

int a=25; 
for (double i=1;i<=a;i++) { 
    int b = 5 * (int)i;  // you must cast "i" in order for this to compile 
    boolean value = b == a;  // you probably wanted "b == a" not "b == i" 
    if (value)
        System.out.println("true");
}

i.e. we print "true" only if value is true.


To stop the loop when value becomes true, we can use a break statement:

int a=25; 
for (double i=1;i<=a;i++) { 
    int b = 5 * (int)i;
    boolean value = b == a; 
    if (value) {
        System.out.println("true");
        break;
    }
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Like this:

int a=25,b: 
boolean value;
for (double i=1;i<=a;i++) { 
    b=5*i; 
    value = (b==i); 
    if (value) {
        System.out.println(value);
    }
}

also never baeware of declaring variables inside loop body since they are redeclared in each iteration - it is bad pratice.

EDIT: LoL, this code always prints false, it cannot print true since

 b=5*i;

and 5*i is never equal to i

EDIT^2:

As CodeGuru suggested, with a==i it prints true only once:

int a=25,b;
boolean value;
for (double i=1;i<=a;i++)     {
    b=5*(int)i; 
    value= i==a; 
    if (value) {
        System.out.println(value);
    }
}
linski
  • 5,046
  • 3
  • 22
  • 35
  • 1
    "never declare variables inside loop body" Never? Really? – Code-Apprentice Oct 10 '12 at 00:23
  • well, at least I never found a use case that would be good - can you show me one? – linski Oct 10 '12 at 00:25
  • 1
    Personally, I would declare `b` inside the for loop as given in the original because it is only used inside the for loop and not afterwards. On the other hand, one solution to the OP's question takes advantage of declaring `value` outside the loop so that you can keep its value for each iteration. – Code-Apprentice Oct 10 '12 at 00:25
  • yes, but it is redeclared in each iteration - it is bad practice. (not that it actually matters in this example ) – linski Oct 10 '12 at 00:28
  • 1
    but I want it to print "true" only one time, in this example it will surely print true one time but in anothers case It will not print "true" one time.. – K Mass Oct 10 '12 at 00:29
  • 1
    @linski, i dont agree with "Never". If you declare it outside, one disadvantage is that the Scope of this variable is big. So even after you exit for loop, that is still hanging around in your memory. If it is big in size, it really does matter. – Jimmy Oct 10 '12 at 00:50
  • The scope is that big, how big is the scope that contains the loop, no? :) I updated the post accordingly, see this [discussion](http://stackoverflow.com/questions/407255/difference-between-declaring-variables-before-or-in-loop) – linski Oct 10 '12 at 06:29
0

One way to do this is similar to adding a list of numbers. You need an accumulator which is a variable that holds the result so far. Declare this before the loop:

boolean value = false;

Now inside the loop, use the |= operator:

value |= (b == i);

However, this seems like an ugly solution. What are you going to do with the true value when you find it?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0
      int a = 25;
    for (double i = 1; i <= a; i++) {
        double b = 5 * i;
        if (b == a) {
            boolean value = b == a;
            System.out.println(value);
        }
    }

This will work