More for loop notes/loop control

More For Loop Examples

Did a debugging example

Loop Control

Statements

Break Statement

loop {

if(exp)

    break;

// When we execute a break statement in a loop (or a switch), we immediately move execution outside the loop (immediately after)

}

example:

// Try each trial divisor.
    for (i=2; i<n; i++) {

        // Check if i divides into n evenly...
        if (n%i == 0) {
            isPrime = 0;

            // No point in continuing the loop, stop immediately.
            break;
        }
    }

Continue Statement

loop{

   
 statement1;
 if (exp){

	continue;
}statement 2;

}

statement 3;

// If you have set continue, it means that you don’t want to compute this loop iteration, but immediately want to continue to the next one

//Each time the expression is true, we’ll skip statement 2

example

 while (1) {

        int score;
        scanf("%d", &score);

        // We are done.
        if (score == -1)
            break;

        // Not done, but we can't count this score.
        if (score < 0 || score > 100)
            continue;

        // Update our accumulators.
        numscores++;
        sumscores += score;
    }

do-while

(not really used)

1) Run loop c

2) Check the boolean expression 1 if then go to step 1

else go to statement 2 then statement x

// Benefit is that you are guaranteed one loop

Nested Loops

loops inside of a lop

for(int i=o; i<n;i++){

    for(int j = 0; j < m; j++){

        printf(“%d, %d”, i, j)

}

printf(“\n);

}

When I changes to another value, J resets

n = 3

m = 4

When I is 0, J is 0, 1, 2, 3, 4

when I is 1, J is 0,1,2,3,4

when I is 2, J is 0,1,2,3,4

when I is 3, J is 0,1,2,3,4

(i, j)

(0,0), (0,1), (0,2), ….

(1,0), (1, 1)….

Simple Application (Multiplication Table)

n = 12, m = 12;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            printf("%5d", i*j);
        }
        printf("\n");
    }
    printf("\n");

Harmonic Number Application

The i^th Hermonic number #

H(i) = 1 + ½ +1/3 + ¼ + 1/5 + …

i: 1 2 2 3 3 3 4 4 4 4

j: 1 1 2 1 2 3 1 2 3 4

for (int i=1; i<=n; i++) {

        // i tells us how far we have to add.
        double sum = 0;

        // j is the current term in our current sum.
        for (int j=1; j<=i; j++)
            sum += 1.0/j;

        // Print out index and corresponding Harmonic number.
        printf("%d %lf\n", i, sum);
    }

Counting Down

    // Counting down example from lecture notes. for (int i=n; i>=1; i--) { // Print a countdown from i. for (int j=i; j>=1; j--) printf("%d ", j); // Go to new line. printf("\n"); }

Stars Example

int row, col, numstars;
    int rate = 1;

    // Loop through each row.
    for (row=1; row<=TOTAL_ROWS; row++) {

        numstars = row;

        // Print out the correct number of stars.
        for (col=1; col<=numstars; col++)
            printf("*");

        // Go to the new line.
        printf("\n");
    }

Starsbacktriangle Example

 // loop index represents # of stars on each row.
    for (int stars=1; stars<=n; stars++) {

        int numspaces = n-stars;

        // This prints numspaces spaces.
        for (int i=0; i<numspaces; i++)
            printf(" ");

        // This prints stars number of stars.
        for (int i=0; i<stars; i++)
            printf("*");

        // Go to the next line.
        printf("\n");
    }


Diamond Example

int n;
    printf("Enter a odd positive integer.\n");
    scanf("%d", &n);

    // Number of spaces on the first row.
    // loop index represents # of stars on each row.
    for (int stars=1, numspaces=n/2; stars<=n; stars+=2, numspaces--) {

        // This prints numspaces spaces.
        for (int i=0; i<numspaces; i++)
            printf(" ");

        // This prints stars number of stars.
        for (int i=0; i<stars; i++)
            printf("*");

        // Go to the next line.
        printf("\n");
    }

    // Print second half.
    for (int stars=n-2, numspaces=1; stars>0; stars-=2, numspaces++) {

        // This prints numspaces spaces.
        for (int i=0; i<numspaces; i++)
            printf(" ");

        // This prints stars number of stars.
        for (int i=0; i<stars; i++)
            printf("*");

        // Go to the next line.
        printf("\n");
    }


You can use two or more variable declarations in a for loop

XYZ Example

 int x, y, n, numsols = 0;

    // Get the total for the equation.
    printf("Please enter n.\n");
    scanf("%d", &n);

    // Go through all possible x's.
    for (x=1; x<n; x++) {

        // And y's.
        for (y=x+1; y<n; y++) {

            // No third loop needed, I know z!.
            int z = n - x - y;

            // No point in looking for further z's.
            if (z <= y)
                break;

            // Print and update our counter.
            printf("x=%d, y=%d, z=%d\n", x, y, z);
            numsols++;
        }
    }

    // Print out the total number of solutions.
    printf("There were a total of %d solutions.\n", numsols);
    return 0;

Perfect Numbers Finder

    dificient is when the sum of divisors is < Self

    abundant is when the sum of divisors is > Self

     “ “ “ “ == Self = Perfect Number

It loops to the square root, but only with integers ( i * I <= n)