Exercise 7 Intro To Programming
Exercise 7 Intro To Programming
FA19-BEE-078
Exercise #07
int x;
std::cin >> x;
switch (x + 3) {
case 5:
std::cout << x << '\n';
break;
case 10:
std::cout << x - 3 << '\n';
break;
case 20:
std::cout << x + 3 << '\n';
break;
}
1. It would print “2” on the screen.
2. It wouldn’t print anything.
3. It would print “4” on screen.
4. It would print “20” on screen.
5. It wouldn’t print anything.
char ch;
std::cin >> ch;
switch (ch) {
case 'a':
std::cout << "*\n";
break;
case 'A':
std::cout << "**\n";
break;
case 'B':
case 'b':
std::cout << "***\n";
case 'C':
case 'c':
std::cout << "****\n";
break;
default:
std::cout << "*****\n";
}
1. It would print 1 asterisk.
2. It would print 2 asterisks.
3. It would print 3 asterisks on 1 row and 4 on the second row.
4. It would print 3 asterisks on 1 row and 4 on the second row.
5. It would print 4 asterisks.
6. It would print 4 asterisks.
7. It would print 5 asterisks.
int x = 0;
do {
std::cout << x << " ";
x++;
} while (x < 10);
std::cout << '\n';
It prints:
0123456789
int x = 20;
do {
std::cout << x << " ";
x++;
} while (x < 10);
std::cout << '\n';
#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cout << "Enter any character: ";
cin >> ch;
switch(ch){
case 'A':
value = 10;
cout << value << '\n';
break;
case'P':
value = 20;
cout << value << '\n';
break;
case 'T':
value = 30;
cout << value << '\n';
break;
case 'V':
value = 40;
cout << value << '\n';
break;
default:
value = 50;
cout << value << '\n';
break;
}
}
Question 07: Re-write the program given in the question to use multi-way if-else statement instead of
switch?
#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cin >> ch;
if (ch=='A') value = 10;
else if (ch=='P') cin >> value;
else if (ch=='T') value = ch;
else if (ch=='V') value = ch + 1000;
else value = 50;
cout << value << '\n';
}
Question 08: Re-write the program given in the question to use multi-way if-else statement instead of
switch statement?
#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cin >> ch;
if (ch=='A'){
cout << ch << '\n';
value = 10;
}
else if (ch=='P' || ch=='E') cin >> value;
else if (ch=='T'){
cin >> ch;
value = ch;
cout << "Value = " << value << ", ch = " << ch << '\n';
}
else if (ch=='C'){
value = ch;
cout << "Value = " << value << " , ch = " << ch << '\n';
}
else if (ch=='V'){
value = ch + 1000;
cout << value << '\n';
}
Question 09: Rewrite the following code fragment so a while loop is used instead of for statement.
Answer:
int i = 100;
while (i >= 1){
cout << i << ‘\n’;
i--;
}
Question 10: Rewrite the following code fragment so that it uses the conditional operator instead of an
if statement:
Answer:
Question 11: Rewrite the following code fragment so that it uses the conditional operator instead of an
if/else statement:
Answer:
(value % 2 == 0) ? 0 : value + 1;
Question 12: Would the following multi-way if/else be a good candidate to rewrite as a switch
statement? If so, rewrite the code using a switch; otherwise, explain why it is impractical to do so.
int x, y;
std::cin >> x >> y;
if (x < 10)
y = 10;
else if (x == 5)
y = 5;
else if (x == y)
y = 0;
else if (y > 10)
x = 10;
else
x = y;
It is impractical to do so because the above program includes less than (<) and greater than (>) operator
which cannot be used in switch statement. So, when we have to compare values it is highly
recommended to use if-else statements and switch statement isn’t applicable in such conditions and
circumstances.