Lesson 4

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Lesson 4

Conditional
Statements
using switch
A switch statement is used instead of nested if..else statements. It is a
multiple branch decision statement of C++. A switch statement tests a
variable with a list of values for equivalence. Each value is called a case.
The case value must be a constant integer.

A switch statement work with byte, short, char, and int primitive data
type, it also works with enumerated types and string.
FLOW CHART OF THE SWITCH STATEMENT IN C++
Syntax

Switch(expression/variable)
{
case value;
//statements
// any number of case statements
Break; //optional
Default://optional
//statements
}
Individual case keyword and a semi-colon (:) is used for each constant. Switch
tool is used for skipping to a particular case, after jumping to that case it will
execute all statements from case beneath that case this is called as “Fall
through”.
In the example below, for example, if the value 2 entered, then the program will
print two one something else!
#include <iostream>
using namespace std;
int main()
{
int num=0;
cout<<"Enter an integer : ";
cin>>num;
switch(num)
{
case 4: cout<<"four";
break;
case 3: cout<<"three";
break;
case 2: cout<<"two";
break;
case 1: cout<<"one";
cout<<"\n\n";
default:cout<<"something else!";
}
return 0;
}
To avoid fall through, the break statements are necessary to exit the switch. If
the value 4 is entered, then in case 4 will just print four and ends the switch.
The default label is non-compulsory, it is used for cases are not present.

Rules for applying Switch Statement

1. With switch statement use only byte, short, int, char data type.
2. You can use any number case statements within a switch statement.
3. Value for a case must be the same as the variable in a switch statement.

Limitation of Switch
Logical operators cannot be used with a switch statement. For instance
case k>=20; //is not allowed
Switch case variable can have only int and char data type. So float data type is
not allowed.
Example Program
#include <iostream>
using namespace std;
int main()
{
int year_level=0;
cout<<"\n";
cout<<"\tYear Level Checker Using Switch Statement";
cout<<"\n\n";
cout<<"\tWhat is your year level? ";
cin>>year_level;
cout<<"\n\n";
switch(year_level){
case 1 : cout<<"\tYou belong to Grade 7";
break;
case 2 : cout<<"\t You belong to Grade 8";
break;
case 3 : cout<<"\t You belong to Grade 9";
break;
case 4 : cout<<"\t You belong to Grade 10";
break;
default:cout<<"\t Sorry Invalid Year Level. Try Again.";
}
cout<<"\n\n";
cout<<"\t End of the program";
cout<<"\n\n";
return 0;
}
Ternary Operator in
C++
It is called Ternary Operator because it has 3 operands to operate a statement. It
also known as Conditional Operator or ? : (Question Mark and Colon) operator.

The three operands are:

1. Operand1:Conditional_part(Here, we write a conditional statement to


validate).
2. Operand2:True_part(Here, we write the statement to execute if the
conditional part is true).
3. Operand3:False_part(Here, we write the statement to execute if the
conditional part is false).
syntax
Operand1?Operand2:Operand3;

in the above symbol, Operand1 is condition and Operand 2 and


Operand 3 will be either value or variable or statement or any mathematical
expression. If condition will be true Operand2 will be executed otherwise
Operand 3 will be executed.

Example:

a<b?cout<<“a is less”<<“a is greater”;

The advantage of Using Ternary Operator


Using Ternary Operator reduces the number of line codes and improve
the performance of application.

You might also like