Download presentation
Presentation is loading. Please wait.
1
Lec 7
2
The do-while Statement
Syntax: do {statements} while (loop repetition condition);
3
Example /* a program that prints the numbers from 1 to 10 */
#include<stdio.h> int main (void) { int counter=1; do printf("%i\n", counter); counter++; } while (counter<=10); return(0);
4
Example /* A program that computes the square root of a positive number, the program keeps prompting the user to enter a positive number until he enter it correctly. */ #include<stdio.h> #include<math.h> int main(void) {float num, y; do {printf("Enter a positive number: "); scanf("%f", &num);} while (num<0); printf( "The square root of %.2f is %.2f\n", num, y=sqrt(num)); return(0);}
5
Types of loops conditional loop counter loop sentinel loop
1.Reading an unknown amount of input from the user. This is the most flexible way to read data from the user. The program will be able to accept any amount of input from the user. 2.Validating input. If it is necessary to verify that a user's input falls within a certain range, then a sentinel loop is required. It is not known ahead of time how many times the user will enter an invalid number. Conditional loops have common traits with sentinel and count loops. They are like sentinel loops in that it is unknown before time how many times they will repeat. They are like counter loops in that they terminate as the result of a calculation, instead of based upon user input. 1.The loop always repeats the same number of times. 2.The program calculates the number of repetitions based upon user input.
6
Counter Loop /* a program to type the subtraction of two numbers until a condition is false */ #include <stdio.h> int main(void) {int i, j; for(i=1, j=10 ; i<=5 ; i++, j--) printf("%i + %i = %i\n", i, j, i+j); return 0;} = = = = = 11
7
Sentinel Loop /* a program to count the even numbers until a condition is false */ #include <stdio.h> int main(void) {int value, totalEven; totalEven=0; do {printf("Enter a list of numbers in the range (0-10)(any other number to quite): "); scanf("%i", &value); if (value%2==0) totalEven++;} while(value>=0 && value<=10); printf("The number of even numbers is %i\n", totalEven); return 0;}
8
Conditional Loop #include <stdio.h> int main(void)
{int year, pop; year=1; pop=9870; /* initialization */ while (pop<=30000) /* testing */ {printf("Year#%i Population = %i\n", year, pop); year++; pop=pop+(pop*.10);} /* updating */ printf("The population will surpass 30,000 during the %ith year\n", year); return(0);}
9
Review of Syntax The for loop:
for ( initialization ; loop condition ; update) {statements} The while loop: intialization; while (loop condition) {statements; update;} The do while loop: do
10
Loops C provides three statements for implementing loops: while, for,
and do-wile. Use the for to implement counting loops. Use the do-while to implement loops that must execute at least once, such as data validation loops for interactive programs. Code other conditional loops using the for or while, using whichever implementation is clearer.
11
Nested Loops Nested loops consist of an outer loop with one or more inner loops. For each outer loop repeated, all repetitions of the inner loop are repeated.
12
Nested loops for (initialization;testing;update) Examples
{ statements; initialization; while (testing) update; } Examples for (initialization;testing;update) { statements; for (initialization;testing;update){ statements }
13
Exercise Modify this program to display the following output: *****
#include<stdio.h> #define columns 5 int main(void) {int j; for (j=1; j<=columns ; j++) printf("*"); printf("\n"); return(0);} #include<stdio.h> #define rows 4 #define columns 5 int main(void) {int i,j; for (i=1; i<=rows; i++) {for (j=1; j<=columns ; j++) printf("*"); printf("\n");} return(0);}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.