loop execution decending order in c
Here is an example of a loop in C++ that executes in descending order:
#include <iostream>
using namespace std;
int main() {
int i;
for (i = 10; i >= 1; i--) {
cout << i << " ";
}
return 0;
}
Explanation for each step:
- The
#include <iostream>
statement includes the necessary input/output library for the program. - The
using namespace std;
statement allows us to use thecout
object from thestd
namespace without explicitly specifying it. - The
int main()
function is the entry point of the program. - The variable
i
is declared as an integer. - The
for
loop is used to execute a block of code repeatedly. Here, the loop variablei
is initialized to 10, and the loop will continue as long asi
is greater than or equal to 1. After each iteration, the value ofi
is decremented by 1 using thei--
statement. - Inside the loop,
cout << i << " ";
is used to print the value ofi
followed by a space. - After the loop completes, the
return 0;
statement indicates that the program has executed successfully.
This program will output the numbers from 10 to 1 in descending order.