21
C Programming Language By: Yogendra Pal [email protected] Dedicated to My mother and Father

Decision control

  • View
    1.480

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Decision control

CProgramming Language

By:

Yogendra [email protected]

Dedicated to My mother and Father

Page 2: Decision control

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

Page 3: Decision control

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

Page 4: Decision control

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

Page 5: Decision control

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

Page 6: Decision control

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

Page 7: Decision control

Relational operator

Operator Meaning

<= Less than OR equal to

< Less than

>= Greater than or equal to

= = Equal

!= Not equal

7

Page 8: Decision control

Logical Operator

Operator Meaning

|| or

&& and

8

Page 9: Decision control

Multiple Statements

• Put all statements inside the curly { } braces.

if(condition)

{

statement 1;

statement 2;

statement 3;

}

9

Page 10: Decision control

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

Page 11: Decision control

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

Page 12: Decision control

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

Page 13: Decision control

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

Page 14: Decision control

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

Page 15: Decision control

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

Page 16: Decision control

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

Page 17: Decision control

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

Page 18: Decision control

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

Page 19: Decision control

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

Page 20: Decision control

• 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

Page 21: Decision control

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

[email protected]

with the subject “C”

For Other information mail at

[email protected]

21