+ 13

Explaination Is Needed

int a=0; int b=1; for (int i=0;i<=b+1;i++){ if (i==2){ System.out.println(a);} else{ System.out.println(a+b);} } Output is 110 Please explain it

13th Dec 2017, 3:27 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
10 ответов
+ 10
3 loops a=0, a+b=1 'if' prints '0' 'else' prints '1' 1st loop-->□ 'if' is false then print 'else' □ print 1 2nd loop-->□ 'if' is false then print 'else' □ print 1 3rd loop-->□ 'if' is true □ print 0 Output: 110
13th Dec 2017, 4:35 AM
I'm_Groot
I'm_Groot - avatar
+ 11
for loop will run 3 times for value of i=0, 1 & 2...... for value of i =0 ------> output = 0+1 =1. (if condition. false for i= 0,1) i =1 --------> output = 1 I =2 --------> if condition becomes true so output = a =0 output = 110
13th Dec 2017, 3:33 AM
🌛DT🌜
🌛DT🌜 - avatar
+ 9
In the first iteration of the cycle i != 2 (i = 0) so it prints on the screen a + b (0 + 1 = 1), in the second iteration of the cycle (i = 1) the same case is repeated At this moment the output is: 11 In the last iteration i == 2, so print a (a = 0), which results in 110
13th Dec 2017, 3:38 AM
Mickel
Mickel - avatar
+ 9
firstly see the for loop , syntax for forloop is (initial value; final value ;incresing in that steps) first step (int a=0,int b=1) we have given 0,1 values to our variables a and b respectively. second step =>The for loop 1)in this the initial value for i is give as 0 2)since 0 <2 we go inside the for loop and execute the statements... 3)inside the for loop we have (if) and (else) statement if(0==2) then print value of a else print the value of b since 0!=2 we go for else statement and print value of b i.e 1 now we go back to for loop and incremwnt the value of i by 1 i.e now it will 0+1=1 now this value will be compared and similar steps to bw followed and we will get our answer as 110 Hope this answerd ur question.
13th Dec 2017, 3:41 AM
Shariff
Shariff - avatar
+ 8
@Mickel Nice explanation
13th Dec 2017, 3:42 AM
Arushi Singhania
Arushi Singhania - avatar
+ 7
I don't know...😞😞
13th Dec 2017, 3:32 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 7
Thanks you all
13th Dec 2017, 4:37 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
//these two lines are declaring a and b as integar type and assaigning them value int a=0; int b=1; /* this is for loop in which firstly i is initialized as 0 and than condition i<= b will be checked if condition is true than control will flow inside { } and will execute whatever code is written inside {} and final part of for loop will increase value of i by value of 1 after each execution. */ for (int i=0;i<=b+1;i++){ /* if condition it will give true or false if condition is true whatever inside {} is present will be executed. */ if (i==2){ // println method will print a // System.out.println(a);} /* IF if condition returns false than control flows to else and executes whatever code is written inside {} */ else{ System.out.println(a+b);} } /* AS FOR LOOPS CONDITION IS i<=b+1 it means for loop will be executed 3 times FIRST TIME WHEN i=0 OUTPUT WILL PRINT else PART THAT MEANS a+b WHICH WILL BE (0+1) = 1 WHEN SECOND TIME LOOP WILL RUN i=1 AGAIN else CONDITION WILL PE PRINTED HENCE AGAIN 1 WILL PE OUTPUT NOW WHEN LOOP WILL RUN FOR THIRD TIME i=2 AND THAT MEANS if CONDITION WILL BE TRUE AND HENCE THIS TIME OUTPUT WILL BE a WHICH IS 0. AND HENCE TOTAL OUTPUT WILL BE 110. */ Output is 110
13th Dec 2017, 2:26 PM
shashank singh
shashank singh - avatar