աäǺȷҧ ( if , if-else , switchcase )
if
if-else
switch...case
Ẻ֡Ѵ ͧ "¹¤ if-else"
Ẻ֡Ѵ ͧ "¹¤ switch...case"

ٻẺ觤Ǻȷҧ

        觤Ǻȷҧ÷ӧҹͧ 㹡óշҾ⨷ѭѡɳзշҧ͡ ͹㹡͡ӧҹ ʶҹó A ӧҹҧ˹ ǹʶҹó B ӧҹաҧ˹᷹ ͶҵŢѺ繨ӹǹ ٳӹǹ鹴 2 ҵŢѺ繨ӹǹ ¹èӹǹ鹴 2 繵
        C 觷ѺǺȷҧ÷ӧҹͧ ö 3 ѡ if , if-else Ф switchcase лٻẺ¡觴ѧ

if

     if 㹡óշշҧ͡ӧҹ§ҧ͡ ¶ҵǨͺ͹繨ԧ ֧зӧҹ ٻẺ¡ if ʴѧ

if(condition) statement;

condition : ͹䢷˹ ѴԹҨзӵ ͹䢨еͧ¹ͧ ( ) ͹ҨٻͧԾ äӹdzºº 繤Ңͧá
statement : 觷ӧҹҼšõǨͺ͹͡繨ԧ Ҩդ觷ͧӧҹҡ˹觤觡 ͧ¹ҹͧ { }

if (condition)
{
         statement_1;
         statement_2;
         statement_3;
          
         statement_n;
}

ҧ¹ if ͵Ǩͺ͹ ʴѧ

if(number = = 25) printf(Youre right);

ҵ number դҡѺ 25 ʴͤ Youre right

 

if -else

      if-else 㹡óշշҧ͡ӧҹ 2 ҧ͡ ¡÷ӧҹͧ if-else ҡõǨͺ͹ Ҽ͡繨ԧзӧҹ觷ѧ if ҡõǨͺ͹䢼͡ ӧҹ觷ѧ else ᷹ ٻẺ if-else ʴѧ
if (condition)
     statement;
else
     statement;



if (condition)
{
     statement_1;
     statement_2;
     statement_3;
     
     statement_n;
}
else
{
     statement_1;
     statement_2;
     statement_3;
     
     statement_n;
}

ҧ¹ if-else ʴѧ

if(a%2 = = 0)
printf(Even number);

else
printf(Odd number);

 


ҤҢͧ a ô 2 ŧ ( 0) ʴͤ Even number

͹䢢ͧ if (a ô 2 ŧ) ʴͤ Odd number


switch...case

switchcase 㹡óշշҧ͡ӧҹ·ҧ õǨͺ͹ѹ§ šõǨͺ͹䢨ж١仾Ԩó͡Ҩзӧҹҧ͡ ٻẺҹ switchcase ʴѧ

switch(variable)
{
case constant_1 : statement;
                            break;
case constant_2 : statement;
                           break;
case constant_3 : statement;
                          break;
              
case constant_n : statement;
                         break;
default : statement;
                         break;
}


variable :
êԴ int char Ҩ繹Ծäӹdz͡ int char µùж١͹㹡͡ӧҹ

constant_1, constant_2, constant_3,, constant_n : Ҥ誹Դ int char µͧ繪ԴǡѺҢͧ variable ҤҢͧ variable çѺ constant ͧ case зӧҹ(statement ) ͧcase

break : Ѻ͡ҡ÷ӧҹͧ switchcase 觤è㹵͹¢ͧء case ͡ҡ switch ѹ ѧҡӧҹ case ͹䢵çѹ ͧǹǨͺ͹ ͨ

default : 繤觷еͧӧҹ ͹͡ҡ switch ѧ ҤҢͧ variable çѺҢͧ constant case ҧͧҷӧҹ觢ͧ default ͹͡ҡ switch


ҧ¹ switchcase йӵô ʴѧ

#include<stdio.h>
#include<conio.h>
char grade;
main()
{
printf("Enter your grade (A,B,C,D OR F) : ");
scanf("%c",&grade);
switch(grade)                       ....................................>
{
case 'A' :printf("Excellent");   ....................................>
            break;
case 'B' :printf("Good");       .....................................>
            break;
case 'C' :printf("Ok");         ......................................>
            break;
case 'D' :printf("Improved"); ....................................>
            break;
case 'F' :printf("You must do better than this");   .........>
            break;

default : printf("What is your grade?");     ................>
            break;
}
getch();
}









grade Դ char ж١͹㹡͡ӧҹ
grade դҡѺ A ʴͤ Excellent

grade դҡѺ B ʴͤ Good

grade դҡѺ C ʴͤ Ok

grade դҡѺ D ʴͤ Improved

grade դҡѺ F ʴͤ You must do better than this

grade çѺ case ʴͤ What is your grade?