AACS1074 Programming Concepts and Design
AACS1074 Programming Concepts and Design
1. Break Point: You can set a "break point" to pause the program when the program
execution reach the particular line. To toggle the "break point", you can make a red dot
(click on the most left side bar or press F9) on the particular source code line.
2. Continue: You can continue the program after the program had been pause.
3. Stop Debugging: You can stop the program immediately.
4. Restart: You can restart the program.
Toggling a break point is very useful. It can help programmer debugging, find out what is the
problems in the program.
During debugging, you can move your mouse to the particular variable to check what is the current
value or check the variable value at the bottom properties.
Page 1 of 4
AACS1074 Programming Concepts and Design I Practical 7(A): Looping
NOTE:
If your program gets into an ENDLESS LOOP during execution, click the[stop] icon.
1. The number 0 has the value FALSE in Boolean, while any non-zero number has the value
TRUE. Examine the following loop that has the expression while (1) which is always true. Run
the program and press the [stop] icon to get out of the program.
#include …
intmain(void)
{
while(1)
printf("True Forever!\n");
printf("End of Program...\n");
system ("pause");
return 0;
}
Examine the output in the following cases. Are the results as you expected?
• while (1) is changed to while (0)
• while (1) is changed to while (-55)
• while (1) is changed to for ( ; ; )
2. Run, observe and save the output of the code segment below using the following scenarios:
a) The firstinput is 0.
b) The first input is 3 and second input is 0.
int n;
do
{
printf("Give me any integer (0 to stop): ");
scanf("%d", &n);
printf("The square of %d is %d\n\n", n, n*n);
} while (n!=0);
printf("Bye Bye\n");
4. Write a program that prompts a user to enter a series of integers until he presses the sentinel
value,-999, to stop, and then displays the following:
• A message stating how many numbers he has keyed in
• The sum of the numbers
• The mean (ie. average) of the numbers (to 3 decimal places)
Page 2 of 4
AACS1074 Programming Concepts and Design I Practical 7(A): Looping
5. How many times does the following for loop execute? Check your answer.
inti, sum = 0;
Will the output be the same if the for expression is changed to the following? Check your
answer.
for (i=0; i<=4; i++)
6. Write a program which will use a loop to display a neat multiplication table (1 to 12) for any
number chosen by the user. Example of output (if the user entered 13):
Multiplication table for what number? 13
13X1=13
13X2=26
: : : : :
: : : : :
13X12=156
Which type of loop do you prefer to use for this -whileloop orfor loop? Explain.
7. abs(x) is a maths function which will return the absolute (positive) value of x, for
exampleabs(-3) gives the value 3, and abs(4) gives 4.
Examine the code below and trace the output manually, using the followinginput
values. Check your answers by running the program.
Note: You must include the math.h header file in your program.
inti, n, d;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i=1; i<=n*2; i++)
{
d=(n*2-1) - abs(n-i);
printf("%d",d);
}
printf("\n");
8. Write a C++ program to display the following square of even numbers from 2 to 10 by
using the for structure.
The square of 2 is 4
The square of 4 is 16
:
The square of 10 is 100
Page 3 of 4
AACS1074 Programming Concepts and Design I Practical 7(A): Looping
9. Write a program that uses a for loop to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum (firstNum must
be less than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their square between firstNum and secondNum.
e. Output the sum of the square of odd numbers between firstNum and
secondNum.
10. Write a program using for loop that prompts a user to enter a series of integers until
he presses the sentinel value, -999 to stop, and then displays the following:
• A message stating how many numbers he has keyed in
• The sum of the numbers
• The mean (ie. average) of the numbers (to 3 decimal places)
Page 4 of 4