This time, the first topic of the lesson is Selection. Selection means pilihan/seleksi in Indonesia. It means that there is a criteria that needs to be fulfill. There are 3 type of selection in general and in C programming language, “if”, “if-else”, and “switch case”.
“If” is used to do selection with actions only on True value. This is the syntax:
if (boolean expression) {
statement1;
statement2;
…
}
This type of “if” only does the statements if the value of the boolean expression is True.
“if-else” is used to do selection with actions on both True and False values. Here is the syntax:
if(boolean expression){
statement1;
statement2;
….
}
else{
statement1;
statement2;
….
}
So, using “if-else” will generate actions on each value (True or False) on the boolean expression. If the boolean expression is True then only the statements inside “if” bracket is done. If the boolean expression is False then only the statements inside “else” bracket is done.
There are also another type of “if-else” selection which is “nested if”.
“nested if” is used to do selection with more than one criteria (boolean expression). Here are the syntax:
if(boolean expression 1){
statement1;
….
}
else if(boolean expression 2){
statement1;
….
}
else{
statement1;
….
}
So, using “nested if” will generate different actions depending on the values of the boolean expressions. If boolean expression 1 is True then only statements inside “if” bracket is done. If boolean expression 1 is False but boolean expression 2 is True then only statements inside “else if” bracket is done. If both boolean expression is False, then only the statements inside “else” bracket is done.
“switch case” is used to do selection only when the criteria is an exact value.
Only integer (int) and character (char) that can use “switch case” selection. “switch case” selection also have to use constant value and “break”. Here is the syntax:
switch (expression/variable){
case (constant1):
statement1;
….
break;
case (constant2):
statement1;
….
break;
default:
statement1;
…..
}
So, using “switch case” will generate different actions for each constants. If the expression/variable is the same with constant1, then only statements under the first case is done. If the expression/variable is the same with constant2, then only statements under the second case is done. If there are no constants that has the same value as the expression/variable, then only statements under the default case is done.
There are also “ternary operator” that is used just like a simple “if-else”. Here is the syntax:
condition (boolean expression) ? (then expression) : (else-expression)
Ternary operator is basically a simple way to write an “if-else” code.
We also found out about “go to” and “label” in C programming language. However, “go to” and “label” are rarely used this days due to the inefficient method that makes the source code is hard to be read and hard to track the errors as well.
We also learned about types of errors. First is compile-time error which is caused by wrong syntax. This type of error is the most common one. Some code-editing program gives us the error list for this type of error. Second, link-time error which is caused by wrong link. This may caused the program can’t find the correct link. Third, run-time error, which is caused by error during the run-time of the program. Forth and last, is logical error, which is caused by the wrong logic of the program. This may cause a wrong answer/output when the program is execute.
That is all for today. See you on the next meeting. Cheers!