Decision control

Preview:

DESCRIPTION

 

Citation preview

CProgramming Language

By:

Yogendra Palyogendra@learnbywatch.com

Dedicated to My mother and Father

THIS IS DECISION CONTROL

Keep Watching Keep Learning

t Keep your notebook with you.

yWrite important point and questions that comes in your mind

Solve Mind band exercise.

Ask Questions by call or SMS or by mail

CRewind when not clear

2

Need

• Consider a problem:

– Write a program which take a number as a input and print it if it is an even number.

– This problem can not solved by calculation or arithmetic expression.

– We need to take decision here.

3

Decision statements

• To change the control flow of a program weuse:

– Branching statements : cause one section of codeto be executed or not executed, depending on aconditional clause.

– Looping statements : used to repeat a section ofcode a number of times or until some conditionoccurs.

4

Branching Statements : 1

• if Statements– General form:

• if Condition is true (non-zero), the statement will be executed.

• if the condition is false (0), the statement will not be executed.

if(condition)

statement;

5

An Example

• Write a program which take a number as ainput and print it if it is an even number.

• == is a relational operator that represents“equal to”.– Eg. if (number % 2 == 0)

printf(“It is an even number”);

• This statement reads– “if the number%2 is equal to zero print ‘it is an even

number’ ”

6

Relational operator

Operator Meaning

<= Less than OR equal to

< Less than

>= Greater than or equal to

= = Equal

!= Not equal

7

Logical Operator

Operator Meaning

|| or

&& and

8

Multiple Statements

• Put all statements inside the curly { } braces.

if(condition)

{

statement 1;

statement 2;

statement 3;

}

9

Problems

• Input cost price and selling price of an itemthrough the keyboard and write a program todetermine the profit or loss.

• Write a program to determine whether theyear (input from keyboard) is a leap or not.

• Write a program that take a character as ainput and determine whether the characterentered is a capital letter or small letter or anumber.

10

Branching Statement : 2

• else statement:

• An alternate form of the if statement:-

• Write a program which take a number as ainput and print it if it is an even numberotherwise print “you entered ODD number”.

if(condition)

statement;

else

statement;

If condition is true

execute this statement

otherwise

execute this statement

11

Nested if - else

if(condition)

if(condition)

statement;

else

statement;

if(condition)

{

if(condition)

statement;

}

else

statement;if(condition)

{

if(condition)

statement;

else

statement;

}

12

Nested if - else

if(condition)

statement;

else if (condition)

statement;

else if (condition)

statement;

else

statement;

if(condition){

statement 1;

statement 2;

}

else if (condition){

statement 1;

statement 2;

}

else {

statement 1;

statement 2;

}

13

A problem

• Write a program which take an operator (+, -, * or /) as an input and perform the operation according to the input.

• Like:

– a = 10;

– b = 2;

– Input = ‘+’ output = 12;

– Input = ‘-’ output = 8;

14

Branching Statement : 3

• switch statement

– An alternative to the chain of if / else statements.

• General form: switch (expression)

{

case constant_1:

statement;

break;

case constant_2:

statement;

default:

statement;

break;

}

Case label

15

Rules to use switch

• Duplicate labels are not allowed.

• Expression must evaluate an integer, characteror enumeration.

• Case labels can be in any order and must beconstant.

• Default label can be put anywhere in theswitch.

• No two case have the same value.

16

If-else / switch

• Break statement causes the program to go to the end of the switch.

if(operator == „+‟){

result += value;

}else if (operator == „-‟){

result -= value;

}else {

printf(“Unknown operator %c \n”,operator);

}

switch (operator)

{

case „+‟:

result += value;

break;

case „-‟:

result -= value;

break;

default:

printf(“Unknown error %c \n”, operator);

}

17

Conditional Operator

• Works as if-else statement.

• Condition ? Statement 1 : Statement 2;

• If condition is true statement 1 will execute other wise statement 2.

– ( a <= 5) ? 0 : 100;

18

Operator PrecedenceOperator Operation Precendence

( ) Parentheses Evaluate First,Innermost first,Left to right.

-, +, --, ++, sizeof, (type) Unary operators Right to left

*, / or % Multiplication, Division ormodulus

Left to right.

+ or - Addition and subtraction Left to right.

<, <=, >, >= Relational operators Left to right.

== != Equality operators Left to right.

&& Logical and Left to right.

|| Logical or Left to right.

? : Conditional operator Right to left

= Assignment (Multiple) Right to left.

19

• Write a program that accept a character fromkeyboard and convert as follows:

– If it is uppercase then convert in lowercase letter.

– If it is lowercase then convert in uppercase letter.

– If it is other then alphabet print error message.

• Write a program which take two sides of arectangle and determine whether the givensides are of a rectangle or a square.

Mind Bend

20

NEXT IS LOOPSKeep Watching Keep Learning

To get complete benefit of this tutorial solve all the quiz on

www.learnbywatch.com

For any problem in this tutorial mail me at

yogendra@learnbywatch.com

with the subject “C”

For Other information mail at

info@learnbywatch.com

21

Recommended