You are on page 1of 11

loops while loop: The simplest of the three loops is the while loop.

In common language while has a fairly obvious meaning: the while-loop has a condition: while (condition) { statements; } and the statements in the curly braces are executed while the condition has the value "true" ( 1 ). There are dialects of English, however, in which "while" does not have its commonplace meaning, so it is worthwhile explaining the steps which take place in a while loop

write program to print the word go to hell frequently #include<stdio.h> void main() { int a; scanf("%d",&a); while(a<=5) { printf("\n go to hell "); a+=1; } } How it works - Step by step:1 first insiallize the the a 2)take value from the keybord using scanf 3)here is conditionto repeat the word go to hell upto a<=5.therefore I use while loop as while(a<=5) 4)if we give value of a=2 then it pass through while loop while loop check is a=2 is less than 5 or not ? yes then it print go to hell .but the process is not stop here after printing because a=a+1; give increment to the value of a=2 and make it a=3 and the whole process repeat again.here also a=3 is less than a<=5 and print go to hell and give increment in the value of and make it a=4 this process is continue upto a<=5.

5)when value become more than 5 it stop automatically. Live image of output :-

C in my words 1) Dont put ; after while statement :What happen if you put If you put ; after the while statement ?what type of error comes during compiling ? If you put ; after the while statement the statement close itself and program print nothing . and onother question is what type of error comes during compiling ? Compiler dont give any type of error because compiler give only error in the way of writing c language and putting a pair of braces around any individual statement or set of statements without affecting the execution of the program. Is not error for compiler. The error is givenby runtime 2)Just try what happen the when we a+=1; in while loop. #include<stdio.h> void main()

{ int a; scanf("%d",&a); while(a<=5) { printf("\n go to hell "); } } Your output is going to be indefinite loop. You can see in live output image Live image of output :-

Onother classic example of indefinite loop is following #include<stdio.h>

void main( ) { int i=1; while (i <= 32767 ) printf ("%d\n", i); i=i+1; } Live output image:Here you can see the output 1 is printed n times.

Why ? the resion is int has range its form 1 to 32767 after the further increment it become 1 because 32768 is out of range .therefore program never stop it continuously print 1 infinite time.

3)It is not defalt that program should be in increment and write program always in integer we can also write the progam using a- =a (i.e decrement) and we can also use the float #include<stdio.h> void main() { float a=10.24; while(a>=5.026) { printf("\n hello"); a=a-1; } } Live output image:-

do...while loop #include <stdio.h> main() { int i =5; do{ printf("Hello %d\n", i ); i = i -1; } while ( i > 0 ); } How it work-

first step is statement in do loop is directly print as it is "Hello then while condition is check. If it true then program executed Live output image:-

break Statement We can make use of break to come out of do...while loop at any time. #include <stdio.h> void main() { int i; for(i=1;i<5 ; i++) { if(i>3) break;

printf("\n%d",i); } } Live output image:-

How it workHere the for condition is loop should be break when i<5 but in break we put i>3 therefor no more then 3 is not printed .

The continue Statement Continue statement is use for the jump one statement #include <stdio.h> void main()

{ int i; for(i=1; i<=10; i++) { if(i==6) continue; printf("\n\t %d",i); // 6 is omitted } } How it workContinue statement is use for the jump one statement. Here in given condition printf("\n\t %d",i); condition is omitted when i==6.therefore in output no 6 is not printed

You might also like