05 Betc 1313 - Lab 3 Repetition Control Tech
05 Betc 1313 - Lab 3 Repetition Control Tech
PROGRAMMING FUNDAMENTAL
3.
DATE 2/4/2018
TOTAL MARKS
1
1.0 OBJECTIVES
2.0 EQUIPMENT
Multiple relational operator can be use together with logical operator. C has 3 logical
operators. The logical operators are:
1
3.2 Repetition Control Structure:
In most commercial software that you use, you can repeat a process many
times.For example, when using an editor program or word processor, you can
move the cursor to a program line and perform as many edit operations as you
need to.
Repetition, you’ll recall is the third type of program control structure (sequence,
selection,repetition), and the repetition of steps in a program is called a loop.
A block of one or more statements that are repeatedly executed until a condition is
reached.In C, while is a reserved word. The expression acts as a decision maker.
It is called the loop condition.The statement can be either a simple or compound
statement. It is called the body of the loop.
2
Explainations:
1. The loop repetition condition (a condition to control the loop process) is
tested. If it is true, the statement (loop body) is executed..
2. loop repetition condition is retested. The statement is repeated as long as
(while) the loop repetition condition is true.
3. When this condition is tested and found to be false, the (while) loop is
exited and the next program statement after the while statement is
executed.
Explainations:
3
3.2.3 The do-while Statement
The do-while loop is a posttest loop, which means its expression is tested after
each iteration. The do-while loop looks something like an inverted while loop.
Here is the format of the do-while loop when the body of the loop contains mltiple
statements:
do
{
statement;
statement;
// place as many statements here
// as necessary.
statement;
} while (expression);
The do-while loop is a posttest loop. This means it does not test its expression
until it has completed an iteration. As a result, the do-while loop always performs
at least one iteration, even if the expression is false to begin with.
#include <stdio.h>
int main()
{
int numLines=0,count=0;
printf("Enter number of line:");
scanf("%d",&numLines);
1. Create a new empty file in the CodeBlock and copy the following code:
2. Build and run the program in the CodeBlock. Test your program with the following
inputs:
Employee Rate per hour (RM) Total working hour
Employee 1 4.50 8
Employee 2 4.00 9
Employee 3 5.00 6
Employee 4 4.00 7
5
3. Observe the results and understand the functionality of the code. You may derive
the flow chart from the given code. At the same time, try to understand on how to
use while loop for repetition.
4. In a new C file, modify the given code in order to replace while loop with for
loop. Redo step 2-3.
Section B:
Here is the Health Club Membership fee table for 3 categories:
Standard Adult Membership RM 40.00/month
Child Membership RM 20.00/month
Senior Citizenship Membership RM 30.00/month
Assuming you are working as a software engineer for this Health Club and your current
project is to build a program for the user to check their membership fee for each category.
The program requirement is needed you to use a do-while loop in your program.
6
5.0 RESULTS
7
1.Write the program using for loop statements in Section A.
8
2.Print screen the output windows for for statements in Section A.
9
4.Write the program code for do-while statement in Section B.
10
5.Print screen output windows for do-while statement in Section B.
11
6.0 DISCUSSION
Kindly discuss on the lab’s result and finding in section 4.0 (Procedure)
First of all in section a, we have copied a while loop program. while loop is a control
section a also we change the while loop program to for loop. For loop is is a control
repeatedly. A for-loop has two parts: a header specifying the iteration, and a body
which is executed once per iteration. The header often declares an explicit loop
counter or loop variable, which allows the body to know which iteration is being
executed. For-loops are typically used when the number of iterations is known
before entering the loop. For-loops can be thought of as shorthands for while-loops
which increment and test a loop variable.In section b we have did a do-while loop
program. A do while loop is a control flow statement that executes a block of code
at least once, and then repeatedly executes the block, or not, depending on a given
boolean condition at the end of the block.Do-while loop actually similar to the while
loop,excepts that the test condition occurs at the end of the loop.This loop is an
exit-conditioning loop.This means that the body of the loop is always executed
7.0 CONCLUSION
Conclude what you have learned in this lab session.
12