Using for loop. What is the output for(int i=5; i>=0; i–) cout<
Expert Answer
program:
#include <iostream>
using namespace std;
int main()
{
int i; //loop variable
for(int i=5; i>=0; i–) //run for loop
{
cout<<“nvalue of is:”<<i; //prints i value
}
return 0;//return success
}
output:
Explanation:
from given for loop for(int i=5; i>=0; i–)
i intialized to 5 and check for condition whether i>=0 so here i=5 so
1st iteration: i prints 5
then i become i– i.e 4
2nd iteration
:i check for condition whether >=0 , here i=4
so i prints 4
i–:3
3rd iteration:
here i=3 and check i>=0 yes so
i prints 3
now i– i.e 2
4th iteration: now i=2 and check whether i>=0 yes
so i prints value 2
then i– i.e 1
5th iteration: here i=1 so i>=0 condition true
so i prints value 1
then i– i.e 0
6th iteration: here i=0 condition i>0 true
so i prints value 0
then i– i.e -1
7th iteration: here i=-1 so check for condition i>=0 ,-1>=0 condition fails so loop will terminated