Sometimes we want some part of our code to be executed more than once. We can repeat the code in our program. For example, we need to display our name for a hundred or more times it is not practical to write the same code. Which concept will be used? Explain it with the help of suitable example
[5 marks]
Loops can be used to execute a code more than once. A while or a for loop can execute a block of code many times provided a given condition is true.
#include<iostream>
using namespace std;
int main(){
//This code will display My name is Abraham 100 times
for(int i=0; i<100; i++){
cout<<"My name is Abraham\n";
}
}
Comments
Leave a comment