sorry

observe the following coding and find how many times "I am very very sorry" displayed??

    int k=10;
      for(int i=0;i<k--;i++)
          System.out.println("I am very very sorry");


The answer is 5.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Sonali Srivastava
Jul 18, 2014

value of i is increasing (i++) while value of k is decreasing (k-- in condition)...Values are as under s.no. i k 1 0 9(k--) 2 1 8 3 2 7 4 3 6 5 4 5 6 5 6 (this violates the condition i<k--) hence the statement is printed 5 times.

Bernardo Sulzbach
Jun 18, 2014

I do not like Java.

Solve it with a table if you need:

i, k, bool

0, 10, 1

1, 09, 1

2, 08, 1

3, 07, 1

4, 06, 1

5, 05, 0

There are 5 (true) evaluations for i < k--.

no operation on k.....so it will remain 10 through out the procedure...so ans should be 8...!!

Darshna Parmar - 6 years, 11 months ago

absolutely wrong! the correct answer should be 10! the value of k doesn't change ...... since no operation is performed with "k" itself.
i<k-- only checks the value of "k"........ doesn't change it!

Mayank Holmes - 6 years, 11 months ago

Log in to reply

there is a post decrement opearator after k (k--)...so with every iteration of the loop k's value decreases.....so answer will be 5

Raj Maiti - 6 years, 11 months ago

5 hoga bhai

meghna priyadarshini - 6 years, 11 months ago

k-- executes k=k-1 on the statement, thus everytime the cycle runs an iteration k=k-1

Carlos David Nexans - 6 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...