Switch Case
A. Definition of Switch Case
Switch Case is a branching of program code where we compare the contents of a variable
with several values. If the comparison process returns true, the program code will run.
The Switch Case condition consists of 2 parts, namely the Switch command where there is a
variable name to be checked, as well as 1 or more Case commands, each for each value that
want to be checked.
B. The difference between switch case and if else is:
a. Switch cases are used to check character type (char) and integer(int) data.
b. If else is used for checking with (<,>,==).
C. Perbandingan Switch Case dan If Else
Basic Termas If-Else Switch Case
Conditional Expression Testing The if-else statement can test The switch statement tests
expressions based on a range expressions based on only one
of values or conditions. integer, an enumerated value,
or a string object.
Ideal for use in certain cases if-else conditional branches are if-else conditional branches are
great for variable conditions great for variable conditions
that produce a Boolean. that produce a Boolean.
Conditions & expressions Having different conditions is We can only have one
possible. expression.
D. Basic format for writing Switch Case conditions in C Language
Switch (nama_variabel) {
case ‘nilai_1’:
program code that is run if nama_variabel == nilai_1
Break;
case ‘nilai_2’:
program code that is run if nama_variabel == nilai_2
Break;
case ‘nilai_3’:
program code that is run if nama_variabel == nilai_3
Break;
...
...
default:
program code executed jiak no conditions met
A. Definition of Switch Case
Switch Case is a branching of program code where we compare the contents of a variable
with several values. If the comparison process returns true, the program code will run.
The Switch Case condition consists of 2 parts, namely the Switch command where there is a
variable name to be checked, as well as 1 or more Case commands, each for each value that
want to be checked.
B. The difference between switch case and if else is:
a. Switch cases are used to check character type (char) and integer(int) data.
b. If else is used for checking with (<,>,==).
C. Perbandingan Switch Case dan If Else
Basic Termas If-Else Switch Case
Conditional Expression Testing The if-else statement can test The switch statement tests
expressions based on a range expressions based on only one
of values or conditions. integer, an enumerated value,
or a string object.
Ideal for use in certain cases if-else conditional branches are if-else conditional branches are
great for variable conditions great for variable conditions
that produce a Boolean. that produce a Boolean.
Conditions & expressions Having different conditions is We can only have one
possible. expression.
D. Basic format for writing Switch Case conditions in C Language
Switch (nama_variabel) {
case ‘nilai_1’:
program code that is run if nama_variabel == nilai_1
Break;
case ‘nilai_2’:
program code that is run if nama_variabel == nilai_2
Break;
case ‘nilai_3’:
program code that is run if nama_variabel == nilai_3
Break;
...
...
default:
program code executed jiak no conditions met