Lecture 6 - Control Statements
Lecture 6 - Control Statements
• Control Statements
• Switch Statement
• Repetition Structures
• Loops
• Practical Application
Conditional Statements 2
Control Structures
• Sequential
• All statements are executed
• No statement is executed more than once
• Conditional
• Executes a statement if the given statement is true
• Decision making/selection structure
• Iterative
• Executes a statement/set of statements repeatedly for a specific number
of times
• Repetition/loops
Conditional Statements 3
Conditional Statements
true if (condition) {
Condition Execute statement(s)
/* block of code to
false
be executed if the
condition is true */
}
Remainder of the
program
• Write a program that inputs salary and grade. It adds 50% bonus
if the grade is greater than 15. It adds 25% bonus if the grade is
15 or less and then displays the total salary.
true true
Execute
Question 1? Question 2?
statement(s)
false false
Remainder of the
program
Conditional Statements 20
Example
Conditional Statements 22
Conditional Statements 23
Practice 4
Conditional Statements 24
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 == num2) {
if (num2 == num3) {
cout << "All numbers are equal." << endl;
} else {
cout << "Numbers are not equal." << endl;
}
} else {
cout << "Numbers are not equal." << endl;
}
return 0;
}
Conditional Statements 25
Switch Statement
• Alternative to the If-Else-If ladder
• Switch block consists of cases to be executed based on the value of
the switch variable
Switch (variable)
switch (variable) {
case 1:
true //code for case 1
Statements
Case 1 break;
break;
case 2:
false //code for case 1
true break;
Statements
Case 2
break; default:
//code when no case is valid
false };
Default Remainder of the
Statements program
Conditional Statements 26
Conditional Statements 27
Conditional Statements 28
A Question….
• If it is not used then all case blocks that come after the matching
case will be executed.
Conditional Statements 29
Example
• Using switch- case, write a program that takes two numbers and an
operator from the user and performs the corresponding
mathematical operation.
Conditional Statements 30
Conditional Statements 31
Conditional Statements 32
Difference between Nested if and
switch
If…..else….if switch
Can test for equality and for logical Can only test for equality
expressions
Uses multiple expressions for multiple Uses a single expression for multiple choices
choices
Can check for a range of values Cannot check for a range of values
Less compact than switch More compact vs nested if
Can evaluate int, float and other data types Can evaluate only characters and integers
Conditional Statements 33
Ternary Conditional Operator (? : )
• Concise, inline method of writing conditional statements
• Shorthand for if – else condition
C++ Syntax
Example Code condition ? expression1 : expression2;
int marks;
cout << "Enter your marks: ";
cin >> marks;
cout << "You " << result << " the exam.";
Conditional Statements 34
Example
• Write a program that takes two numbers as input and finds the
largest using a ternary operator.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
return 0;
} Conditional Statements 35
Example
• Write a program that takes three numbers as input and finds the
largest using a ternary operator.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter the first number:";
cin >> a;
cout << "Enter the second number:";
cin >> b;
cout << "Enter the third number:";
cin >> c;
int maxNum = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
cout << "The max. number is:" << maxNum << endl;
return 0;
}
Conditional Statements 36
Example
• Write a C++ program that checks if given point inside of triangle or outside.
Hint:
• Let the coordinates of the three corners be (x1, y1), (x2, y2), and (x3, y3) and
coordinates of the given point Q be (x, y)
• Calculate the area of the given triangle, i.e., the area of the triangle ABC using:
Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2
• Calculate the area of the triangle QAB. We can use the same formula for this.
Let this area be A1.
• Calculate the area of the triangle QBC. Let this area be A2.
• Calculate the area of the triangle QAC. Let this area be A3.
• If Q lies inside the triangle, then A1 + A2 + A3 must be equal to A
Conditional Statements 37
Solution
#include <iostream> int main()
using namespace std; {
float area(int x1, int y1, int x2, int y2, int x3, int y3) if (Q_Inside(0, 0, 20, 0, 10, 30, 10, 15))
{ cout <<"Inside";
return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); else
} cout <<"Not Inside";
bool Q_Inside(int x1, int y1, int x2, int y2, int x3, int y3, return 0;
int x, int y)
}
{
float A = area (x1, y1, x2, y2, x3, y3);
float A1 = area (x, y, x2, y2, x3, y3);
float A2 = area (x1, y1, x, y, x3, y3);
float A3 = area (x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
Conditional Statements 38