The question ON DATABASE
A. Goto statements and labelsThe statements (statements) are written in a program will be carried out sequentially starting from the first statement to the last statement. Example:
...
statement_1;
statement_2;
statement_3;
statement_4;
statement_5;
statement_6;
...
If we intend to state that after the execution of certain statements (eg after statement_2) program flow will proceed to the specific statements other than statements directly below (eg to statement_5), then we must use goto statement, and set a label at the location in question . Example:
...
statement_1;
statement_2;
goto next;
statement_3;
statement_4;
next:
statement_5;
statement_6;
...
A label is a common identifier followed by a colon symbol (:). B. Function statement The statement is a statement function is used to call a function. Statement of functions performed by writing the name of the intended function and its arguments will be given to the parameters contained in these functions. In connection with this we must know what the name of the function and what parameters are needed by the function we will use. Such information can be found in the prototype, definition, or declaration of the function. Prototype and the definition of standard functions (which have been provided by the manufacturer compiler) can be found in the include directory with the file name berekstensi. H are grouped according to their usefulness, such as stdio.h, stdlib.h, conio.h, io.h, and others. To allow the compiler can check the truth of the statement writing a function in the source code, the file containing the function prototype should be include in the source code. C. Joint Statement / Block Joint statement or block is a set of statements defined as a unified statement. This is used to simplify the writing of statements repeatedly. Joint statement or a block preceded by the symbol "(" followed by statements that become members and terminated by the symbol ")". Example: {
statement_1;
statement_2;
statement_3;
...
}
D. If-else statement Implementation of a statement may be made only if a certain condition are met. If-else statement is one statement that can be used for this purpose. The general form of this statement are: if (expression) stat_1; [else stat_2;] Where expression is the expression of an ordinal (returns an integer value). Stat_1 statement will be made if the expression value is not 0 and stat_2 statement will be made if the expression is 0. Example: Code: if (code = 0) strcpy (sex, "Women"); else strcpy (sex, "Male"); Let us try to apply this statement in the following problem. Say you want a Fuel Price Table, from 1 to 10 liters. Desired output as follows. Code: Solar Liter Super Premium --------------------------- 1 250 385 450 2 500 770 900 3 750 1155 1350 4 1000 1540 1800 5 1250 1925 2250 6 1500 2310 2700 7 1750 2695 3150 8 2000 3080 3600 9 2250 3465 4050 10 2500 3850 4500 --------------------------- By using the statements that have been discussed above, the program for it is as shown below. Program-4.1: bbm.c Code: # include int liter, diesel, premium, super; main () ( printf ( "Solar Liter Super Premium \ n"); printf ("--------------------------- \ n "); liter = 1; repeat: * liter diesel = 250; Premium = liter * 385; liter * super = 450; printf ( "% 5d% 5d% 5d% 5d \ n", liter, diesel, premium, super); liter + +; if (liters <= 10) goto repeat; ("--------------------------- printf \ n ");) E. Switch-case statement, the default switch-case statement, the default is an extension of if-else statement that allows a variety of conditions. The general form of this statement is: switch (expression) (case eks_1: [stat_1; ... break;] case eks_2: [stat_2; ... break;] ... [default: stat_n; ... break;]) stat_1 statement and would do so if expression = eks_1. Stat_2 statements and so on will be done if expression = eks_2. Stat_n statements and so on will be done if the expression is not the same as the previous test. For example, say we have the item code (kb) and will set the name of goods (nb) and the type of goods (jb) based on the item code. Name of goods is the CPU, CRT, LCD, MS Office, Norton Anti Virus, and PhotoShop for code items 1, 2, 3, 4, 5, and 6. While the types of goods are goods Hardware for code 1, 2, and 3 or software to code items 4, 5, and 6. Code: switch (kb) (case 1: strcpy (nb, "CPU"); strcpy (jb, "Hardware"); break; case 2: strcpy (nb, "CRT"); strcpy (jb, "Hardware") ; break; case 3: strcpy (nb, "LCD"); strcpy (jb, "Hardware"); break; case 4: strcpy (nb, "MS Office"); strcpy (jb, "Software"); break; case 5: strcpy (nb, "Norton Anti Virus"); strcpy (jb, "Software"); break; case 6: strcpy (nb, "PhotoShop"); strcpy (jb, "Software"); break; default: strcpy (nb, "Name of the goods had not been set"); strcpy (jb, "Types of goods had not been set"); break;) Use a case for doing the same statement can be done as the following example. Code: switch (kb) (case 1: strcpy (nb, "CPU"); break; case 2: strcpy (nb, "CRT"); break; case 3: strcpy (nb, "LCD"); break; case 4: strcpy (nb, "MS Office"); break; case 5: strcpy (nb, "Norton Anti Virus"); break; case 6: strcpy (nb, "PhotoShop"); break; default: strcpy (nb, "The name of the goods had not been set"); break;) switch (kb) (case 1: case 2: case 3: strcpy (jb, "Hardware"); break; case 4: case 5: case 6: strcpy (jb, " Software "); break; default: strcpy (jb," Types of goods had not been set "); break;) F. While statement while statement is used to repeat the execution of certain statements during certain conditions are met. The general form of this statement is: while (expression) statement; Where expression is the expression of an ordinal (returns an integer value). Statement will be repeated during the expression of value is not 0. Algorithm this statement is: repeat: if (expression) statement; goto repeat; endif Here is the use to replace the while statement if and goto statements in the program 4.1. Code: # include int liter, diesel, premium, super; main () ( printf ( "Solar Liter Super Premium \ n"); printf ("--------------------------- \ n "); liter = 1; while (liters <= 10) (solar = liter * 250; premium = liter * 385; super = liter * 450; printf ( "% 5d% 5d% 5d% 5d \ n", liter, diesel, premium, super); liter + +;) printf ("--------------------------- \ n ");) G. Statements do-while do-while statement is basically the same as while statement, which is used to repeat the execution of certain statements during certain conditions are met, only the location of the test condition is at the end of the statement. The general form of this statement is: do statement; while (expression); Where expression is the expression of an ordinal (returns an integer value). Statement will be repeated during the expression of value is not 0. Algorithm this statement is: repeat statement if (expression) goto repeat; endif Here is the use of do-while statement instead of if and goto statements in the program 4.1. Code: # include int liter, diesel, premium, super; main () ( printf ( "Solar Liter Super Premium \ n"); printf ("--------------------------- \ n "); liter = 1; do (Solar = liter * 250; Premium = liter * 385; liter * super = 450; printf ( "% 5d% 5d% 5d% 5d \ n", liter, diesel, premium, super); liter + +; ) While (liters <= 10) printf ("--------------------------- \ n ");) H. Statement for statement for essentially the same as while statement, which is used to repeat the execution of certain statements during certain conditions are met, only where there is a statement for the pre and post implementation of the statement. The general form of this statement is: for ([prestatement]; [expression]; [poststatement]) statement; algorithm of this statement is:. prestatement; repeat: statement; poststatement; if (expression) goto repeat; endif The following is a statement for use to replace if and goto statements in the program 4.1. Code: # include int liter, diesel, premium, super; main () ( printf ( "Solar Liter Super Premium \ n"); printf ("--------------------------- \ n "); for (liters = 1; liter <= 10; liters + +) (Solar = liter * 250; Premium = liter * 385; liter * super = 450; printf ( "% 5d% 5d% 5d% 5d \ n", liter, diesel, premium, super); ) printf ("--------------------------- \ n "); ) Statement liters = 1 can be defined as prestatement and statements liters + + as poststatement. With so then it can be written by using the statement for a more simpler. I. Break and continue statements Break statement is used to control the program flow in a statement switch-case-default, while, do-while, and for, namely to move to the next statement. Continue statement is used to control flow of programs on the next iteration of the while statement, do-while, and for where he is.
Tidak ada komentar:
Posting Komentar