Page 278 - Computer_Science_F5
P. 278
4. Rewrite the following if statement of performing the same task, C++
by using the switch-case statement. programming languages uses looping
Then, draw a flowchart of the given or iteration control structure. Looping
switch-case statement. control manages repeated executions in
a program. The loop contains two parts: Chapter Four: Object oriented programming with C++
FOR ONLINE READING ONLY
If (k==1) the body of the loop and the control
m+=5; statement. The control statement tests
else if (k==2) the condition provided, and it allows
m+=10;
else if (k==3) the repeated execution of the statements
m+=16; within the loop body. There are three
else if (k==4) types of looping or iteration used in the
m+=34;
C++ programming language. These are:
5. Considers the following code While loop, Do while loop and For loop.
fragment.
int x; While loop
cin >> x; The while loop allows a program to test
switch (x + 3) { the conditions first before performing
case 5: the execution. If the condition is true, the
cout << x << ‘\n’; loop is executed according to the loop
break; body. Otherwise, the loop terminates.
case 10:
cout << x - 3 << ‘\n’; The syntax of the while loop is given as:
break; while (loop condition)
case 20: {
cout << x + 3 << ‘\n’; Statement (s);
break; }
}
Determine the output of the above Figure 4.46 shows the execution flowchart
program when the user enters 3, 7, 9, of the while loop.
19 and 22 respectively.
Iteration or looping control Loop
structure condition
Suppose you are required to type the
text “C++ programming” ten times, True False
you will type the code cout<< “C++
programming”<<endl; ten times which Statement (s)
(loop body)
is very tedious. Also, consider you
are a teacher and you want to find the
average marks of each student for a class
of 100 students. To avoid repetition Figure 4.46: A flowchart of a While loop
269
Student’s Book Form Five
Computer Science Form 5.indd 269 23/07/2024 12:34

