0% found this document useful (0 votes)
80 views21 pages

Decision Making I

The document discusses different decision making statements in C++ including if statements, switch statements, and loops. It provides examples of using if-else statements to check conditions and switch statements to perform different actions based on a variable's value. Nested if-else statements and else-if ladders are also covered. The key aspects of if statements, switch statements, and nested/else-if conditional structures are explained through examples.

Uploaded by

Parth R. Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views21 pages

Decision Making I

The document discusses different decision making statements in C++ including if statements, switch statements, and loops. It provides examples of using if-else statements to check conditions and switch statements to perform different actions based on a variable's value. Nested if-else statements and else-if ladders are also covered. The key aspects of if statements, switch statements, and nested/else-if conditional structures are explained through examples.

Uploaded by

Parth R. Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

DECISION MAKING AND BRANCHING

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

Different forms of if statement


1. Simple if statement. 2. ifelse statement. 3. Nested ifelse statement. 4. else if ladder.

3/22/2013

CS&E Dept

Decision Making with if Statement


Used to control the flow of execution of statements. Two way decision statement & used in conjunction with an expression. General form of the simplest if statement: if (test Expression) statement1; statement_x;

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. If FALSE (zero) the next statement following the if statement


block is executed. 4. So, during the execution based on some condition, some codes not executed (skipped). For example: if (hours > 70) hours = hours + (hours - 70); cout<<

3/22/2013

CS&E Dept

Flow chart
Entry

Test Expressio n ?

True Statement -block

False

Statement -x Next statement

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

Example2:WAP to check whether a number is even or not .

#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

Nesting of if else Statements

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

The else if Ladder

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

Example: Using else-if ladder ,calculate grade for the

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

The switch Statement


This is multiplebranching statement where, based on a condition, the control is transferred to one of the many possible points. The most flexible control statement in selection structure of program control. Enables the program to execute different statements based on an expression that can have more than two values. Also called multiple choice statements. General form: switch(expression) { case template_1 : statement(s); break; case template_2 : statement(s); break; ... ... case template_n : statement(s); break; default : statement(s); } CS&E Dept 3/22/2013 next_statement;

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

You might also like