Control statement in c

25

description

Control statement in c

Transcript of Control statement in c

Page 1: Control statement in c
Page 2: 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

Page 3: Control statement in c

Control statement

in cShanu k [email protected]/shanunnitwitter.com/shanuin.linkedin.com/in/shanu9656153432

Page 4: Control statement in c

• C provides two sytles of flow control:• Branching• Looping

Page 5: Control statement in c

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

If(test expression)Statement;Or If (test expression){Block of statements1;}Else{Statement2;}

Page 7: Control statement in c

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

Switch( expression){ case 1Statement 1Break;}Case2Statement 2Break;}

Page 9: Control statement in c

• 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

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

• C language provides three constructs for perfoming loop operations

• While statement

• Do statements

• For statements

Page 12: Control statement in c

While(test condition){

body of the loop}

Page 13: Control statement in c

……………………… ……………………… 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

Do statement

do{ Body of the loop} While(test condition)

Page 15: Control statement in c

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

For statements

For(intialization;testcondition;icrement){

body of the loop}

Page 17: Control statement in c

• 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

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

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

Nesting of for loop

For(i=0;i<n;i++){ ………………………………For(j=0;j<n-1;j++){ ………………………………}}

Page 21: Control statement in c

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

Eg: while(test condition) { …………………………….. ……………………………… if(condition) break;

Page 23: Control statement in c

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

???

Page 25: Control statement in c

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]