Lab 6

5
UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 6 : Control Statements – Part 2 (Loops) NAME : _____________________________ MATRICS NO.: _______________ DATE : _____ MARK Objectives: Students be able to develop a repetition statements or program by using the iteration structure (loops). i. while loop statement ii. do…while loop statement iii. for loop statement Theory: a. while i. Syntax of while loop (one statement): while (condition- Code to execute while the condition is true) { Statement; } ii. Syntax of while loop (> one statement): while(condition- repeated until becomes false) { Statement 1; Statement 2; : : Statement n; } Example: /*Total up from marks 1 to marks 5.*/ while(count<=5) /* count from 1 until 5*/ { printf(“Key in the marks (%d) =”, count); scanf(“%d”, &marks); total=total+marks; /*add the marks one by one*/ count+=1; /*add the count*/ } b. do…while loops are useful for things Example: 1

Transcript of Lab 6

Page 1: Lab 6

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING

BTI 10202: COMPUTER PROGRAMMING

LAB 6 : Control Statements – Part 2 (Loops)

NAME : _____________________________MATRICS NO.: _______________ DATE : _____ MARK

Objectives: Students be able to develop a repetition statements or program by using the iteration

structure (loops).

i. while loop statement

ii. do…while loop statement

iii. for loop statement

Theory:

a. while

i. Syntax of while loop (one statement):

while (condition- Code to execute while the

condition is true)

{

Statement;

}

ii. Syntax of while loop (> one statement):

while(condition- repeated until becomes false)

{

Statement 1;

Statement 2;

:

:

Statement n;

}

Example:

/*Total up from marks 1 to marks 5.*/

while(count<=5) /* count from 1 until 5*/

{

printf(“Key in the marks (%d) =”,

count);

scanf(“%d”, &marks);

total=total+marks; /*add the marks

one by one*/

count+=1; /*add the count*/

}

b. do…while loops are useful for things that want to loop at

least once

Notice that the condition is tested at the end of the block

instead of the beginning, so the block will be executed at

least once

Syntax of do…while loop:

do

{

Statement 1;

Example:

/* Total up from marks 1 until marks 5*/

do

{

printf(“Key in the

marks(%d)=”,count);

scanf(“%d”, &marks);

total=total+marks; ‘* total up the

marks one by one*/

1

Page 2: Lab 6

:

:

Statements n;

}

while(condition);

count+=1; /*add count*/

}

while(count<=5); /*repeat until count

reach 5*/

c. for condition tells the program that while the conditional

expression is true the loop should continue to repeat

itself.

Syntax of for loop:

for(initial value; condition; update counter)

{

Statement(s);

}

Example:

/*Total up from marks 1 until marks 5*/

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

/*can write as ++count*/

{

printf(“key in the

marks(%d)=”,count);

scanf(“%d”, &marks);

total=total+marks; /* total up the

marks one by one*/

}

Flowchart for loop structures

d. Nested loops

Example:

#include<stdio.h>

#include<conio.h>

main()

{

int row,column,num;

do

2

Page 3: Lab 6

{

printf(“\nEnter the number of row of your triangle (1-30 only):\n”);

scanf(“%d”,&num);

}

while(num<=0||num>30); /*cannot more than 30 or less than 0*/

printf(“Your triangle\n”);

for(row=1;row<=num;++row)

{

printf(“\n”);

for(column=1;column<=(row*2-1);column++)

{

printf(“*”);

}

}

getch();

}

Exercise:

1. What is the output of following ‘for’ loop source code?

#include<stdio.h>

#include<conio.h>

main()

{

int i;

printf("This is a for loop\n");

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

printf("%d ", i);

getch();

}

3

Output

Page 4: Lab 6

2. Rewrite the ‘for’ loop program in Exercise 1 using ‘while’ loop statement to give the same

output.

3. Type the example source code for the nested loops (at theory: d section) and observe the

output. Explain the program in detail.

Output:

4

While statement

Page 5: Lab 6

5