Question

1) Convert the "Switch" statement to "if/else"

Convert the following switch statement to an equivalent if/else: switch(x) case 10: cost- 1; break; case 20: cost- 2; break; default: cost 0; 0 if (cost =-1) x = 1; else if (cost-20) x = 2; else x = 0; O if ( x = 10) cost = 1; else if (x = 2) cost-20; else cost = 0; if (case10) cost 1; else if (case 20) cost- 2; else cost-0 if (cost1) case- 10; else if (cost 2) case 20; else cost 0; if (x10) cost 1; else if (x20) cost 2; else cost 0;

2) Convert from for loop to while loop

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) E (Last Option)

if(x==10) cost = 1 else if(x==20) .. . .. . .. .. . ..


2) B

int k = 0;
while (k < 10) {
cout<<k;
k++;

}



Thanks, PLEASE UPVOTE if helpful

Add a comment
Know the answer?
Add Answer to:
1) Convert the "Switch" statement to "if/else" 2) Convert from for loop to while loop Convert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 41. Executing the "continue" statement from within a loop     causes control to go     A....

    41. Executing the "continue" statement from within a loop     causes control to go     A. to the next line of code     B. out of the loop     C. to the beginning of the loop          D. to check the loop condition for repeating the loop     E. none of the above      42. The 'default' statement inside a 'switch()' is optional.          T__   F__             43. The following 'switch' implementation is legal:              int i = 2;        ...

  • Rewrite the following switch statement as an equivalent if statement. The variable digit is of type...

    Rewrite the following switch statement as an equivalent if statement. The variable digit is of type int. switch (digit) { case 0: case 1: value = 3; break; case 2: value = 6; break; case 3: value = 9; break; default: value = 0; }

  • A) Rewrite the following pseudocode segment in C++ using the loop structures (for and while). Assume...

    A) Rewrite the following pseudocode segment in C++ using the loop structures (for and while). Assume that all the variables are declared and initialized. k-G+ 13)/27 loop: if k> 10 then goto out k=k+1 i=3"k-1 goto loop I out: B) Rewrite the following code segment in C++ using the multiple-selection (switch and if-else) statements. Assume that all the variables are declared and initialized. if((k 1) || (k 2))j-2 k-1 if ((k 3) || ( ks))j - 3.k+1 if (k 4)j-4k-1...

  • Convert the following code to assembly MIPS void checki() { int i = 0; while(i <...

    Convert the following code to assembly MIPS void checki() { int i = 0; while(i < 10) { switch(i) { case 1: i += 5; break; case 2: i --; break; case 3: i += 2; break; default: i++; } } } void main() { checki(); }

  • Pick a switch statement from the reading. Put it into a main() Java program and make...

    Pick a switch statement from the reading. Put it into a main() Java program and make it work. Attach the .java file and copyAndPaste the output into the WriteSubmissionBox The switch Statement S1.51 Multiway if-else statements can become unwieldy when you must choose from among many possible courses of action. If the choice is based on the value of an integer or character expres- sion, the switch statement can make your code easier to read. The switch statement begins with...

  • Convert the below code into if else selection: #include <iostream> using namespace std; int main() {...

    Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }

  • using C++ 1. Convert the following while loop into a for loop: int count = 0;...

    using C++ 1. Convert the following while loop into a for loop: int count = 0; while (count < 10) x = count + sin(x); y = count + cos(y); count++;

  • Computer science multiple choice: no explanation needed it 27. Which type of loop will always execute...

    Computer science multiple choice: no explanation needed it 27. Which type of loop will always execute its code at least Once? a while cfor b. do-while d. for in 28. What does $message contain after the following code executes? Srate 0.1; if (!is_numeric Srate)) { Smessage 'Rate is not a number. } else if (Srate < 0) { Smessage 'Rate cannot be less than zero": else if ($rate >0.2) [ Smessage "Rate cannot be greater than 20%." } else {...

  • The switch Statement C++program This lab work with the switch statement. • Remove the break statements...

    The switch Statement C++program This lab work with the switch statement. • Remove the break statements from each of the cases. What is the effect on the execution of the program? • Add an additional switch statement that allows for a Passing option for a grade of D or better. Use the sample run given below to model your output. Sample Run: What grade did you earn in Programming I? YOU PASSED! An A - excellent work! The following is...

  • Write a c++ expression representing the following algebraic expression. Assume that all variables in your program...

    Write a c++ expression representing the following algebraic expression. Assume that all variables in your program are of the type double and that your program has already included the <cmath> header file. 3x + 1/y - 10 + Squareroot g Your answer: (b) Rewrite the same expression assuming that variables x and y in your program are of type int your answer: Convert the following switch statement into an equivalent if-else if statement switch (ch) {case 'A'; cout << "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT