1)簡化形式
if(表示式) 語句塊
如: if(x>y) printf(“%d”,x);
2)一般形式
if (表示式)
語句塊1
else
語句塊2
例如:
if (x>y)
printf(“max=%d”,x);
printf(“max=%d”,y);
3)巢狀形式
含義:一條if語句重又包含另一個if語句稱為if語句的巢狀.
格式:
if( )
if( ) 語句塊1
else 語句塊2
if( ) 語句塊3
else 語句塊4
if (score>90)
printf(“very good”);
if(score>80)
printf(“good”);
if(score>60)
printf(“ok”);
printf(“bad”);
如果語句塊包含多個語句,用{}就行了
例如:
if(n>0)
{
printf("%d",n);
printf("是正數\n");
}
1)簡化形式
if(表示式) 語句塊
如: if(x>y) printf(“%d”,x);
2)一般形式
if (表示式)
語句塊1
else
語句塊2
例如:
if (x>y)
printf(“max=%d”,x);
else
printf(“max=%d”,y);
3)巢狀形式
含義:一條if語句重又包含另一個if語句稱為if語句的巢狀.
格式:
if( )
if( ) 語句塊1
else 語句塊2
else
if( ) 語句塊3
else 語句塊4
例如:
if (score>90)
printf(“very good”);
else
if(score>80)
printf(“good”);
else
if(score>60)
printf(“ok”);
else
printf(“bad”);
如果語句塊包含多個語句,用{}就行了
例如:
if(n>0)
{
printf("%d",n);
printf("是正數\n");
}