# Is there any formula or trick to count how many times a statement inside a nested loop gets executed?

I am modifying a software which is for trash collection. While reading the code, I asked myself is there any formula or trick to quickly calculate the number of time the statement gets executed inside the nested loop rather than doing it manually in head. Here's the kinda pseudo code for this----

``````for(i=1; i<=9;i++)
{
sum = 0;
for(j=i; j<=N && sum<=10; j++)
sum = sum + arr[j] //any quick trick to calculate it's number of execution
}
``````
• In this situation, it will highly depend on the content of the array, due to sum<=10 predicate. You can estimate worst and best case scenarios. But for real-world execution, you need to run it and measure it. Mar 20, 2020 at 12:15
• By introducing a counter variable? Or do you mean "beforehand", by analysing the code? Mar 20, 2020 at 12:25
• Do you know about computational complexity ? Mar 20, 2020 at 12:35
• OK. Which takes us back to @Euphoric's comment. Mar 20, 2020 at 12:39
• Quick trick eh? Set N to 0 and it executes no times : ) Seriously the quickest way to calculate this is to write a program that calculates this. Mar 20, 2020 at 13:50

You can use another variable like this to find out how many time loop is executed or what is the current number of a loop being executing in debug :

``````int count = 0;

for(i=1; i<=9;i++)
{
sum = 0;
for(j=i; j<=N && sum<=10; j++)
{
sum = sum + arr[j] //any quick trick to calculate it's number of execution
count++;
}
}
``````