yaitu contoh program membuat data pribadi.
Berikut Penjelasan Mengenai Fungsi Switch-Case dalam Bahasaa C:
-----------------------------------------------------------------------------
The C switch Statement
The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.
- selection-statement:
- switch (expression)statement
- labeled-statement:
- case constant-expression : statement
default : statement
Use of the switch statement usually looks something like this:
switch ( expression )
{
declarations
.
.
.
case constant-expression :
statements executed if the expression equals the
value of this constant-expression
.
.
.
break;
default :
statements executed if expression does not equal
any case constant-expression
}
Decision
making are needed when, the program encounters the situation to choose a
particular statement among many statements. If a programmar has to
choose one among many alternatives if...else can be used but, this makes
programming logic complex. This type of problem can be handled in C
programming using switch...case statement.
Syntax of switch...case
switch (expression) { case constant1: codes to be executed if expression equals to constant1; break; case constant2: codes to be executed if expression equals to constant3; break; . . . default: codes to be executed if expression doesn't match to any cases; }- See more at: http://www.programiz.com/c-programming/c-switch-case-statement#sthash.Gar6Jq86.dpuf
You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.
The default statement is executed if no case constant-expression is equal to the value of switch ( expression ). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. A case or default label can only appear inside a switch statement.
The type of switch expression and case constant-expression must be integral. The value of each case constant-expression must be unique within the statement body.
The case and default labels of the switch statement body are significant only in the initial test that determines where execution starts in the statement body. Switch statements can be nested. Any static variables are initialized before executing into any switch statements.
Note |
---|
Declarations can appear at the head of the compound statement forming the switch body, but initializations included in the declarations are not performed. The switch statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations. |
switch( c ) { case 'A': capa++; case 'a': lettera++; default : total++; }
switch( i ) { case -1: n++; break; case 0 : z++; break; case 1 : p++; break; }
A single statement can carry multiple case labels, as the following example shows:
case 'a' : case 'b' : case 'c' : case 'd' : case 'e' : case 'f' : hexcvt(c);
Microsoft Specific
Microsoft C does not limit the number of case values in a switch statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in a switch statement.
The default for Microsoft C is that the Microsoft extensions are enabled. Use the /Za compiler option to disable these extensions.
END Microsoft Specific
Example again:
#include<stdio.h>
int main()
{
switch (pilih);
{
case '1':
printf("Masukan nama anda: ");
scanf("%s", &masukan);
printf("Nama anda: %s\n",masukan);
break;
case '2':
printf("Masukan bulan lahir anda: ");
scanf("%s", &masukan);
printf("Anda lahir bulan: %s\n",masukan);
break;
case '3':
printf("Masukan nama kota anda berasal: ");
scanf("%s", &masukan);
printf("Anda berasal dari kota: %s\n",masukan);
break;
default:
printf("Masukan anda salah\n");
}
}
Posting Komentar