wile loop 22 april 2015

2
22 April 2015 The While Statement It is used to perform repetition in C programming. The general format of the while statement is: while (test condition) { body of the loop; } Here the given test condition is evaluated and if the condition is true then the body of the loop is executed. The process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. Following diagram illustrates how “while” statement operates

Upload: mohd-faizal

Post on 07-Nov-2015

217 views

Category:

Documents


0 download

DESCRIPTION

ss

TRANSCRIPT

22 April 2015

The While Statement

It is used to perform repetition in C programming.

The general format of the while statement is:

while (test condition) { body of the loop; } Here the given test condition is evaluated and if the condition is true then the body of the loop is executed. The process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. Following diagram illustrates how while statement operates

Example program for printing the given number is ODD or EVEN repeatedly until the user wants to quit using do while loop:

# include < stdio.h >int main(){ int n; char opt = 'y'; while( (opt == 'y') || (opt == 'Y')) { printf(" \n Enter an integer number: "); scanf ("%d",&n); getchar(); // to capture the enter key pressed after the entry if (n%2==0) printf("\n The number is EVEN"); else printf ("\n The number is ODD"); printf("\n Would you like to continue : "); scanf("%c", &opt); } printf(" \n Bye Bye.....");}