Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the final value of the variable n after this C++ code excuted: int n=5; for(int i=1; i<=n; i++){ cout<<'*'; n--; }?

user-image
Question added by هاشم المشارقة , Key Account Manager , Advanced United Systems Ltd. ( A member of Taj Holding Group)
Date Posted: 2016/02/03
Gayasuddin Mohammed
by Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

The value of n after the execution is2. it iterates3 times while n decrements by1 and i increments by1 so when3 < =2 (i <=2) fails, loop fails and comes out of the for loop. Thanks.

MohammedMansoor Sahul Hameed
by MohammedMansoor Sahul Hameed , Senior Software Engineer , Femina Lace International

Value of n will be2, since inside the loop value of n is decreasing and the loop will execute only till i<=n.

salma samreen
by salma samreen , Quality Assurance Engineer , Private Limited

int n=5; for (int i=1,i<=5,i++)

cout<<"* n--<<

}

here the output sequence will be you will increment the value of i, till you try to come out of for loop and simultaneously decrement the value of n,

the end result will be4,3,2,1,0

More Questions Like This