0% found this document useful (0 votes)
34 views20 pages

Military Institute of Science and Technology Department of NAME

Here are C++ programs to create the patterns shown: (1) #include <iostream> using namespace std; int main() { int n, i, j; cout << "Enter number of rows: "; cin >> n; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) cout << "* "; cout << endl; } return 0; } (2) #include <iostream> using namespace std; int main() { int n, i, j, space; cout << "Enter number of rows: "; cin

Uploaded by

Sakib Rafee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views20 pages

Military Institute of Science and Technology Department of NAME

Here are C++ programs to create the patterns shown: (1) #include <iostream> using namespace std; int main() { int n, i, j; cout << "Enter number of rows: "; cin >> n; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) cout << "* "; cout << endl; } return 0; } (2) #include <iostream> using namespace std; int main() { int n, i, j, space; cout << "Enter number of rows: "; cin

Uploaded by

Sakib Rafee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Military Institute of Science and Technology

 
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:

Write a program using switch statement to check vowel or consonant.


switch Statement:
#include <iostream> break; break;
using namespace std; case 'o': case 'O':
int main() cout<<"vowel "<<endl; cout<<"vowel "<<endl;
{ break; break;
char ch; case 'u': case 'U':
cout<<"vowel "<<endl; cout<<"vowel "<<endl;
cin>>ch;
break;
switch(ch) { break;
default:
case 'a': case 'A': cout<<"consonant
cout<<"vowel "<<endl; cout<<"vowel "<<endl; "<<endl;
break; break; }
case 'e': case 'E': return 0;
cout<<"vowel "<<endl; cout<<"vowel "<<endl; }
break; break;
case 'i': case 'I':
cout<<"vowel "<<endl; cout<<"vowel "<<endl;
Class Task 2
Display Fibonacci series up to a number of terms
Solve >>
Class Task 3
Check a number Palindrome or not
Solve >>
Class Task 4
Check Armstrong Number
Creating Different Patterns
*** * *** * * * * *
*** ** ** ** ** ***
*** *** * * * * * *
(1) (2) (3) (4) (5) (6)
Solve (1)
#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<=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;

for (i=1; i<=row; i++){


for(space = i; space < row; space++){
cout << " ";
}
for (j=1; j<=i; j++){
cout << "* ";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
Solve (5)
int main(){
>> int i, j, row, space;
cout << "Enter the number of row: ";
cin >> row;

for (i=row; i>=1; i--){


for(space = i; space < row; space++){
cout << " ";
}
for (j=1; j<=i; j++){
cout << "* ";
}
cout << endl;
}
}
Solve (6)
>>
#include <iostream>
using namespace std;
}
cout << endl;
}
int main(){
int i, j, row, space; for (i=row-1; i>=1; i--){
cout << "Enter the number of row: "; for(space = i; space < row; space++){
cin >> row; cout << " ";
}
for (i=1; i<=row; i++){ for (j=1; j<=(2*i-1); j++){
for(space = i; space < row; space++){ cout << "*";
cout << " "; }
} cout << endl;
for (j=1; j<=(2*i-1); j++){ }
cout << "*"; }
Create Different Patterns (Assignment)

(1) (2) (3) (4)

You might also like