PREPARED BY-PRADEEP DWIVEDI(persuingB.TECH-IT)	from HINDUSTAN COLLEGE OF SCIENCE AND 			  TECHNOLOGY(MATHURA)	MOB-+919027843806	E-MAIL-pradeep.it74@gmail.com	C-PROGRAMMING SLIDE-4Wednesday, September 01, 20101PRADEEP DWIVEDI
C-4TOPIC:-Loopbreak statementcontinue statementgo to statementWednesday, September 01, 20102PRADEEP DWIVEDI
LOOPWednesday, September 01, 2010PRADEEP DWIVEDI3when we want to repeat a particular task again and again that time we use the loop.loop is a control structure that repeats a group of sets in a program.there are three loops in c-while fordo while
WHILE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI4syntax:-while(test-condition){body of the loop.}
WHILE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI5the while loop is a entry controlled loop statement.the test condition is evaluated and if the condition is true then the body of loop is executed.after execution of the body the test condition is once again evaluated and if it is true the body is executed once again.this process of repeated execution of the body continues until the test condition finally becomes false and controlled is transferred out of the loop.
FLOW CHART(WHILE LOOP)Wednesday, September 01, 2010PRADEEP DWIVEDI6
PROG13Wednesday, September 01, 2010PRADEEP DWIVEDI7/* w a p to print 1 to 10  */#include<stdio.h>#include<conio.h>void main(){inti;clrscr();i=1;while(i<=10){printf("%d\t",i);i++;}getch();}
DO WHILE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI8in while loop, makes a test of condition before the loop is executed.therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt.on a some situations it must be necessary to execute the body of the loop before the test is performed. such situation can be handled with the help of the do statement.
DO WHILE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI9syntax:-do{body of the loop           (true)}while(test-condition);
DO WHILE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI10in the do while loop it first execute the body and then checks the condition.so if the condition is initially false, it execute atleast once.
prog14Wednesday, September 01, 2010PRADEEP DWIVEDI11/* w.a.p. to print 1 TO 10 digit */#include<stdio.h>#include<conio.h>void main(){inti;clrscr();i=1;do{printf("%d\t",i);i++;}while(i<=10);getch();}
FOR LOOPWednesday, September 01, 2010PRADEEP DWIVEDI12It is two type ifsimple for loop.nested for loop.
FOR LOOPWednesday, September 01, 2010PRADEEP DWIVEDI13syntax:-for(initialization; test of the condition; increment/decrement){body of loop}
FOR LOOPWednesday, September 01, 2010PRADEEP DWIVEDI14in a for loop we always follows the following 4 steps sequentially.initialize the value.test the condition (if it is true)execute the body.increment/decrement the value.
FOR LOOPWednesday, September 01, 2010PRADEEP DWIVEDI15eg:-for(i=1;i<=10;i++){printf(“%d”,i);}124falsetrue3
prog15/* w.a.p. to check the entered number is a prime number or not */#include<stdio.h>#include<conio.h>#include<process.h>void main(){inti,num;clrscr();printf("Enter a number");scanf("%d",&num);for(i=2;i<num;i++){if(num%i==0){printf("number is not prime");getch();exit(0);}}printf("Number is prime");getch();}Wednesday, September 01, 201016PRADEEP DWIVEDI
SOME POINTSWednesday, September 01, 2010PRADEEP DWIVEDI17exit() is a predefine function with the help of it we move out or terminate from the program.it comes from process.h header file.when we use semicolon after the for loop that time it become a self executed loop and it execute when condition become false.we can have more than one initialization and the iteration in a for loop.eg. for(i=1;j=10;i<=10;i++; j--)
prog16Wednesday, September 01, 2010PRADEEP DWIVEDI18/*w.a.p. to calculate factorial of a given number */#include<stdio.h>#include<conio.h>void main(){intn,i,fac=1;clrscr();printf("Enter a number whose factorial is found :");scanf("%d",&n);for(i=n;i>=1;i--){fac=fac*i;}printf("factorial is:%d",fac);getch();}
prog17Wednesday, September 01, 2010PRADEEP DWIVEDI19/* w.a.p. to print fabonicai series 1 1 2 3 5 8 13 21 34 55 89 144 223 337 */#include<stdio.h>#include<conio.h>void main(){int num1,num2;clrscr();num1=num2=1;printf("%d\t",num2);while(num2<=200){printf("%d\t",num2);num2=num2+num1;num1=num2-num1;}getch();}
NESTED FOR LOOPWednesday, September 01, 2010PRADEEP DWIVEDI20nesting of loops, that is, one for statement with in another for statement is allowed in c.eg.		for(i=1;i<10;++i)  {		for(j=1;j<5;++j)      {       }        }outer loopinnerloop
prog18/* print this pattern	1	1 2 	1 2 3	1 2 3 4	1 2 3 4 5                       */#include<stdio.h>#include<conio.h>void main(){inti,j,num;clrscr();printf("Enter the number :");scanf("%d",&num);for(i=1;i<=num;i++){for(j=1;j<=i;j++){printf("%d\t",j);}printf("\n");}getch();}Wednesday, September 01, 201021PRADEEP DWIVEDI
prog19/* w.a.p. to print this pattern	* * * * *	* * * *	* * * 	* *	**/#include<stdio.h>#include<conio.h>void main(){inti,j;clrscr();for(i=1;i<=5;i++){for(j=5;j>=i;j--){printf("* ");}printf("\n");}getch();}Wednesday, September 01, 201022PRADEEP DWIVEDI
JUMPING IN THE LOOPWednesday, September 01, 2010PRADEEP DWIVEDI23For jumping in the loop we use two statements:breakcontinue
THE BREAK STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI24break is a keyword with the help of it we jump out from the loop.break is only use in the loop and switch case.
SYNTAX FOR BREAK STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI25A.            while(………..)        {		…….		……..      if (condition)		break;		…….		}exit from loop
SYNTAX FOR BREAK STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI26B.   do	{    ……..		if(condition)		break;		……….		……….		}		while(………);exit from loop
SYNTAX FOR BREAK STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI27C.	for(……………………………………)	{	---------   ---------      if(condition)		break;		-------		-------       }	------exit from loop
SYNTAX FOR BREAK STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI28D.	for(……………………………….)	{		………		for(……………………..)		{			if(condition)			break;			…			}			..			}exit from inner loop
prog20/*w.a.p. to determine wheather a number is prime or not(using break statement)*/#include<stdio.h>#include<conio.h>void main(){intnum,i;clrscr();printf("Enter a number: ");scanf("%d",&num);i=2;while(i<num){if(num%i==0){printf("number is not prime"); break;	}i++;}if(i==num)printf("number is prime");getch();}Wednesday, September 01, 201029PRADEEP DWIVEDI
goto STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI30goto keyword (statement) can transfers the control to any place in a program, it is useful to provide branching with in a loop.
SYNTAX OF GOTO STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI31while(………..)                                              (A){if(error)goto stop;…….…… if(condition)gotoabc;……….……….abc:……..}stop:…………jump with in loopexit from loop
SYNTAX OF GOTO STATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI32for(………………..)                      (B){……..……..	for(…….)	{	……	…....if(error)goto error;……..}…..}error:…..…….
SOME POINTSWednesday, September 01, 2010PRADEEP DWIVEDI33in syntax(A) if the condition is satisfied goto statement transfers control to the label abc:  stop:abc:  error:called label
prog21/* Demo for goto statement */#include<stdio.h>#include<conio.h>void main(){inti,j,k;clrscr();for(i=1;i<=3;i++){for(j=1;j<=3;j++){for(k=1;k<=3;k++){if(i==3&&j==3&&k==3)goto out;elseprintf("%d%d%d\n",i,j,k);}}}out:printf("out of the loop at last!");getch();}Wednesday, September 01, 201034PRADEEP DWIVEDI
SKIPPING A PART OF A LOOPWednesday, September 01, 2010PRADEEP DWIVEDI35For skipping a part of a loop we use continue keyword.the continue statement tells the compiler ,”skip the following statements and continue with next iteration”.the format of continue statement is simply:continue;continue:- is a keyword with the help of that we can move up at the beginning of the loop.
SYNTAXWednesday, September 01, 2010PRADEEP DWIVEDI36A.	while(test-condition)	{	continue;	……..	……..	}
SYNTAXWednesday, September 01, 2010PRADEEP DWIVEDI37B.		do		{	……….	……….		if(……..)		continue;		……..		……..			}	while(test-condition);
SYNTAXWednesday, September 01, 2010PRADEEP DWIVEDI38C. for(initialization;test-condition;increment)		{	…………..	…………..		if(………….)		continue;		……….		……….		}
prog22Wednesday, September 01, 2010PRADEEP DWIVEDI39/* Demo for break statement  */#include<stdio.h>#include<conio.h>void main(){inti;for(i=0;i<=10;i++){if(i==5)continue;printf("Er.pradeepdwivedi\n");}getch();}

