Más contenido relacionado La actualidad más candente (20) Más de Dimitris Psounis (20) ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 11 (ΕΚΤΥΠΩΣΗ)1. H C
11:
.
1. continue
2. break
3. goto
4. switch
.
1. menu
2. !
3. " " # $
%.
.
1. ! continue
!
" # $ % ! (% % & '
for, while, do…while).
( ) & , # ' , % & # '
& ! , * ' +
! .
, " # , continue. # * '
+ ! .
- # & continue ( . . & for) ) , *
'$% ' :
% ) # % , # ,
continue & + ! .
for (i=0; i<N; i++)
{
...
if ( )
continue;
...
}
.
1. ! continue
"
/* continue.c: Deixnei tin xrisi tis continue */
#include <stdio.h>
main()
{
int i;
for (i=0; i<10; i++)
{
printf("nArithmos: %d",i);
if (i%2==0)
continue;
printf(", kai to tetragono tou: %d",i*i);
}
}
2. .
2. % ' ! break
#
" # $ % ! (% % & '
for, while, do…while).
( ) & , # ' , & # % ' ,
! .
, " # , break. # % '
! ' , # $ ! .
- # & break ( . . & for) ) , *
'$% ' :
% ) # % , # ,
break & + ! .
for (i=0; i<N; i++)
{
...
if ( )
break;
...
}
.
2. % ' ! break
$
/* break.c: Deixnei tin xrisi tis break */
#include <stdio.h>
#define N 5
main()
{
int i;
int A[N]= {5, 3, 2, 4, 8 }; // Pinakas dedomenwn
int x=2; // Pros anazitisi stoixeio
/* Psaxnoume gia to stoixeio x ston pinaka A */
for (i=0; i<N; i++)
{
if (A[i]==x)
break;
}
if (i<N)
printf("nVrethike to stoixeio %d sti thesi %d",x,i);
else
printf("nDen Vrethike to stoixeio");
}
.
3. ) & # goto
%
# % ' ," ' , # + , & # ' ' &
, goto.
# ) # '& (' ,. # & « % »
) # ' ' :
/ ' ,. # , ' # "-' " , .
( & #% # # ) # ! # :
( # + , , # & # ! '& '
' & & ' , , .
, % ) " goto ! #
& , ! .
:
goto ;
.
3. ) & # goto
&
/* goto.c: Deixnei tin xrisi tis goto */
#include <stdio.h>
main()
{
int i=0;
label: // To onoma tis etiketas
printf("ni=%d",i);
if (i<5)
{
i++;
goto label;
}
}
3. .
3. ) & # goto
'
' goto ) , ' + , ' , , '
' , & ' $ .
& '& ' # %# # , $ '&
'& # & # ' goto.
0 ' '$% ' , ' , '$% ' - ' , ' #
% # '& ' goto.
# # # # :
0, % , & goto.
, &, ' ' ) , & !
.
4. & # # ' switch
(
switch ' #' , & # ' & #
% ) '& & # & + .
1& # % ' # # +&+ % % '& if…else if…else.
switch , ' $ ' , ' ' ,
%," & # & # & * # , 2.
% % % ) . + & # % % '& if…else #
' % ) '& & '& + x:
* + & # " # # switch:
& & ) # + .
% % ' case / : & # & '$% ' &* #
+ .
/ ' else ) switch default
& ' case ) # break.
.
4. & # # ' switch
if (x==1)
{
;
}
else if (x==2)
{
;
}
else if (x==3)
{
;
}
else if (x==4)
{
;
}
else
{
;
}
switch(x)
{
case 1:
;
break;
case 2:
B;
break;
case 3:
;
break;
case 4:
;
break;
default:
;
}
.
4. & # # ' switch
switch , , # % % & " # $ '&
( . . int ' char) ' '& & ( . . float double)
&* % , break & ' case.
0, , ! % ! # , ' & # , '
case, & # , break, & ) # , switch
# , & + ' ' .
2 ! # break % & ' , & break # % '
+ #. - " ' + , )& break
%, ) '$% ' .
% ) , % &+
, break & ' case!
, + , case " # # $
break.
4. .
4. & # # ' switch
!
/* wrong_switch.c: Deixnei mia kaki xrisi tis switch xwris ta aparaitita break */
#include <stdio.h>
main()
{
int x;
scanf("%d",&x);
switch(x)
{
case 1:
printf("nEisagate 1");
case 2:
printf("nEisagate 2");
case 3:
printf("nEisagate 3");
case 4:
printf("nEisagate 4");
default:
printf("nEisagate arithmo >=5");
}
}
3. '& / '&
1. & 3 ' ' # menu
"
switch , # %# & & + ' ' #
menu % . % ) ' # & # .
,
0, # & , & C % ,. % ) '&
& # . & ! # # # '
' % ) ' .
4 & & + – % % ! # % $ &.
# ' ' & , # ' while(1) # , + # $
' & # + # " # ' & exit() , # .
3. '& / '&
1. & 3 ' ' # menu
#
/* menu.c: Kataskeyi menu me tin switch */
#include <stdio.h>
#include <stdlib.h>
void func1();
void func2();
void func3();
main()
{
int epilogi;
while(1)
{
printf("nMenu");
printf("n-------------");
printf("n1-Epilogi 1");
printf("n2-Epilogi 2");
printf("n3-Epilogi 3");
printf("n4-Exit");
printf("n-------------");
printf("nEpilogi? ");
scanf("%d",&epilogi);
switch(epilogi)
{
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
case 4:
printf("nExodos apo to
programma");
exit(0);
break;
default:
printf("nLanthasmeni Epilogi");
}
system("pause");
}
}
3. '& / '&
2. 5 * %
$
# :
- & + + ' # " stdlib.h
, ' , ,. .
& " # , &! # # ' .
6 # & # # ' # '
.
" # &) , # # ' , &
' # exit &) & " , # ' .
( + 0, , # &* % , $
1, , # &* % .
void exit(int message)
5. 3. '& / '&
3. 0' & 0 $ #
%
# % ' ," ' & # # #
- & + + ' # " stdlib.h
# & # ' # %& " "
# + .
' * ) # # # , # ) & #
(% * ' # ' ), , # & ,
:
, ' , # pause # ' # « $ »
& # Enter.
' :
# «' ,. »
void system(char *entoli)
system("pause");
system("cls");
. '
1. / , #
&
« 9 0 " 0, % / * % % & " – 0) 1» ' ' # &
, , 4 & , % # .
/ $ , switch , % % '& if…else.
. '
2. ( ' # 0 $
'
« 8: ,' – 0) 5» ' ' # & , '
# * , ' .
/ $ , switch ' ' # # ,
% % '& if…else.
7 ) ,. , ' # , % .
. '
3. ( ' # 0 $
(
# ,. # , ) " * :
' # , « $ »
system(“pause”) $ ) ,. & " ' & # $ . 7 %,
Enter # , .
7 ' ,. '' ' " # system(“cls”)