Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
C programming slide c04
1. 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-4 Wednesday, September 01, 2010 1 PRADEEP DWIVEDI
2. C-4 TOPIC:- Loop break statement continue statement go to statement Wednesday, September 01, 2010 2 PRADEEP DWIVEDI
3. LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 3 when 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 for do while
4. WHILE LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 4 syntax:- while(test-condition) { body of the loop. }
5. WHILE LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 5 the 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.
7. PROG13 Wednesday, September 01, 2010 PRADEEP DWIVEDI 7 /* 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",i); i++; } getch(); }
8. DO WHILE LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 8 in 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 LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 9 syntax:- do { body of the loop (true) } while(test-condition);
10. DO WHILE LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 10 in 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. prog14 Wednesday, September 01, 2010 PRADEEP DWIVEDI 11 /* w.a.p. to print 1 TO 10 digit */ #include<stdio.h> #include<conio.h> void main() { inti; clrscr(); i=1; do { printf("%d",i); i++; } while(i<=10); getch(); }
12. FOR LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 12 It is two type if simple for loop. nested for loop.
13. FOR LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 13 syntax:- for(initialization; test of the condition; increment/decrement) { body of loop }
14. FOR LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 14 in 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.
16. 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, 2010 16 PRADEEP DWIVEDI
17. SOME POINTS Wednesday, September 01, 2010 PRADEEP DWIVEDI 17 exit() 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. prog16 Wednesday, September 01, 2010 PRADEEP DWIVEDI 18 /*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(); }
20. NESTED FOR LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 20 nesting 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 loop inner loop
23. JUMPING IN THE LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 23 For jumping in the loop we use two statements: break continue
24. THE BREAK STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 24 break 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 BREAK STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 25 A. while(………..) { ……. …….. if (condition) break; ……. } exit from loop
26. SYNTAX FOR BREAK STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 26 B. do { …….. if(condition) break; ………. ………. } while(………); exit from loop
27. SYNTAX FOR BREAK STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 27 C. for(……………………………………) { --------- --------- if(condition) break; ------- ------- } ------ exit from loop
28. SYNTAX FOR BREAK STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 28 D. for(……………………………….) { ……… for(……………………..) { if(condition) break; … } .. } exit from inner loop
29. 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, 2010 29 PRADEEP DWIVEDI
30. goto STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 30 goto 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 GOTO STATEMENT Wednesday, September 01, 2010 PRADEEP DWIVEDI 31 while(………..) (A) { if(error) goto stop; ……. …… if(condition) gotoabc; ………. ………. abc: …….. } stop: ………… jump with in loop exit from loop
33. SOME POINTS Wednesday, September 01, 2010 PRADEEP DWIVEDI 33 in syntax(A) if the condition is satisfied goto statement transfers control to the label abc: stop: abc: error: called label
34. 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; else printf("%d%d%d",i,j,k); } } } out: printf("out of the loop at last!"); getch(); } Wednesday, September 01, 2010 34 PRADEEP DWIVEDI
35. SKIPPING A PART OF A LOOP Wednesday, September 01, 2010 PRADEEP DWIVEDI 35 For 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.