decision control
Post on 18-Oct-2014
1.480 views
DESCRIPTION
TRANSCRIPT
![Page 2: Decision control](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/7.jpg)
Relational operator
Operator Meaning
<= Less than OR equal to
< Less than
>= Greater than or equal to
= = Equal
!= Not equal
7
![Page 8: Decision control](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/8.jpg)
Logical Operator
Operator Meaning
|| or
&& and
8
![Page 9: Decision control](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/9.jpg)
Multiple Statements
• Put all statements inside the curly { } braces.
if(condition)
{
statement 1;
statement 2;
statement 3;
}
9
![Page 10: Decision control](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/20.jpg)
• 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](https://reader030.vdocuments.us/reader030/viewer/2022020306/5442b8d9b1af9f390a8b473f/html5/thumbnails/21.jpg)
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
with the subject “C”
For Other information mail at
21