For loop & Catch handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

For loop & Catch handling

int n=6; for (int i=0;i <3;++i){ try n/=i; } catch(Exception e){ n+=2; } } //Output:4 tried to solve and i know just a little bit how but i need more explanation why from someone who knows it thank-you😊

27th Feb 2018, 11:05 PM
Sha
Sha - avatar
2 Answers
+ 7
n=6 loop 1: n = 6/0 = ~ call cactch block: n = 6+2= 8 loop 2: n = 8/1 = 8 loop 3: n = 8/2 = 4 end loop that's why the out put is 4;
27th Feb 2018, 11:40 PM
Nura Programmer
Nura Programmer - avatar
+ 3
in the first loop the try block throws an exception (divide by 0 ) so the catch block gets executed (n becomes 8). and the rest is explained very well by @th3_c0d3r
28th Feb 2018, 2:44 AM
Farshaad Heydari
Farshaad Heydari - avatar