Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Repetition control structure using while loop.

Similar presentations


Presentation on theme: "The Repetition control structure using while loop."— Presentation transcript:

1 The Repetition control structure using while loop

2 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 Loop

3 While loop just as for loop In for loop initialize, condition & inc\dec define in same line But in while loop define separately While loop???

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

5 Flowchart

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

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

8 Flowchart

9 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. Sentinel controlled loop

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

11 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); Example

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

13 No.TopicsCounter controlled loop Sentinel controlled loop 01Number of executionPreviously known number of execution occurs. Unknown number of execution occurs. 02Condition variableCondition variable is known as counter variable. Condition variable is known as sentinel variable. 03Value 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. 04Examples= = = = 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); = = =

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

15 #include int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d", &n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); }

16 Program task2…. 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.

17 #include void main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); 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); }

18 Program task3…. 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

19 #include 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(); }

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

21 #include 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(); }


Download ppt "The Repetition control structure using while loop."

Similar presentations


Ads by Google