Sei sulla pagina 1di 23

Handout 4b

LOOPS

while loop for loop do while loop

While loop
Syntax: while (<expression>) <statement>

Example: Print the numbers 1 to 20

#include <stdio.h>
void main(void) { int i;

i = 1;

/* initialization */

while (i <= 20) /* conditional check */ { printf ("%d\n", i); i++; }


}

Exercises 1) Write a C program that will generate the numbers from 10 down to 1, i.e., 10,9,8, ..., 2, 1 using a while loop. 2) Write a program that will print all the odd numbers from 1 to 149 using a while loop. 3) Write a program that will print the values 0.0, 0.3, 0.6, ,2.1 using a while loop.

for loop
Syntax:
for ([initialization]; [condition]; [change of state]) <statement>

More compact form of the while loop Executed as follows:


perform the initialization check the condition if 1 (true) execute the <statement>,ie., the body of loop, then change the state, and check for the condition again if it is false, exit from the loop

example
Print the numbers from 1 to 50 using a for loop
#include<stdio.h> void main () { int i; for (i=1;i<=50;i++) printf("%d\n",i);

Program that prints the numbers 0.0, 0.3, 0.6, 2.1 using a for loop
/*Program that prints the numbers 0.0, 0.3, 0.6,...,2.1 */ #include<stdio.h> void main () { float f; for (f=0; f<=2.1; f=f+0.3) printf("%.1f ",f); }

do while loop
Syntax:

do
<statement> while (<expression>)

/* program to print numbers from 1 to 50 using do while loop */


#include <stdio.h> void main () { int i; i=1; do { printf("%d\n",i); i++; } while (i<=50); }

/* program to print numbers from 50 to 1 using do while */ #include<stdio.h> void main () { int i=50; do { printf("%d\n",i); i--;

} while (i>0);
}

Exercise: Write a program that will print the numbers 0.0, 0.3, 0.6,,2.1 using do while loop.

counter
Used to keep track of the count of a certain group of items Usually
Data type is int Initialized to a value 0 Incremented by 1 inside a loop

Example: Write a program that will ask the user to input 10 integers. The program should output how many of the input data are positive.

#include <stdio.h>
void main(void) { int i; int n; int ctr_positive; ctr_positive = 0;

/* this is the counter */ /* initialization */

for (i = 0; i < 10; i++) { printf("Input integer number %d: ", i); scanf("%d", &n);

/* is the number positive? */ if (n > 0) ctr_positive = ctr_positive + 1; /* change counter */ } printf("The number of positive integers =%d\n" , ctr_positive) ; }

Accumulator
a variable that is used to keep track of the accumulated value of a certain group of items. may have a data type of int, float or double it is usually initialized to a value of 0 changes by assuming the sum of the current value of the accumulator and the value of another variable

Example Write a program that will ask the user to input 10 integers. The program should output the sum of all the input data.

#include <stdio.h> void main(void) { int i; int n; int sum; /* this is the accumulator */

sum = 0;

/* initialization */

for (i = 0; i < 10; i++) { printf ("Input integer number %d: ",i); scanf("%d", &n); sum = sum + n; /* accumulate sum of all input n */
} printf("The sum of the integers is %d\n", sum); }

Write a program that prints the capital letter E in a grid of 17 rows and 20 columns, consisting of the characters blank and * as follows: 1. Twenty asterisks on rows 1 through 3 and 15 through 17 2. Four asterisks on rows 4 through 7 and 11 through 14 3. Thirteen asterisks on rows 8 through 10.

******************** ******************** ******************** **** **** **** **** ********** ********** ********** **** **** **** **** ******************** ******************** ********************

#include <stdio.h> void main(void) { int i, j; for (i = 1; i <= 17; i += 1) { if (i <= 3 || i >= 15) for (j = 1; j <= 20; j += 1) printf("*"); if (i > 3 && i <= 7 || i > 10 && i <= 14) for (j = 1; j <= 4; j += 1) printf("*");

if (i > 7 && i <= 10) for (j = 1; j <= 13; j += 1) printf("*"); printf("\n");


}

Exercises:
1) Write a C implementation of all the pseudo-coding exercises. 2) Write a program that reads in a number N and outputs the sum the squares of the numbers from 1 to N. Example: input = 4 output = 30 (i.e. 1^2 + 2^2 + 3^2 + 4^2) 3) Write a program that will compute for and display the sum of numbers from 1 to 400 that are divisible by 3. 4) Write a program that will ask the user for an integer N and display an N x N square of asterisks. Example: input is 5 output * * * * * * * * * * * * * * * * * * * * * * * * *

Potrebbero piacerti anche