C programming slide c04

  • 1.
    PREPARED BY-PRADEEP DWIVEDI(persuingB.TECH-IT) fromHINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY(MATHURA) MOB-+919027843806 E-MAIL-pradeep.it74@gmail.com C-PROGRAMMING SLIDE-4Wednesday, September 01, 20101PRADEEP DWIVEDI
  • 2.
    C-4TOPIC:-Loopbreak statementcontinue statementgoto statementWednesday, September 01, 20102PRADEEP DWIVEDI
  • 3.
    LOOPWednesday, September 01,2010PRADEEP DWIVEDI3when we want to repeat a particular task again and again that time we use the loop.loop is a control structure that repeats a group of sets in a program.there are three loops in c-while fordo while
  • 4.
    WHILE LOOPWednesday, September01, 2010PRADEEP DWIVEDI4syntax:-while(test-condition){body of the loop.}
  • 5.
    WHILE LOOPWednesday, September01, 2010PRADEEP DWIVEDI5the while loop is a entry controlled loop statement.the test condition is evaluated and if the condition is true then the body of loop is executed.after execution of the body the test condition is once again evaluated and if it is true the body is executed once again.this process of repeated execution of the body continues until the test condition finally becomes false and controlled is transferred out of the loop.
  • 6.
    FLOW CHART(WHILE LOOP)Wednesday,September 01, 2010PRADEEP DWIVEDI6
  • 7.
    PROG13Wednesday, September 01,2010PRADEEP DWIVEDI7/* w a p to print 1 to 10 */#include<stdio.h>#include<conio.h>void main(){inti;clrscr();i=1;while(i<=10){printf("%d\t",i);i++;}getch();}
  • 8.
    DO WHILE LOOPWednesday,September 01, 2010PRADEEP DWIVEDI8in while loop, makes a test of condition before the loop is executed.therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt.on a some situations it must be necessary to execute the body of the loop before the test is performed. such situation can be handled with the help of the do statement.
  • 9.
    DO WHILE LOOPWednesday,September 01, 2010PRADEEP DWIVEDI9syntax:-do{body of the loop (true)}while(test-condition);
  • 10.
    DO WHILE LOOPWednesday,September 01, 2010PRADEEP DWIVEDI10in the do while loop it first execute the body and then checks the condition.so if the condition is initially false, it execute atleast once.
  • 11.
    prog14Wednesday, September 01,2010PRADEEP DWIVEDI11/* w.a.p. to print 1 TO 10 digit */#include<stdio.h>#include<conio.h>void main(){inti;clrscr();i=1;do{printf("%d\t",i);i++;}while(i<=10);getch();}
  • 12.
    FOR LOOPWednesday, September01, 2010PRADEEP DWIVEDI12It is two type ifsimple for loop.nested for loop.
  • 13.
    FOR LOOPWednesday, September01, 2010PRADEEP DWIVEDI13syntax:-for(initialization; test of the condition; increment/decrement){body of loop}
  • 14.
    FOR LOOPWednesday, September01, 2010PRADEEP DWIVEDI14in a for loop we always follows the following 4 steps sequentially.initialize the value.test the condition (if it is true)execute the body.increment/decrement the value.
  • 15.
    FOR LOOPWednesday, September01, 2010PRADEEP DWIVEDI15eg:-for(i=1;i<=10;i++){printf(“%d”,i);}124falsetrue3
  • 16.
    prog15/* w.a.p. tocheck the entered number is a prime number or not */#include<stdio.h>#include<conio.h>#include<process.h>void main(){inti,num;clrscr();printf("Enter a number");scanf("%d",&num);for(i=2;i<num;i++){if(num%i==0){printf("number is not prime");getch();exit(0);}}printf("Number is prime");getch();}Wednesday, September 01, 201016PRADEEP DWIVEDI
  • 17.
    SOME POINTSWednesday, September01, 2010PRADEEP DWIVEDI17exit() is a predefine function with the help of it we move out or terminate from the program.it comes from process.h header file.when we use semicolon after the for loop that time it become a self executed loop and it execute when condition become false.we can have more than one initialization and the iteration in a for loop.eg. for(i=1;j=10;i<=10;i++; j--)
  • 18.
    prog16Wednesday, September 01,2010PRADEEP DWIVEDI18/*w.a.p. to calculate factorial of a given number */#include<stdio.h>#include<conio.h>void main(){intn,i,fac=1;clrscr();printf("Enter a number whose factorial is found :");scanf("%d",&n);for(i=n;i>=1;i--){fac=fac*i;}printf("factorial is:%d",fac);getch();}
  • 19.
    prog17Wednesday, September 01,2010PRADEEP DWIVEDI19/* w.a.p. to print fabonicai series 1 1 2 3 5 8 13 21 34 55 89 144 223 337 */#include<stdio.h>#include<conio.h>void main(){int num1,num2;clrscr();num1=num2=1;printf("%d\t",num2);while(num2<=200){printf("%d\t",num2);num2=num2+num1;num1=num2-num1;}getch();}
  • 20.
    NESTED FOR LOOPWednesday,September 01, 2010PRADEEP DWIVEDI20nesting of loops, that is, one for statement with in another for statement is allowed in c.eg. for(i=1;i<10;++i) { for(j=1;j<5;++j) { } }outer loopinnerloop
  • 21.
    prog18/* print thispattern 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 */#include<stdio.h>#include<conio.h>void main(){inti,j,num;clrscr();printf("Enter the number :");scanf("%d",&num);for(i=1;i<=num;i++){for(j=1;j<=i;j++){printf("%d\t",j);}printf("\n");}getch();}Wednesday, September 01, 201021PRADEEP DWIVEDI
  • 22.
    prog19/* w.a.p. toprint this pattern * * * * * * * * * * * * * * **/#include<stdio.h>#include<conio.h>void main(){inti,j;clrscr();for(i=1;i<=5;i++){for(j=5;j>=i;j--){printf("* ");}printf("\n");}getch();}Wednesday, September 01, 201022PRADEEP DWIVEDI
  • 23.
    JUMPING IN THELOOPWednesday, September 01, 2010PRADEEP DWIVEDI23For jumping in the loop we use two statements:breakcontinue
  • 24.
    THE BREAK STATEMENTWednesday,September 01, 2010PRADEEP DWIVEDI24break is a keyword with the help of it we jump out from the loop.break is only use in the loop and switch case.
  • 25.
    SYNTAX FOR BREAKSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI25A. while(………..) { ……. …….. if (condition) break; ……. }exit from loop
  • 26.
    SYNTAX FOR BREAKSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI26B. do { …….. if(condition) break; ………. ………. } while(………);exit from loop
  • 27.
    SYNTAX FOR BREAKSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI27C. for(……………………………………) { --------- --------- if(condition) break; ------- ------- } ------exit from loop
  • 28.
    SYNTAX FOR BREAKSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI28D. for(……………………………….) { ……… for(……………………..) { if(condition) break; … } .. }exit from inner loop
  • 29.
    prog20/*w.a.p. to determinewheather a number is prime or not(using break statement)*/#include<stdio.h>#include<conio.h>void main(){intnum,i;clrscr();printf("Enter a number: ");scanf("%d",&num);i=2;while(i<num){if(num%i==0){printf("number is not prime"); break; }i++;}if(i==num)printf("number is prime");getch();}Wednesday, September 01, 201029PRADEEP DWIVEDI
  • 30.
    goto STATEMENTWednesday, September01, 2010PRADEEP DWIVEDI30goto keyword (statement) can transfers the control to any place in a program, it is useful to provide branching with in a loop.
  • 31.
    SYNTAX OF GOTOSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI31while(………..) (A){if(error)goto stop;…….…… if(condition)gotoabc;……….……….abc:……..}stop:…………jump with in loopexit from loop
  • 32.
    SYNTAX OF GOTOSTATEMENTWednesday, September 01, 2010PRADEEP DWIVEDI32for(………………..) (B){……..…….. for(…….) { …… …....if(error)goto error;……..}…..}error:…..…….
  • 33.
    SOME POINTSWednesday, September01, 2010PRADEEP DWIVEDI33in syntax(A) if the condition is satisfied goto statement transfers control to the label abc: stop:abc: error:called label
  • 34.
    prog21/* Demo forgoto statement */#include<stdio.h>#include<conio.h>void main(){inti,j,k;clrscr();for(i=1;i<=3;i++){for(j=1;j<=3;j++){for(k=1;k<=3;k++){if(i==3&&j==3&&k==3)goto out;elseprintf("%d%d%d\n",i,j,k);}}}out:printf("out of the loop at last!");getch();}Wednesday, September 01, 201034PRADEEP DWIVEDI
  • 35.
    SKIPPING A PARTOF A LOOPWednesday, September 01, 2010PRADEEP DWIVEDI35For skipping a part of a loop we use continue keyword.the continue statement tells the compiler ,”skip the following statements and continue with next iteration”.the format of continue statement is simply:continue;continue:- is a keyword with the help of that we can move up at the beginning of the loop.
  • 36.
    SYNTAXWednesday, September 01,2010PRADEEP DWIVEDI36A. while(test-condition) { continue; …….. …….. }
  • 37.
    SYNTAXWednesday, September 01,2010PRADEEP DWIVEDI37B. do { ………. ………. if(……..) continue; …….. …….. } while(test-condition);
  • 38.
    SYNTAXWednesday, September 01,2010PRADEEP DWIVEDI38C. for(initialization;test-condition;increment) { ………….. ………….. if(………….) continue; ………. ………. }
  • 39.
    prog22Wednesday, September 01,2010PRADEEP DWIVEDI39/* Demo for break statement */#include<stdio.h>#include<conio.h>void main(){inti;for(i=0;i<=10;i++){if(i==5)continue;printf("Er.pradeepdwivedi\n");}getch();}