Download presentation
Presentation is loading. Please wait.
1
Week 6 CPS125
2
The while Loop condition is true */ Another type of looping structure
Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.
3
The while statement The while statement permits the repetition of the statements until the condition becomes false.
4
Counting loop int n; n = 1; /* initialization */ while (n <= 100) /* condition */ { printf ("%d ", n); /* body */ n = n + 1; /* update */ }
5
Execution of while In a while loop, the initialization is performed once, then the condition is checked. If the condition is true, the body and update statements are executed and the condition is then checked again. If the condition is false, the loop ends and the program continues to the next statement. init Condition update body
6
for vs. while The for loop is used when we know, A PRIORI, the number of iterations (counting loops). The while loop is used when we don't. This is a style issue only. We know that the for statement works exactly like the while statement.
7
Write a Program!!!!!!! TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE TO PRINT INTEGER NUMBER FROM 1 TO 100 USING FOR LOOP
8
/* PROGRAM # 29 */ /* TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE*/ #include <stdio.h> #define NUMBER 100 main( ){ int counter= 1; /*Print the numbers 1 through 100 */ while(counter <= NUMBER){ printf("Value of counter = %d\n", counter); counter++; } printf("The value of cnt after the while loop is over: %d\n", counter);
9
/* PROGRAM # 30*/ /*TO PRINT INTEGER NUMBER
FROM 1 TO 100 USING FOR LOOP*/ #include <stdio.h> #define NUMBER 100 main( ){ int counter; /* Print the numbers 1 through 100 */ for(counter= 1; counter <= NUMBER; counter = counter + 1) printf("Counter is %d. \n", counter); printf(“The counter after the loop is finished: %d.\n”, counter); } Q) What is the difference between these 2 programs?
10
Home Work!!! GET INTEGER NUMBERS AND ADD THEM DO THIS AS LONG AS THE NUMBER ENTERED IS POSITIVE
11
REVIEW Use a loop to repeat steps in a program. two kinds of loops occur frequently in programming: counting loops and sentinel-controlled loops. For a counting loop, the number of iterations required can be determined before the loop is entered. For a sentinel-controlled loop, repetition continues until a special data value is scanned.
12
The while Loop condition is true */ Another type of looping structure
Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.
13
The do while loop Similar to while-loop However,
The WHILE LOOP tests the loop condition at the top of the loop but The do while loop tests the condition at the end of the loop Therefore, regardless of the condition, the body of the DO WHILE LOOP is executed at least once. General Syntax: do{ statement; … }while (condition); As long as the condition is true, the body of the loop will be repeated.
14
Input validation loop using do-while
int n; do { printf ("Enter a number not between 1 and 5"); scanf ("%d", &n); }while (n < 1 || n > 5); /* rest of program */ ...
15
Q? TO PRINT INTEGER NUMBERS 1 THROUGH 100 USING DO WHILE LOOP.
16
/* PROGRAM # 33 */ /* TO PRINT INTEGER NUMBERS 1 THROUGH 100
USING DO WHILE LOOP */ #include <stdio.h> #define NUMBER 100 main( ){ int counter=1; /*Print numbers until number is greater than 100*/ do{ printf("counter is %d\n", counter); counter++; }while(counter <= NUMBER); printf("The value of the counter after the loop is over: %d.\n", counter); } Q) What is the value of counter after the loop is over?
17
Execution of do-while init Body condition update
In a do-while loop, the initialization is performed once, then the body and update statements are executed, and finally the condition is checked. If the condition is true, the body and update are executed once more. If the condition is false, the loop ends and the program continues to the next statement. init Body condition update
18
/*For while-2 */ #include <stdio.h> void main(void) { int i=4, j=1; do printf("old_i=%2d, ",i); i--; printf("new_i=%2d\n",i); } while(i>0); do ++j; while(j>999); printf("j=%2d\n",j); }
19
/*For Lesson for-2 */ #include <stdio.h> void main(void) { int i, j, k, m=0; for (i=1; i<=5; i+=2) { for (j=1; j<=4; j++) k = i+j; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } m=k+i; printf("i=%3d, j=%3d, k=%3d, m=%3d \n", i, j, k,m);
20
Nested Loops A nested loop is a loop within a loop, an inner loop within the body of an outer one. The inner and outer loops need not be generated by the same control structure (one can be a while loop, the other a for loop).
21
Nested Loops The inner loop must be completely embedded in the outer loop (no overlaps). Each loop must be controlled by a different index (loop control variable). i and j are often used and sometimes the outer loop (big loop) is called the i-loop and the inner loop (little loop) the j-loop.
22
A Nested Loop Example Suppose we want to display a 12x12 multiplication table. A nested loop here will make perfect sense. We can use the outer loop for the multiplicand and the inner loop for the multiplicator. The idea is to loop the outer loop 12 times and for each value, loop the inner loop 12 times as well.
23
A Nested Loop Example #include <stdio.h> int main (void) {
int i, j; for (i = 1; i <= 12; ++i) for ( j= 1; j <=12 ; ++j) printf ("%4d", i * j); printf ("\n"); } return (0);
24
What will be the exact output of this program?
#include <stdio.h> int main (void) { int a,b=3; double x=5.0, y; for (a=1; a<b; ++a) { while (b>0){ b=b-a; printf ("%5d%4d\n",a,b); } x=x-b; y=b%a; printf ("%7.2f%8.3lf\n", x,y);} printf ("The sum is: %3.1lf\n",a+b+x+y); return (0); }
25
1 2 1 1 The sum is: 7.0
26
Write a complete C program that asks the user for an integer n
Write a complete C program that asks the user for an integer n. If the number is positive, the summation of all numbers between 1 and n is displayed on the screen ( n). If the number is not positive, you must ask the user again for another number.
27
#include <stdio.h> int main (void){ int n, i, sum=0; do
{ printf ("Enter a positive number: "); scanf ("%d", &n); } while (n<=0); for (i=1; i<=n; ++i) sum = sum + i; printf ("%d", sum); return (0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.