0% found this document useful (0 votes)
117 views13 pages

05 Betc 1313 - Lab 3 Repetition Control Tech

The document describes a programming lab assignment on repetition control techniques in C programming. The objectives are to familiarize students with while, for, and do-while loops. The lab involves modifying a payroll calculation program to replace the while loop with a for loop. It also involves writing a new program to calculate health club membership fees using a do-while loop. The document provides background on repetition control structures and the syntax of while, for, and do-while loops in C. It outlines the procedures for completing the programming tasks and reporting the results.

Uploaded by

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

05 Betc 1313 - Lab 3 Repetition Control Tech

The document describes a programming lab assignment on repetition control techniques in C programming. The objectives are to familiarize students with while, for, and do-while loops. The lab involves modifying a payroll calculation program to replace the while loop with a for loop. It also involves writing a new program to calculate health club membership fees using a do-while loop. The document provides background on repetition control structures and the syntax of while, for, and do-while loops in C. It outlines the procedures for completing the programming tasks and reporting the results.

Uploaded by

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

1 BETC G2-

FAKULTI TEKNOLOGI KEJURUTERAAN


UNIVERSITI TEKNIKAL MALAYSIA MELAKA

PROGRAMMING FUNDAMENTAL

BETC 1313 SEMESTER 2 SESI 2017/2018

LAB 3: REPETITION CONTROL TECHNIQUES

NAME OF GROUP 1.KISHEN A/L KUNALAN B071710622


MEMBERS &
MATRIX NUMBER
2.

3.

COURSE BETT S2/1

DATE 2/4/2018

NAME OF INSTRUCTOR PN.ROSZIANA binti HASHIM

EXAMINER’S COMMENT VERIFICATION STAMP

TOTAL MARKS

1
1.0 OBJECTIVES

1. To familiarize with C programming control structure.


2. To create a program using while statement.
3. To create a program using for statement.
4. To compare the differences between while and for statement.
5. To create a program using do-while statement.
6. To work effectively in given task in group.

2.0 EQUIPMENT

1. Personal Computer / desktop


2. Software: CodeBlocks

3.0 SYNOPSIS& THEORY

3.1 Selection Control Structures:

Defines two options of action depending on the result of a condition. A condition is


an expression that is when evaluated as either true or false. The expression
normally involve single or multiple relational operators. There are six relational
operators available in C:

Relational Operator Meaning


< Less than
<= Less than or equal to
== Equal To
> Large than
>= Large than or equal to
!= Not equal to

Multiple relational operator can be use together with logical operator. C has 3 logical
operators. The logical operators are:

Relational Operator Meaning


! Logical NOT
&& Logical AND
|| Logical OR

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.

3.2.1 The while Statement


The loop shown in psuedocode below is called a counter-controlled loop (or
counting loop) because its repeatition is managed by a loop control variable
whose value represents a count. A counter-controlled loop follows this general
format:

Set loop control variable to an initil value of 0.


while loop control variable < final value
...........
Increase loop control variable by 1.

We use a counter-controlled loop when we can determine prior to loop execution


exactly how many loop repetitions will be needed to solve the problem. This
number should appear as the final value in the while condition.

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.

while (loop repetition condition)


{
statement;
}

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.

3.2.2 The for Statement


C provides the for statement as another form of implementing loops. The
loop we have seen so far are typical of most repetition structures in that they
have three loop control components in addition to the loop body:
 Initialization of the loop control variable
 Test of the loop repetition condition, and
 Change (update) of the loop control variable.

An important feature of the for statement in C is that is supplies the


designated place for each of these three components.

for (initialization expression;


loop repetition condition;
update expression)
{
statement;
}

Explainations:

1. The initialization expression is executed.


2. The loop repetition condition is tested. If it is true, the statement is
executed.
3. The update expression is evaluated.
4. The loop repetiton condition is retested. The statement is repeated as long
as the loop repetition condition is true.
5. When this condition is tested and found to be false, the for loop is exited
and the next program statement after the for statement is executed.

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.

Example of nested loop statement:

#include <stdio.h>
int main()
{
int numLines=0,count=0;
printf("Enter number of line:");
scanf("%d",&numLines);

for(count = 0; count < numLines; count++)


{
int numSymbol=0;
while(numSymbol < 10)
{
printf("*");
numSymbol++;
}
printf("\n");
}
return 0;
} 4
4.0 PROCEDURE

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.

The program output should be like an interface below:

6
5.0 RESULTS

Copy the program in Section A.

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.

3.Draw the flow chart for the program 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

flow statement that allows code to be executed repeatedly based on a given

Boolean condition. The while loop can be thought of as a repeating if statement. In

section a also we change the while loop program to for loop. For loop is is a control

flow statement for specifying iteration, which allows code to be executed

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

first.Then,the test condition is evaluated.

7.0 CONCLUSION
Conclude what you have learned in this lab session.

From this experiment, i have learned to familiarize with c programming control


structure.Besides that, i have create a program using while ,for and do-while
statements.I also have learned the difference between these statements.Lastly,i
also have learned how to apply the for,while and do-while statements based on a
problem given.

12

You might also like