Sie sind auf Seite 1von 4

Lab Title Lab No.

Course Lab Date OBJECTIVE:

POLITEKNIK SULTAN IDRIS SHAH PROGRAMMING 1 E3062 Types of Control Structure in C++ (Sequence,Selection and Iteration Structure) 04 DKE 3 6 October 2009

1. To describe and differentiate three types of program control structure which are Sequence, Selection and Iteration Structure. 2. To apply the control structure in C++ programming. 3. To write and debug the simple program control structure using if,if...else and switch...case in C++ 4. To understand the used of while, dowhile and for in C++ THEORY : Logical structure or program control structures refer to the order of execution of instructions in a program. So far, in all our examples, the instructions were executed sequentially one by one, from the top downwards. However, most real life problems require some kind of decision making, which involves comparing values, and based on the comparison, to take a certain course of action. Hence, C++ provides structure that will allow the non-sequential execution of program instructions. This means that an instruction or a whole block of instructions can be executed, repeated or skipped. C++ has a set of rich and powerful control structures (statements) that makes it a popular language. Control structure generally fall into four categories but in this lab we have to know only three of them, which are: i. sequence structure ii. selection structure iii. repetition or iteration structure Sequence structure The sequence control structure is the simplest of all the structures. The program instructions are executed one by one, starting from the first instruction and ending in the last instruction as in the program segment.

Example: x = 5 y = 10 Total = x * y cout<< \n << Total = <<Total<< \n; Entry S1 S2 S3 S4

(S1) (S2) (S3) (S4) Exit

Selection Structure The selection structure allows to be executed non-sequentially. It allows the comparison of two expressions, and based on the comparison, to select a certain course of action. In C++, there are three types of selection statements. They are: a. if statement b. if-else statement c. switch statement. Repetition (or Iteration) structure The repetition (or iteration) structure permits a sequence of the instructions to be executed repeatedly until certain condition is reached. The repetition structure or loop in C++ comes in three forms while, do-while and for. In this lab session, you need to familiarize with three program control structure which is Sequence, Selection and Iteration Structure to develop your own programming structure in C++. PROCEDURE: 1. Write the programming in Appendix 1. 2. Repair and debug the mistake in programming that you write. 3. Save your programming as lab4a, lab4b and lab4c.

REPORT FORMAT: 1. 2. 3. 4. 5. 6. Find new theory and describe the differences for each control structure. Show the result that you get for each exercise. Discuss the differences between three control structures. Discuss the programming that you have done in Appendix 1. Write down the conclusion that you get from this lab session. References.

Appendix 1 Exercise 4a-Selection Structure (while) /* Inputkan satu aksara. Selagi aksara yang diinputkan ialah Y atau y, mesej "Malaysia Boleh" akan dipaparkan */ #include <stdio.h> void main() { char chYes; cout<<Masukkan aksara:"; chYes = getchar(); while ( chYes == 'y') || ( chYes == 'Y') { cout<<Malaysia boleh\n"; fflush(stdin); cout<<Masukkan aksara:"; chYes = getchar(); }; } Exercise 4b-Looping Structure (do..while) #include <stdio.h> #include <ctype.h> void main() { int nRM50, nRM10, nRM5, nRM1, n50sen, n20sen, n10sen, n5sen, n1sen; float fJumlah=0.00; char chrPilihan; do { printf("\nBerapa keping/syiling yang anda ada bagi matawang:\n"); printf("RM 50: "); scanf("%d", &nRM50); printf("RM 10: "); scanf("%d", &nRM10); printf("RM 5: "); scanf("%d", &nRM5); printf("RM 1: "); scanf("%d", &nRM1); do { printf("50 Sen: "); scanf("%d", &n50sen); } while (n50sen > 50); do { printf("20 Sen: "); scanf("%d", &n20sen); } while (n20sen > 50); do { printf("10 Sen: ");

scanf("%d", &n10sen); } while (n10sen > 50); do { printf("5 Sen: "); scanf("%d", &n5sen); } while (n5sen > 50); do { printf("1 Sen: "); scanf("%d", &n1sen); } while (n1sen > 50); fJumlah = ((nRM50 * 50) + (nRM10 * 10) + (nRM5 * 5) + (nRM1 * 1) + (n50sen * 0.50) + (n20sen * 0.20) + (n10sen * 0.10) + (n5sen * 0.05) + (n1sen * 0.01)); printf("\nJumlah wang anda ialah: RM %.2f", fJumlah); printf("\nAdakah anda ingin meneruskan (Y/N): "); scanf("%s", &chrPilihan); } while (toupper(chrPilihan) == 'Y'); } Exercise 4c-Looping Structure (for) /* Satu aturcara C yang boleh menerima input, nama, no IC, No Matrik dan markah 3 matapelajaran atau lebih. */ #include <stdio.h> #include <conio.h> char strNama[50]; char strIC[50]; char strMatrik[50]; int nMarkah[4]; int ctIndex; float fPurata; void main() { cout<<\nMasukkan nama: "; cin>>strNama; cout<<"\nMasukkan no. IC: "; cin>>strIC); cout<<"\nMasukkan no. Matrik: "; cin>>strMatrik; for(ctIndex = 1;ctIndex < 4;ctIndex = ctIndex + 1) { cout<<"\nMasukkan Markah %d: ",ctIndex); cin>>Markah[ctIndex]; }; fpurata= ((nMarkah[1] + nMarkah[2] + nMarkah[3])/3); count<<"/nPurata markah adalah: %.2f", fPurata; getch(); }

Das könnte Ihnen auch gefallen