Military Institute of Science and Technology Department of NAME
Military Institute of Science and Technology Department of NAME
Department of NAME
Course Code: NAME 430
Course Title: Computer Programming in Ship Design
Lecture - 05
switch Statement:
Switch case statement is used when we have Syntax:
multiple conditions and we need to perform switch (variable or an integer expression)
different action based on the condition. { case constant:
When we have multiple conditions and we
//C++ code
need to execute a block of statements when a
particular condition is satisfied. In such case break;
either we can use lengthy if…else--if or case constant:
switch case. The problem with lengthy if… //C++ code
else--if is that it becomes complex when we break;
have several conditions. The switch case is a default:
clean and efficient method of handling such
scenarios. //C++ code
;
}
switch Statement:
Switch Case statement is mostly
used with break statement even
though the break statement is
optional.
It evaluates the value of expression
or variable (based on whatever is
given inside switch braces), then
based on the outcome it executes
the corresponding case.
switch Statement:
#include <iostream> break;
using namespace std; case 3:
int main() cout<<"Case3 "<<endl;
{ break;
int i; case 4:
cin>>i; cout<<"Case4 "<<endl;
switch(i) break;
{ default:
case 1: cout<<"Default "<<endl;
cout<<"Case1 "<<endl; }
break; return 0;
case 2: }
cout<<"Case2 "<<endl;
switch Statement:
• break statement after default
The control would itself come out of the switch after default so using
break statement after it is not mandatory, however if you want you
can use it, there is no harm in doing that.
• Important Notes
1) Case doesn’t always need to have order 1, 2, 3 and so on. It can
have any integer value after case keyword. Also, case doesn’t need to
be in an ascending order always, you can specify them in any order
based on the requirement.
2) You can also use characters in switch case.
switch Statement:
Class Task 01:
int main(){
int i, j, row;
cout << "Enter the number of row: ";
cin >> row;
for (i=1; i<=row; i++){
for (j=1; j<=row; j++){
cout << "*";
}
cout << endl;
}
}
Solve (2)
#include <iostream>
>> using namespace std;
int main(){
int i, j, row;
cout << "Enter the number of row: ";
cin >> row;
for (i=1; i<=row; i++){
for (j=1; j<=i; j++){
cout << "*";
}
cout << endl;
}
}
Solve (3)
#include <iostream>
>> using namespace std;
int main(){
int i, j, row;
cout << "Enter the number of row: ";
cin >> row;
for (i=row; i>0; i--){
for (j=1; j<=i; j++){
cout << "*";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
Solve (4)
int main(){
>> int i, j, row, space;
cout << "Enter the number of row: ";
cin >> row;