A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances.
(a) What are the differences between a while loop and a do-while loop?
(4 marks)
(b) Convert the while loop in Program 4 to: i. do-while loop.
ii. for loop.
(3 marks) (3 marks)
(c) Develop a program that asks the user to enter an integer. If the user enters integer 1, then the program will display "Got 1" on the screen. If the user enters integer 66, then the program will display "Got 66" on the screen. . If the user enters integer 99, then the program will display "Got 99" on the screen. If the user enters an integer other than these three numbers, the program will display "Got something else" on the screen. You are required to switch structure.
Here is program:
int main()
{
int num;
cout << "Enter number:" << endl;
cin >> num;
switch (num)
{
case 1:
cout << "Got 1" << endl;
break;
case 66:
cout << "Got 66" << endl;
break;
case 99:
cout << "Got 99" << endl;
break;
default:
cout << "Got something else" << endl;
break;
}
}
Comments
Leave a comment