Decision Making I
Decision Making I
3/22/2013
CS&E Dept
Introduction
C++ program is a set of statements which are normally executed sequentially in the order in which written. But in practice it is required to change the order based on certain conditions , or repeat a group of statements until certain specified conditions are met. This requires decision making. C++ decision making statements: 1. if Statement 2. switch statement 3. Loops.
3/22/2013 CS&E Dept
3/22/2013
CS&E Dept
It allows to evaluate the expression first and then, depending on the value is true or false, it transfer the control to a particular statement. i.e Two way branching as shown in fig. Entry Test Expression ? False
True
3/22/2013 CS&E Dept
Explanation:
1.(expression) is evaluated.
2 If TRUE (non-zero) the statement ( or a block of statements ) is executed.
3/22/2013
CS&E Dept
Flow chart
Entry
Test Expressio n ?
False
3/22/2013
CS&E Dept
If else statement
Explanation: 1.The (expression) is evaluated. 2.If it evaluates to non-zero (TRUE), statement_1 is executed, otherwise, if it evaluates to zero (FALSE), statement_2 is executed. 3.They are mutually exclusive, meaning, either statement_1 is executed or statement_2, but not both. 4.The statements_ 1 and statements_ 2 can take the form of block and must be put in curly braces. Example: if(job_code == '1') rate = 7.00; else rate = 10.00; cout<< .;
3/22/2013 CS&E Dept
Flow chart
3/22/2013
CS&E Dept
Example1:WAP to find largest of 2 numbers #include<iostream.h> #include<conio.h> void main() { clrscr(); float a,b; cout<<"enter 2 numbers"<<endl; cin>>a>>b; cout<<endl; if(a>b) cout<<"large is "<<a; else cout<<"large is "<<b; }
3/22/2013
CS&E Dept
#include<iostream.h> #include<conio.h> void main() { int num; clrscr(); cout<<"enter a number "; cin>>num; if (num%2==0) cout<<"the number is even"; else cout<<"the number is odd"; getch(); }
3/22/2013
CS&E Dept
3/22/2013
CS&E Dept
Explanation
The if-else constructs can be nested (placed one within another) to any depth In this nested form, expression_1 is evaluated. If it is zero (FALSE-F), statement_4 is executed and the entire nested if statement is terminated; if not (TRUE-T), control goes to the second if (within the first if) and expression_2 is evaluated. If it is zero, statement_3 is executed; if not, control goes to the third if (within the second if) and expression_3 is evaluated. If it is zero, statement_2 is executed; if not, statement_1 is executed. The statement_1 (inner most) will only be executed if all the if statement is true.
3/22/2013 CS&E Dept
expression_1 is first evaluated. If it is TRUE, statement_1 is executed and the whole statement terminated and the next_statement is executed. On the other hand, if expression_1 is FALSE, control passes to the else if part and expression_2 is evaluated.
3/22/2013 CS&E Dept
If it is TRUE, statement_2 is executed and the whole system is terminated. If it is False, other else if parts (if any) are tested in a similar way. Finally, if expression_n is True, statement_n is executed; if not, last_statement is executed. Note that only one of the statements will be executed other will be skipped. The statements_n could also be a block statement and must be put in curly braces.
3/22/2013
CS&E Dept
marks entered.
avg -marks grade
80-100
60-79 50-59 40-49 00-39
A
B C D F
3/22/2013
CS&E Dept
#include<iostream.h> #include<conio.h> void main() { char grade; int marks; cout<<"enter marks";
cin>>marks; if(marks>79) grade = 'A'; else if (marks>59) grade = 'B'; else if (marks>49) grade = 'C'; else if (marks>39) grade = 'D'; else grade = 'F'; cout<<"\n grade = "<<grade; getch(); }
3/22/2013 CS&E Dept
Explanation
Evaluates the (expression) and compares its integer or character value with the templates following each case label. 1. If a match is found between (expression) and one of the templates, execution is transferred to the statement(s) that follows the case label. 2. If no match is found, execution is transferred to the statement(s) following the optional default label.
3. If no match is found and there is no default label, execution passes to the first statement following the switch statement closing brace, the next_statement.
4. To ensure that only the statements associated with the matching template are executed, include a break statement where needed, which terminates the entire switch statement. 5. As usual the statement(s) can also be a block of code put in curly braces
3/22/2013 CS&E Dept
index=mark/10; switch (index) { case 10: case 9: case 8: grade=A; break; case 7: case 6: grade=B; break; case 5: grade=C break; case 4: grade=D break; default: grade=F; break; } cout<<grade;
3/22/2013
Example
CS&E Dept
Example2: WAP to perform arithmetical operations according to the choice of the users
#include<iostream.h> #include<conio.h> void main() { clrscr(); char choice; int a,b; cout<<"enter two numbers\n"; cin>>a>>b; cout<<"enter ur choice\n"; cout<<" + addition \n - subtration\n"; cout<<"* multiplication \n / division\n"; cin>>choice;
3/22/2013 CS&E Dept
switch(choice) { case '+' : cout<<a+b; break; case '-' : cout<<a-b; break; case '*' : cout<<a*b; break; case '/' : cout<<a/b; break; default: cout<<"invalid choice"; } getch(); } 3/22/2013
CS&E Dept