Lecture-6

Programming Fundamental
Instructor Name:
Lecture-6
Today’s Lecture
 Decision Making (Continue)
 Conditional operator (ternary Operator)
 Switch Statement
 Switch Statement Rules
 Break Statement
 goto Statement
2
Example
A company insures its drivers in the following cases
―If driver is married
―If the driver is unmarried, male & above 30 years of age
―If the driver is unmarried, female & above 25 years of age
Nested If example
main()
{
char gender,ms;
int age;
cout<< "enter age , gender, marital status";
cin>>age>>gender>>ms;
if(ms=='M')
cout<<"driver is insured";
else
{
if(gender=='M')
{
if(age>30)
cout<<"driver is insured";
else
cout<<"driver is not insured";
}
else
{
if (age>25)
cout<<"driver is insured";
else
cout<<"Driver is not insured";
}
}
getch();
Logical Operator example
#include<iostream.h>
#include<conio.h>
main()
{
char gender, ms;
int age;
cout<<"enter the age gender
cin>>age>>gender>>ms;
martial status";
if((ms=='M')|| (ms=='U'&& gender=='M' && age>30)||
(ms=='U' && gender=='F' && age>25))
cout<<"Driver is insured";
else
cout<<"driver is not insured";
getch();
}
Decision Making in C++
Conditional Operator
 The simple if/else statement can be replaced with a conditional operator
(?).
 The other names used for conditional operator are:
 Ternary operator
 Inline if (iif)
 Ternary if
 The conditional operator is a ternary operator that takes three operands.
 The syntax of conditional operator is as follows
Conditional_expression ? expression1 : expression2;
 if the conditional Expression is true, expression1 executes, otherwise if
the conditional Expression is false, expression 2 executes.
6
Decision Making in C++
Conditional Operator
 The first operand (conditional expression)is implicitly converted to bool. It
is evaluated and all side effects are completed before continuing.
 If the first operand (conditional expression) evaluates to true (1), the
second operand (expression1 ) is evaluated.
 If the first operand (conditional expression) evaluates to false (0), the third
operand (expression2;) is evaluated.
 The result of the conditional operator is the result of whichever operand is
evaluated — the second or the third.
 Only one of the last two operands is evaluated in a conditional expression.
7
Decision Making in C++
Conditional Operator – Example
using namespace std;
#include<iostream>
void main() {
int x,y;
cout<< "Enter two integers: "; cin>>x>>y;
cout<<x << (x ==y ? " is" : " is not") <<" equal to "<< y;
}
Output of the program
Enter Two Digits: 3 3
3 is equals to 3
8
Decision Making in C++
Switch Structure
 All the problem we discussed so far, we see that there are two approaches
for a multi way decision.
 In the first approach, we use as many if statements as needed. This is an
expensive approach.
 The second is the use of nested if statements. Which is little more efficient
than the first one.
 In the 'nested if statements' the nested else is not executed if the first if
condition is true and the control goes out of the if block.
 The C++ language provides us a stand-alone construct to handle these
instances.
 This construct is switch structure.
9
Decision Making in C++
Switch Structure
 The switch structure is a multiple-selection construct that is used in such
cases (multi way decisions) to make the code more efficient and easy to
read and understand.
 A switch statement allows a variable to be tested for equality against a list
of values.
 Each value is called a case, and the variable being switched on is checked
for each case.
 if a case label matches, the statements after the case label are executed.
 If no case label matches the switch expression, the code under the default
label is executed (if it exists).
10
Decision Making in C++
Switch Structure
 The Syntax for switch statement is as follow
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
//any number of case statements.
default :
//Optional statement(s);
}
11
Decision Making in C++
Switch Structure[Flow Chart]
12
Decision Making in C++
Switch Structure [ RULES]
 The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
 You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
 The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
 When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
13
Decision Making in C++
Switch Structure [ RULES]
 When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
 Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
 A switch statement can have an optional default case, which must appear
at the end of the switch.
 The default case can be used for performing a task when none of the cases
is true. No break is needed in the default case.
14
Decision Making in C++
Switch Implementation
Write a program that has a switch structure. The switch structure will check
different cases for grades. Implement your switch structure as follows
 If grade is A then display “Excellent”
 If grade is B or C then display “Well Done”
 If grade is D the display “You passed”
 If grade is F then display “You are Fail”
15
Decision Making in C++
void main () {
char grade = 'D';
switch(grade) {
case 'A' :
cout << "Excellent!\n";
break;
case 'B' :
case 'C' :
cout << "Well done\n";
break;
case 'D' :
cout << "You passed\n“;
break;
case 'F' :
cout << “You are Fail\n";
break;
default :
cout << "Invalid grade\n";
}
}
16
Decision Making in C++
Switch Implementation
 We will see the use of ' the letter a' instead of 'A' can affect our program.
 We want our program to be user- friendly. We don’t want to restrict the
user to enter the grade in capital letters only. So we have to handle both
small and capital letters in our program.
 Here comes the limitations of switch statement.
 We have to make two separate cases so we write
case ‘A” :
case ‘a’ :
statements;
Home Activity: Rewrite Code
17
Decision Making in C++
Break Statement
 The break statement interrupts the flow of control.
 In switch statement, when a true case is found, the flow of control goes
through every statement down ward.
 We want that only the statements of true case should be executed and the
remaining should be skipped.
 For this purpose, we write the break statement after the statements of a
case. Thus, when a true case is found and its statements are executed then
the break statement interrupts the flow of control and the control jumps
out of the switch statement.
18
Decision Making in C++
Break Statement
 If we want to do the same task for two cases, like in previous example for
‘A’ and ‘a’, then we don't put break statement after the first case.
 We write both the cases (or the cases may be more than two) line by line
then write the common statements to be executed for these cases and then
write the break statement.
 The break statement is necessary in switch structure, without it the switch
structure becomes illogic. As without it all the statement will execute after
first match case is found.
19
Example
Example
If alpha input is 5 , what will be alpha value after
processing
cin>>alpha;
switch (alpha)
{
case 1:
case 2:
alpha = alpha + 2;
break;
case 4:
alpha++;
case 5:
alpha = 2 * alpha;
case 6:
alpha = alpha + 5;
break;
default:
alpha--;
}
If alpha input is 5 , what will be alpha value after
processing
cin>>alpha;
switch (alpha)
{
case 1:
case 2:
alpha = alpha + 2;
break;
case 4:
alpha++;
case 5:
alpha = 2 * alpha;
case 6:
alpha = alpha + 5;
break;
default:
alpha--;
}
If a is 6
cin >> a;
if (a > 0)
switch (a)
{
case 1:
a = a + 3;
case 3:
a++;
break;
case 6:
a = a + 6;
case 8:
a = a * 8;
break;
default:
a--;
}
else
a = a + 2;
Practice Problem
Write a program that prompt the user to enter two numbers and
one
“Arithmetic Operator” . Program should perform the Arithmetic operation on
that numbers and display result.
Decision Making in C++
goto Statement
 The goto is an unconditional branch of execution.
 The goto statement is used to jump the control anywhere (back and forth)
in a program.
 To understand and decode such programs that contain unconditional
branches is almost impossible.
 It is very difficult, for a programmer, to keep the track of execution as the
control jumps from one place to the other and from there to anywhere else.
 It is very difficult to trace out the way of execution and figure out what the
program is doing.
26
27