Control structures in C

36

Transcript of Control structures in C

Page 1: Control structures in C
Page 2: Control structures in C

Mohammed Razi Krazikallayi @gmail.comwww.facebook.com/

razikallayitwitter.com/razikallayiin.linkedin.com/in/razikallayi9746730324

Control Structures in C

Page 3: Control structures 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 4: Control structures in C

Flow of control

• Unless specified, the order of statement execution is linear: one statement after another in sequence

• A control structure is any mechanism that departs from the default straight-line execution.

Page 5: Control structures in C

Control Structures

SelectionIf statementsSwitch cases

ItrationFor loopWhile loop

JumpGotocall/returnBreakcontinue

2

1

3

Page 6: Control structures in C

Selection statements• Used to choose which statement to be executed next

• Therefore they are sometimes called conditional statements

• Conditional statements give us the power to make basic decisions

• The C conditional statements are the:

– if statement– if-else statement– switch statement

Page 7: Control structures in C

The if StatementThe if statement has the following syntax:

if ( condition ) statement;

• if is a C reserved keyword.

• The condition must be a boolean expression. It is evaluated to

either true or false.

• If the condition is true, the statement is executed.

• If it is false, the statement is skipped.

Page 8: Control structures in C

The if-else Statement

• If the condition is true, statement1 is executed; if the condition is false, statement2 is execute

if ( condition ) statement1;else statement2;

Page 9: Control structures in C

Consider the following example,

int grade=76;if ( grade >= 60 ) printf( "Passed\n");else printf( "Failed\n");

The above code will produce an output:PassedSince the true statement only executes

Page 10: Control structures in C

• An if statement which is within another if or else is called nested if.

• Braces can be used to specify the if statement to which an else clause belongs

Nested if statements

Page 11: Control structures in C

Syntax of nested if:

if(condition1){ if(condition2) do this; else //this else with condition2 { do this; and this; }}else //this else with condition1 do this;

Note that in C an else statement always refers to the nearest if that is within the same block of else.

Page 12: Control structures in C

An example for nested if:

if(avg>=90) printf("Student %s gets an A grade",name); else if(avg>=80 &&avg<90) printf("Student %s gets a B grade",name); else if(avg>=70 &&avg<0) printf("Student %s gets a C grade",name); else if(avg>=60 &&avg<70) printf("Student %s gets a D grade",name); else if(avg<60) printf("Student %s failed");

Nested if (contd…)

Page 13: Control structures in C

The Switch statement

• The switch statement provides another way to decide which statement to execute next

• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases

• Each case contains a value and a list of statements

• The flow of control transfers to statement associated with the first case value that matches

Page 14: Control structures in C

• Often a break statement is used as the last statement in each case's statement list

• A break statement causes control to transfer to the end of the switch statement

• If a break statement is not used, the flow of control will continue into the next case

• Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

The Switch statement(Contd…)

Page 15: Control structures in C

• A switch statement can have an optional default case

• If the default case is present, control will transfer to it if no other case value matches

• If there is no default case, and no other value matches, control falls through to the statement after the switch

The Switch statement(Contd…)

Page 16: Control structures in C

• The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,) or a char

• It cannot be a floating point value (float or double)

• The implicit test condition in a switch statement is equality

• You cannot perform relational checks with a switch statement

The Switch statement(Contd…)

Page 17: Control structures in C

The general syntax of a switch statement is:

switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 default:

statement}

switchandcaseare

reservedwords

If expressionmatches value2,control jumpsto here

The Switch statement(Contd…)

Page 18: Control structures in C

switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break;}

The Switch statement(Contd…)

An example for switch statement:

Page 19: Control structures in C

Iteration Statements

• Also known as loops• It allow a set of instructions to be

executed until a certain condition is met.

• The loop structures available in C are:• For loop• While loop• Do while loop

Page 20: Control structures in C

The for Loop• The syntax of for statement in C:

for (initialization expression;loop repetition condition;update expression)

{ statement}

• The initialization expression set the initial value of the loop control variable.

• The loop repetition condition test the value of the loop control variable.

• The update expression update the loop control variable.

Page 21: Control structures in C

Nested Loops• Nested loops consist of an outer loop with one or more

inner loops.• e.g.,

for (i=1;i<=100;i++){for(j=1;j<=50;j++){

…}

}• The above loop will run for 100*50 iterations.

5-22

Inner loop

Outer loop

Page 22: Control structures in C

For loop: An example

/*For loop to print even numbers below 100*/

for(i=2;i<100;i=i+2) { printf("%d,",i); }

Page 23: Control structures in C

The While loop

• The syntax of do-while statement in C:

dostatement

while (loop repetition condition);

• The statement is first executed.• If the loop repetition condition is true, the statement

is repeated.• Otherwise, the loop is exited.

Page 24: Control structures in C

An Example of the do-while Loop

/* Find even number input */do{

printf(“Enter a value: ”);scanf(“%d”, &num);

}while (num % 2 !=0)

5-25

This loop will repeat if the user inputs odd number.

Page 25: Control structures in C

Jump Statements

• Also called unconditional branching.• It means transfer of control to a specified

statement.• C has four statements that perform

unconditional branch– Call/return– Goto– Break– Continue

Page 26: Control structures in C

The return statement• Used to return fro a function• It returns to the point at which the function is

called.• It can have a value with it.• General form: return (expression);• The expression is optional• Function can have more than one return

statement, but will return at the first return statement

Page 27: Control structures in C

The goto statement

• Allows to transfer control to any other statement in the function.

• General form label:statement goto label;

Page 28: Control structures in C

The break statement

• It can be used to terminate a case in switch statement.

• Can used to force immediate loop termination by checking a condition

Page 29: Control structures in C

• Here, loop will terminate when i=10

for(i=0;i<=100;i++) {

if(i==10) { break; }

printf(“%d”,i); }

Page 30: Control structures in C

The Continue Statement

• It allows to skip the current iteration and hence wont run the remaining statements in the loop.

Page 31: Control structures in C

for(i=0;i<=100;i++) {

if(i==10) { break; }

printf(“%d”,i); }

• Here, loop will skip the printf statement when i=10 and incement i to 11

Page 32: Control structures in C

thank you

Page 33: Control structures in C

Doubts…?

?

Page 34: Control structures in C

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.

Page 35: Control structures in C

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 36: Control structures in C

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

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.Email: [email protected]

Contact Us