Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 15)
15.
What will be the output of the program?
public class Delta 
{ 
    static boolean foo(char c) 
    {
        System.out.print(c); 
        return true; 
    } 
    public static void main( String[] argv ) 
    {
        int i = 0; 
        for (foo('A'); foo('B') && (i < 2); foo('C')) 
        {
            i++; 
            foo('D'); 
        } 
    } 
}
ABDCBDCB
ABCDABCD
Compilation fails.
An exception is thrown at runtime.
Answer: Option
Explanation:

'A' is only printed once at the very start as it is in the initialisation section of the for loop. The loop will only initialise that once.

'B' is printed as it is part of the test carried out in order to run the loop.

'D' is printed as it is in the loop.

'C' is printed as it is in the increment section of the loop and will 'increment' only at the end of each loop. Here ends the first loop. Again 'B' is printed as part of the loop test.

'D' is printed as it is in the loop.

'C' is printed as it 'increments' at the end of each loop.

Again 'B' is printed as part of the loop test. At this point the test fails because the other part of the test (i < 2) is no longer true. i has been increased in value by 1 for each loop with the line: i++;

This results in a printout of ABDCBDCB

Discussion:
15 comments Page 1 of 2.

Rupesh singh said:   8 years ago
Please anyone explain me clearly to get it.

Ami said:   8 years ago
Can anyone explain in detail?

Anish Kumar said:   9 years ago
This is a correct program.

No doubt.

We can write for loop as for (anything ;anything which will give boolean result ;anything).

But we have to initialize the for loop before execution of for loop and we can increment inside the loop.

Saurabh said:   9 years ago
Hi @Volod. The program will compile successfully and print "ABDCBDCB".
and "public static void main( String[] argv )". Line is fully correct because,
1. int[]a;
2. int a[]; both array declaration is fine.

"Yatin Sharma" short circuit operator"&&"AND) not create any problem because foo()method every time return true that's why it will check the second condition.

And for loop can work fine with condition only ex:- for(;a&&b;).

Volod said:   9 years ago
public static void main( String[] argv )

What about this line?

I think that it will be compilation problem!

Shankar said:   10 years ago
I also fail to understand the result, can anyone please explain the for loop in detail and the output with regards to it?

Brajesh said:   10 years ago
Three loop are going to run.

1. Will print ABDC.

2. BDC.

3. CB.

Friends you will see that there are there section of for loop.

Riya said:   10 years ago
Right @Gaurav My Question is same as yours.

How come D prints before B, and then C is also being printed? Any one please answer this?

Gaurav said:   1 decade ago
How can D print after B.

And again after D it print C.

Can any one explain this process?

Madhu said:   1 decade ago
The code is perfect and produces the output as mentioned in option A.


Post your comments here:

Your comments will be displayed after verification.