3 control statements ppt

34
Control Statements in Control Statements in C C

Upload: api-3728136

Post on 11-Apr-2015

5.167 views

Category:

Documents


1 download

DESCRIPTION

PPT3

TRANSCRIPT

Page 1: 3 Control Statements PPT

Control Statements in Control Statements in ‘‘CC’’

Page 2: 3 Control Statements PPT

Control StatementsControl Statements•• Selection StatementsSelection Statements

–Using if and if...else–Nested if Statements–Using switch Statements–Conditional Operator

•• Repetition StatementsRepetition Statements–Looping: while, do-while, and for

–Nested loops–Using break and continue

Page 3: 3 Control Statements PPT

Selection StatementsSelection Statements• if Statements

• switch Statements

• Conditional Operators

Page 4: 3 Control Statements PPT

ifif StatementsStatementsif (Condition) {

statement(s);}

Example:if (i > 0) {

printf("i = %d “, i );}

Condition ?

statement1

statement2

statement3

true

false

Page 5: 3 Control Statements PPT

CautionCautionAdding a semicolon at the end of an if clause is a common mistake.if (radius >= 0);{

area = radius*radius*PI;printf ("The area for the circle of radius %d is =%d“, radius,area);

}This mistake is hard to find, because it is neither

a compilation error nor a runtime error, it is a logic error. This error often occurs when you use the next-line block style.

Wrong

Page 6: 3 Control Statements PPT

The The if...elseif...else StatementStatement

if (condition) {

statement(s)-for-the-true-case;}else {statement(s)-for-the-false-case;

}

Page 7: 3 Control Statements PPT

if...elseif...else ExampleExample

if (radius >= 0) {

area = radius*radius*PI; printf("The area for the circle of radius

%d is =%d" , radius, area);}else {

printf(“Radius can not be Negative ");}

Page 8: 3 Control Statements PPT

Multiple Alternative if StatementsMultiple Alternative if Statements

if (score >= 90)grade = ‘A’;

else if (score >= 80)grade = ‘B’;

elseif (score >= 70)

grade = ‘C’;else

if (score >= 60)grade = ‘D’;

elsegrade = ‘F’;

if (score >= 90)grade = ‘A’;

else if (score >= 80)grade = ‘B’;

else if (score >= 70) grade = ‘C’;

else if (score >= 60)grade = ‘D’;

else grade = ‘F’;

Page 9: 3 Control Statements PPT

NoteNoteThe else clause matches the most recent if clause in the same block. For example, the following statement

int i = 1; int j = 2; int k = 3;if (i > j)

if (i > k)printf("A");else printf("B");

Other combination is :int i = 1; int j = 2; int k = 3;if (i < j) {

if (i > k)printf("A");

}else

printf("B");

Page 10: 3 Control Statements PPT

switchswitch StatementsStatementsswitch (variable-name){ case value1:

Execute this;break;

case value2: Execute this;break;

case valueN: Execute this;break;

default: Execute this;

}

Page 11: 3 Control Statements PPT

switchswitch Statement Flow ChartStatement Flow Chart

Variablevalue1

default

Next Statement

Execute this; Execute this; Execute this; Execute this;

value2 valueN

Page 12: 3 Control Statements PPT

switchswitch Statement RulesStatement RulesThe switch-expression must yield a value of char or

int type and must always be enclosed in parentheses.The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. (The case statements are executed in sequential order.)

The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement.

If the break statement is not present, the next case statement will be executed.

Page 13: 3 Control Statements PPT

switchswitch Statement Rules, Statement Rules, contcont……....

The default case, which is optional, can be used to perform actions when none of the specified cases is true.

The order of the cases (including the default case) does not matter. However, it is a good programming style to follow the logical sequence of the cases and place the default case at the end.

Page 14: 3 Control Statements PPT

CautionCautionDo not forget to use a break statement when one is needed. For example, the following code always displays Wrong number of years regardless of what num is. Suppose the num is 15. The statement rate = 8.50 is executed, then the statement rate = 9.0, and finally the statement, printf("Wrong number of years").

switch (num) {case 7: rate = 7.25;case 15: rate = 8.50;case 30: rate = 9.0;default: printlf("Wrong number of years");

}

Page 15: 3 Control Statements PPT

Example: Example: switchswitch--casecaseint w = 20;switch(w){case 10: printf(“First”);

break;case 20: printf(“Second”);

break;case 30: printf(“Third”);

break;default: printf(“Wrong value…”);

}

Page 16: 3 Control Statements PPT

Conditional OperatorConditional Operator

if (x > 0) y = 1else y = -1;

is equivalent to

y = (x > 0) ? 1 : -1;

Ternary operator

(condition) ? exp1 : exp2

Page 17: 3 Control Statements PPT

Conditional OperatorConditional Operator

if (num % 2 == 0)printf(“%d is even”,num);

else printf(“%d is odd”,num);

(num%2==0)?printf(“even”):printf(“odd”);

Page 18: 3 Control Statements PPT

RepetitionsRepetitions

while Loops

do-while Loopsfor Loops

break and continue

Page 19: 3 Control Statements PPT

whilewhile Loop Flow ChartLoop Flow Chart

false

true

Statement(s)

Next Statement

Continuation condition?

while (continuation-condition)

{

// loop-body;

}

Page 20: 3 Control Statements PPT

whilewhile Loop Flow Chart, cont.Loop Flow Chart, cont.

int i = 0;while (i < 100){printf("Welcome to C!");i++;

}

false

true

printf("Welcome to C!"); i++;

Next Statement

(i < 100)

i = 0;

Page 21: 3 Control Statements PPT

dodo--whilewhile LoopLoop

false

true

Statement(s)

Next Statement

Continue condition?do

{

// Loop body;

} while (continue-condition);

Page 22: 3 Control Statements PPT

forfor Loop Loop

Initialization

false

true

Increment Decrement

Statement(s)(loop-body)

Next Statement

Continuation condition?

for (initialization; condition; increment/decrement){

//loop body;}

Page 23: 3 Control Statements PPT

forfor Loop ExampleLoop Example

i<100?

true

false

i++

i = 0

Next Statem ent

printf(“W elcome to C”); int i;for (i=0;i<100;i++){printf("Welcome to C");

}

Page 24: 3 Control Statements PPT

CautionCaution

Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below:

for (int i=0; i<10; i++);{

printf("i is %d“,i);}

Wrong

Page 25: 3 Control Statements PPT

Caution, cont.Caution, cont.Similarly, the following loop is also wrong:

int i=0; while (i<10);{

printf("i is %d“,i);i++;

}

In the case of the do loop, the following semicolon is needed to end the loop.

int i=0; do{

printf("i is %d“,i);i++;

} while (i<10);

Wrong

Correct

Page 26: 3 Control Statements PPT

Which Loop to Use?Which Loop to Use?The three forms of loop statements, while, do, and

for, are expressively equivalent; that is, you can write a loop in any of these three forms.

It is recommend that you use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.

Page 27: 3 Control Statements PPT

The The breakbreak KeywordKeyword

false

true

Statement(s)

Next Statement

Continuation condition?

Statement(s)

break

Page 28: 3 Control Statements PPT

Example: Example: breakbreak statementstatementint a = 10;while( a >= 0 ){

printf(“\nValue of a = %d”,a);a--;if(a==5)break;

}Output:Value of a = 10Value of a = 9Value of a = 8Value of a = 7Value of a = 6

Page 29: 3 Control Statements PPT

The The continuecontinue KeywordKeyword

false

true

Statement(s)

Next Statement

Continue condition?

Statement(s)

continue

Page 30: 3 Control Statements PPT

Example: Example: continuecontinue statementstatementint a = 6;while( a >= 0 ){

a--;if(a==3)continue;printf(“\nValue of a = %d”,a);

}Output:Value of a = 5Value of a = 4Value of a = 2Value of a = 1Value of a = 0

Page 31: 3 Control Statements PPT

Test your skillsTest your skills……

1. Find the largest number fromthree inputted numbers.

2. Find whether the accepted yearis leap year or not ?

If – else construct

Page 32: 3 Control Statements PPT

Test your skillsTest your skills……

1. Display all the even numbers from 20 to 300.

2. Find whether the entered number is prime or not ?

3. Calculate squares of all the numbers from -10 to 50.

while loop construct

Page 33: 3 Control Statements PPT

Test your skillsTest your skills……

1. Calculate the factorial of the given number n.

2. Calculate sum of digits of given number.

3. Calculate squares of all the numbers from -10 to 50.

for loop construct

Page 34: 3 Control Statements PPT

Created By,Created By,

• Mr. Tushar B Kute,Lecturer in Information Technology,K. K. Wagh Polytechnic, [email protected]