Sunteți pe pagina 1din 10

PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

TITLE : Looping Statements I(LOOPING STATEMENT : FOR, WHILE, DO WHILE)


CLO : CLO3
OUTCOMES :

Upon completion the lab session, students should be able to:


i- Understand simple while, for and do while statements.

EQUIPMENTS:
No Component Quantity
1 Computer 1 unit
2 Codeblocks/Microsoft Visual C 1 unit

SAFETY PRECAUTION:

1. Do not plug in external devices (e.g USB thumb drive) without scanning them for
computer viruses
2. Always back up all your important data files.

PRACTICAL WORK:

PRACTICAL WORK:

Lab 5.1

Trace the output

#include <stdio.h>
int main()
{

int times;
int x;

printf("Sila masukkan berapa kali anda mahu ulang ayat ini : ");
scanf("%d",&times);

for(x=1; x<=times; x++)


{
printf("\n%d - ", x);

printf("HAI SEMUA.. SALAM SEJAHTERA..\n\n ");

}
return 0;
}

1
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

OUTPUT :

Lab 5.2
By using for statement, create a program that will display I WILL SCORE A IN
FUNDAMENTAL PROGRAMMING 7 times.

Sample output:

ANSWER :

Program

2
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

Lab 5.3
Create a program that will display numbers from 1 to 50 using while statement.

Sample output:

Answer:
Program

Lab 5.4
Write a program to produce the output that displays an odd numbers only. Use do-while
statement.

TIPS : Add the given condition in your program.

if ( number % 2 ! = 0 )

Sample output:

3
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

Answer:
Program

Lab 5.5
Based on lab 4.3, modify the program to produce the output that displays an even numbers
only. Use for statement.

Sample output:

Answer:
Program

4
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

Lab 5.6
Write a complete program that will display an output as below. Use FOR statements.

ANSWER :
Program

5
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

QUESTIONS :

1. What is one difference between while statement and do-while statement. (2 marks)

Do-while statement While statement

2. Draw flowchart for LAB 5.6. (10 marks)

3. What does the code block following while (x<100) execute?


a) When x is less than one hundred
b) When x is greater than one hundred
c) When x is equal to one hundred
d) While it wishes

6
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

4. Which is not a loop structure?


a) for
b) do while
c) while
d) repeat until

5. What is the final value of x when the code for (int x=0; x<10; x++) is run?
a) 10
b) 9
c) 0
d) 1

6. Repeat the section of your program based on a specified number of times.


The above statement is referring to
a) for
b) if
c) if-else
d) switch-case

7.
#include<stdio.h>
int main()
{
int i=0;
for(i=1; i<=4; i++)
printf(%d,i);
getchar();
return 0;
}

What will be the output of the program above?


a) 0 1 2 3 4 5
b) 0 1 2 3 4
c) 1 2 3 4
d) 1 2 3 4 5

8. Give the output (3 Marks)


a)
int s =1;
while (s < 10)
{
printf ( %d \t , s ) ;
s ++;
}
Output :

7
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

int z =10;
while (z >2)
{
z--;
printf ( %d \t , z*2 );
}
Output :

int loopcounter=1;
c)
while ( loopcounter <= 10 )
{
printf ( "%d \t %d \n",loopcounter , loopcounter*loopcounter ) ;
loopcounter++;
}

Output :

8
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

RUBRIC OF PRACTICAL WORK 5

Course Learning Very Good Good Poor


Outcome (CLO)/ Skills/ Aspects Mark
5 3 1
Learning Domain (LD)
Lab 5.1 Succesful to make correct for Successful to make correct for statement Having difficulties to make for
For statements statement syntax. after being assisted. statement syntax and need
assistance.
Lab 5.1 Successful and manage to get Successful and manage to get output in Having difficulties to run the
Compile and get output in given time. extended time. program and need assistance.
output
Lab 5.2 Succesful to make correct for Successful to make correct for statement Having difficulties to make for
CLO3: for statement statement syntax. after being assisted. statement syntax and need
Construct, run and assistance.
debug programs Lab 5.2 Successful program with neat Successful program after being assisted Having difficulties to run the
written in C language Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
for assigned project output output in given time. to get output in extended time.
during practical work Lab 5.3 Succesful to make correct while Successful to make correct while Having difficulties to make while
sessions while statement statement syntax. statement after being assisted. statement syntax and need
assistance.
LD2: Lab 5.3 Successful program with neat Successful program after being assisted Having difficulties to run the
Practical Skills Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
output output in given time. to get output in extended time.
Lab 5.4 Succesful to make correct do Successful to make correct do while Having difficulties to make do
Do-while while statement syntax. statement after being assisted. while statement syntax and need
statement assistance.
Lab 5.4 Successful program with neat Successful program after being assisted Having difficulties to run the
Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
output output in given time. to get output in extended time.
Lab 5.5 Succesful to make correct for Successful to make correct for statement Having difficulties to make for
For statement statement syntax. after being assisted. statement syntax and need
assistance.
Lab 5.5 Successful program with neat Successful program after being assisted Having difficulties to run the
Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
output output in given time. to get output in extended time.

9
PTSB/JKE DEC2012 FUNDAMENTAL PROGRAMMING/PRACTICAL WORK 5

Succesful to make correct Succesful to make correct declaration Having difficulties to run the
declaration syntax. syntax in extended time. program and need assistance.
int times;
Lab 5.6
int x;
Declaration
float height;
float weight
float bmi;
Lab 5.6 Succesful to make correct printf Succesful to make correct printf and scanf Having difficulties to run the
Condition for and scanf for condition syntax. for condition syntax in extended time. program and need assistance.
looping scanf (%d, &times );
Lab 5.6 Succesful to make correct FOR Succesful to make correct FOR Having difficulties to run the
Condition for statements. statements in extended time. program and need assistance.
looping (for(x=1; x<=times; x++)) (for(x=1; x<=times; x++))
Lab5.6 Succesful to used a correct Succesful to used a correct printf(), Having difficulties to used a
Compile and get printf(), scanf() and formula. scanf() and formula after being assisted. correct printf(), scanf() and
output formula, and need assistance.
Lab5.6 Succesful to used a correct Succesful to used a correct formular after Having difficulties to used a
Compile and get process being assisted. correct process and need
output bmi = weight / (height*height) assistance.
Lab 5.6 Successful program with neat Successful program after being assisted Having difficulties to run the
Compile and get code-typing and manage to get with quite-neat code-typing and manage program and need assistance.
output output in given time. to get output in extended time.
Total / 80

10

S-ar putea să vă placă și