CSCI 102 - Fall 2023 Fundamentals of Computation

#include <iostream>
using namespace std;

int main()
{
    int n = 15;
    int total = 0;

	// will run for i = 1, 3, 5, 7, 9, 11, 13
    for(int i = 1; i < n; i+=2)
    {
		// only the values of 3 and 9 will make this if statement true
		// i values of 1, 5, 7, 11, 13 will skip over this if statement
		//   and go on to the next value of i
        if (i % 3 == 0)
        {
			// total will add 0+3 and save 3 to total
			// then total will add 3+9 and save 12 to total
             total += i;
        }
    }
	
	// Without the break statement below, this loop would start with
	// total = 12 and count down from 12, 11, ... 1 
	// stopping when total when total is 0.  We will see that with
	// the break statement we will end early.
    for ( ; total > 0; total--)
    {
		 // when total = 12, n will increment to 16
		 // when total = 11, n will increment to 17
		 // when total = 10, n will increment to 18
          n++;

		  // the first time this if statement will trigger is for total = 10
		  // but then the break statement quits the loop immediately.
          if(total % 5 == 0)
          {
               break;
          }
    }
	// The break statement above will move execution to after the for loop
	// and at this point n = 18 and total = 10
    return 0;
}