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?

2
  • 2
    I cannot understand this code. I've also tested it and can never get a true out of this. b cannot ever equal i. Am I missing something in the code itself? Commented Oct 10, 2012 at 0:26
  • 1
    @Suroot Good catch. Maybe the OP means a == i? Commented Oct 10, 2012 at 0:32

4 Answers 4

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;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

but I want it to print true only one time
The code is not compilable, remove the : in the first line, and cast i to int b==(int)i. It will never print true
@linski Thanks for the tip - I just copied/pasted what he had without checking for errors. I edited my answer.
u r welcome :) but it still won print true :))) as CodeGuru suggested if we put a==i it will but now I don't understand anything anymore. good night
there is not a way to stop the loop when the boolean give me true ?
|
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);
    }
}

7 Comments

"never declare variables inside loop body" Never? Really?
well, at least I never found a use case that would be good - can you show me one?
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.
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..
@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.
|
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?

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.