The Repetition control structure using while loop Loop

  • Slides: 21
Download presentation
The Repetition control structure using while loop

The Repetition control structure using while loop

Loop • Loops causes program to execute the certain block of code repeatedly until

Loop • Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i. e. , loops are used in performing repetitive work in programming. • Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop. • There are 3 types of loops in C programming: – for loop – while loop – do. . . while loop

While loop? ? ? • While loop just as for loop • In for

While loop? ? ? • While loop just as for loop • In for loop initialize, condition & incdec define in same line • But in while loop define separately

Syntax of while loop while (test expression) { statement/s to be executed; }

Syntax of while loop while (test expression) { statement/s to be executed; }

Flowchart

Flowchart

Example *print 1 st 10 whole number * int a=0; while(a<=10) { printf(“%d”, a);

Example *print 1 st 10 whole number * int a=0; while(a<=10) { printf(“%d”, a); a++; }

Syntax of do-while loop do { statement/s to be executed; } while (test expression)

Syntax of do-while loop do { statement/s to be executed; } while (test expression) ;

Flowchart

Flowchart

Sentinel controlled loop • The type of loop where the number of execution of

Sentinel controlled loop • The type of loop where the number of execution of the loop is unknown, is termed by sentinel controlled loop. In this case, the value of the control variable differs within a limitation and the execution can be terminated at any moment as the value of the variable is not controlled by the loop. The control variable in this case is termed by sentinel variable.

Example *print 1 st 10 whole number * int a=0; do { printf(“%d”, a);

Example *print 1 st 10 whole number * int a=0; do { printf(“%d”, a); a++; } while(a<=10);

Example The following do. . while loop is an example of sentinel controlled loop.

Example The following do. . while loop is an example of sentinel controlled loop. int num =0; do { printf(“Input a number. n”); scanf("%d", &num); } while(num>0);

The differences between the counter and sentinel controlled loops are as follows

The differences between the counter and sentinel controlled loops are as follows

No. Topics Counter controlled loop Sentinel controlled loop 01 Number of execution Previously known

No. Topics Counter controlled loop Sentinel controlled loop 01 Number of execution Previously known number of execution occurs. Unknown number execution occurs. of 02 Condition variable is known as counter variable. Condition variable is known as sentinel variable. 03 Value and limitation of variable The value of the variable and the limitation of the condition for the variable both are strict. The limitation for the condition variable is strict but the value of the variable varies in this case. 04 Examples ==== sum = 0; n = 1; while (n <= 10) { sum = sum + n*n; n = n+ 1; } ====== do { printf(“Input a number. n”); scanf("%d", &num); } while(num>0); ======

Program task 1…. • Write a program to reverse the number for example if

Program task 1…. • Write a program to reverse the number for example if user enter 123 as input then 321 is printed as output.

#include <stdio. h> #include <conio. h> int main() { int n, reverse = 0;

#include <stdio. h> #include <conio. h> int main() { int n, reverse = 0; printf("Enter a number to reversen"); scanf("%d", &n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %dn", reverse); }

Program task 2…. Write a program to find a palindrome number. A palindrome number

Program task 2…. Write a program to find a palindrome number. A palindrome number is a number such that if we reverse it, it will not change For example some palindrome numbers examples are 12321, 121, 212, 12321, -454.

#include <stdio. h> void main() { int n, reverse = 0, temp; printf("Enter a

#include <stdio. h> void main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or notn"); scanf("%d", &n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number. n", n); else printf("%d is not a palindrome number. n", n); }

Program task 3…. • Write a program that computes the sum of the following

Program task 3…. • Write a program that computes the sum of the following series using the while loop. 1 + 1/2 + 1/4 + 1/6 + 1/8 + ………. + 1/100

#include <stdio. h> #include<conio. h> void main() { float n, s; n=2. 0; s=1.

#include <stdio. h> #include<conio. h> void main() { float n, s; n=2. 0; s=1. 0; while(n<=100) { s = s + 1. 0/n; n = n + 2; } printf(“sum of series = %d” , s ); getch(); }

Program task 4…. • Write a program that computes the sum of the following

Program task 4…. • Write a program that computes the sum of the following series using the do while loop. 1 + 1/3 + 1/5 + ………. + 1/199

#include <stdio. h> #include<conio. h> void main() { float n, s; n=1. 0; s=0.

#include <stdio. h> #include<conio. h> void main() { float n, s; n=1. 0; s=0. 0; do { s = s + 1/n; n = n + 2; } while(n<=99); printf(“sum of series = %d” , s ); getch(); }