Control statement in c
-
Upload
baabtracom-first-coding-school-in-india -
Category
Education
-
view
2.008 -
download
4
description
Transcript of Control statement in c
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
Control statement
in cShanu k [email protected]/shanunnitwitter.com/shanuin.linkedin.com/in/shanu9656153432
• C provides two sytles of flow control:• Branching• Looping
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.
If(test expression)Statement;Or If (test expression){Block of statements1;}Else{Statement2;}
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.
Switch( expression){ case 1Statement 1Break;}Case2Statement 2Break;}
• Depending on the position of control statement in c,control structure may be classified
• Entry_ controlled loop• Exit _controlled loop
False
true
Entry controlled loop exit controlled loop
Test conditio
n ?
Body of the loop
Body of the loop
Test conditio
n ?
• C language provides three constructs for perfoming loop operations
• While statement
• Do statements
• For statements
While(test condition){
body of the loop}
……………………… ……………………… 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); …………………………………………..
Do statement
do{ Body of the loop} While(test condition)
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);
For statements
For(intialization;testcondition;icrement){
body of the loop}
• 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
int_sum=0; for(int_n=1;int_n<=10;int_n++) { int_sum=int_sum+int_n; } printf(“sum=%d\n”,int_sum);
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; }
Nesting of for loop
For(i=0;i<n;i++){ ………………………………For(j=0;j<n-1;j++){ ………………………………}}
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
Eg: while(test condition) { …………………………….. ……………………………… if(condition) break;
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;
???
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]