0% found this document useful (0 votes)
68 views4 pages

AACS1074 Programming Concepts and Design

1. The document describes various looping constructs in C++ like while, do-while, and for loops. It provides examples of using breakpoints, continue, stop debugging, and restart functions in Visual Studio. 2. Loops can be used to repeatedly perform tasks like displaying output, calculating sums, and checking for sentinel values. Examples are provided to illustrate using loops to square and cube input numbers, calculate averages, and display multiplication tables. 3. Debugging techniques like setting breakpoints are useful for finding problems in programs. Loops will execute repeatedly until their condition is met, so care must be taken to ensure they terminate.

Uploaded by

Girish bv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

AACS1074 Programming Concepts and Design

1. The document describes various looping constructs in C++ like while, do-while, and for loops. It provides examples of using breakpoints, continue, stop debugging, and restart functions in Visual Studio. 2. Loops can be used to repeatedly perform tasks like displaying output, calculating sums, and checking for sentinel values. Examples are provided to illustrate using loops to square and cube input numbers, calculate averages, and display multiplication tables. 3. Debugging techniques like setting breakpoints are useful for finding problems in programs. Loops will execute repeatedly until their condition is met, so care must be taken to ensure they terminate.

Uploaded by

Girish bv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AACS1074 Programming Concepts and Design I Practical 7(A): Looping

Practical 7(A): Simple Looping


Microsoft Visual Studio provided some useful function for debugging:

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");

3. Modify the program inQ2 above so that it will:


• Display the number and its cube (e.g. the cube of 3 = 3*3*3=27)
• Stop only when the user keys in the number -999 (this is called the sentinel value)
• Use a while-loop with priming read.
o This means the code for reading is appears once before the loop starts, and
another time before the end of the loop.
o (This is considered a better method when sentinels are used.)

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;

for (i=1; i<=5; i++)


printf("i = %d \t sum = %d\n", i, sum+=i);

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.

Input expected output?


i. 3 _________________
ii. 7 _________________

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

You might also like