control statement in c
DESCRIPTION
Control statement in cTRANSCRIPT
![Page 1: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/1.jpg)
![Page 2: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/3.jpg)
Control statement
in cShanu k [email protected]/shanunnitwitter.com/shanuin.linkedin.com/in/shanu9656153432
![Page 4: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/4.jpg)
• C provides two sytles of flow control:• Branching• Looping
![Page 5: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/5.jpg)
If statement
• It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
![Page 6: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/6.jpg)
If(test expression)Statement;Or If (test expression){Block of statements1;}Else{Statement2;}
![Page 7: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/7.jpg)
Switch statement
• A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
![Page 8: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/8.jpg)
Switch( expression){ case 1Statement 1Break;}Case2Statement 2Break;}
![Page 9: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/9.jpg)
• Depending on the position of control statement in c,control structure may be classified
• Entry_ controlled loop• Exit _controlled loop
![Page 10: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/10.jpg)
False
true
Entry controlled loop exit controlled loop
Test conditio
n ?
Body of the loop
Body of the loop
Test conditio
n ?
![Page 11: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/11.jpg)
• C language provides three constructs for perfoming loop operations
• While statement
• Do statements
• For statements
![Page 12: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/12.jpg)
While(test condition){
body of the loop}
![Page 13: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/13.jpg)
……………………… ……………………… int_sum=0; int_n=1; while(int_n<=10) { int_sum=int_sum+int_n; int_n=int_n+1; } printf(“sum=%d\n”,int_sum); …………………………………………..
![Page 14: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/14.jpg)
Do statement
do{ Body of the loop} While(test condition)
![Page 15: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/15.jpg)
int_i=1; int_sum=0; do { int_sum=int_sum+I i=i+2; } while(sum<40||i<10); printf(“%d %d\n,I,sum);
![Page 16: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/16.jpg)
For statements
For(intialization;testcondition;icrement){
body of the loop}
![Page 17: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/17.jpg)
• Intialization of control variable done first using assignment statement• The value of control variable tested
using test condition,ie reletional expression such as i>10,that determine when the loop will exit• If the condition is true ,the body of
loop executed,otherwise terminated
![Page 18: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/18.jpg)
int_sum=0; for(int_n=1;int_n<=10;int_n++) { int_sum=int_sum+int_n; } printf(“sum=%d\n”,int_sum);
![Page 19: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/19.jpg)
additional features of for loop
1. More than one variable can be intialized atb a time2. More than one variable incremented at a time3. The thest condition may have compound relation4. One or more sections can be omitted ,if necessary eg: m=5; for(;m!=100;) { printf(“%d\n”,m) m=m+5; }
![Page 20: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/20.jpg)
Nesting of for loop
For(i=0;i<n;i++){ ………………………………For(j=0;j<n-1;j++){ ………………………………}}
![Page 21: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/21.jpg)
Jumping out of a loop
• Exit from a loop using break statement if a break statement encounteredd in a
loop,the loop will immidiatly exited and the program continues with the statement immidiatly following loop;ie break will exit only a single loop
![Page 22: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/22.jpg)
Eg: while(test condition) { …………………………….. ……………………………… if(condition) break;
![Page 23: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/23.jpg)
Skipping a part of loop
Another statement ‘continue’,• It tells the compiler skip the following statements
and continue with next iterationEg:While (test condition){………………………..If(…………)Continue;
![Page 24: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/24.jpg)
???
![Page 25: Control statement in c](https://reader035.vdocument.in/reader035/viewer/2022062312/555e35f5d8b42aad058b4811/html5/thumbnails/25.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]