c語言條件語句示例
PHP條件語句 (PHP Conditional Statements)
While coding, you may get to a point where your results can only be gotten when a condition is valid. We make use of conditional statements. Conditional statements are statements that can only be executed based on the fulfillment of a particular condition(s).
在編碼時,您可能會達到只有在條件有效的情況下才能得到結果的程度。 我們利用條件語句 。 條件語句是只能基于滿足特定條件才能執行的語句。
There are basically 4 different types of conditional statements in PHP,
在PHP中 ,基本上有4種不同類型的條件語句 ,
1)if語句 (1) The if statement)
With the if statement your code only executes only when the condition is true.
使用if語句,僅當條件為true時,您的代碼才執行。
Syntax:
句法:
if(condition){
//code to be executed when condition is true
}
Example:
例:
Let's check if a mark entered is greater than or equal to 80. If true an A grade is given.
讓我們檢查輸入的分數是否大于或等于80。如果為true ,則給出A成績。
PHP Code:
PHP代碼:
<?php
//defining a variable
$mark = 120;
if($mark >= 80){
echo "you have an A";
}
?>
Output
輸出量
you have an A
2)if ... else語句 (2) The if...else statements)
The if...else statement is used when a condition is satisfied and when it is not satisfied. So it's used when the condition is either true or false.
如果滿足條件和不滿足條件,則使用if ... else語句 。 因此在條件為true或false時使用 。
Syntax:
句法:
if (condition){
//code to be executed when true }
else {
//code to be executed when false
}
Example:
例:
Here, we are going to check if the letter entered is an F which will display female else we display male.
在這里,我們將檢查輸入的字母是否為F,該字母將顯示女性,否則顯示男性。
PHP Code:
PHP代碼:
<?php
//defining a variable
$gender = 'F';
if ($gender == 'F'){
echo "FEMALE";
}
else {
echo "MALE";
}
?>
Output
輸出量
FEMALE
3)if ... elseif ... else語句 (3) The if...elseif...else statements)
In a situation where you have several conditions, for example a program to grade students based on their marks with the letters A, B, C, D, F. the if...elseif...else is used for this.
在有幾種情況的情況下,例如,根據學生的成績給他們打上字母A,B,C,D,F的程序。if ... elseif ... else用于此目的。
Syntax:
句法:
if (condition1){
//code 1 to be executed
}
elseif(condition2) {
//code 2 to be executed
}
else{
//code to be executed if code 1 and code 2 are not true
}
Example:
例:
We are going to grade students with the letters A, B, C, D, F based on their marks on 100.
我們將根據學生在100上的分數為字母A,B,C,D,F評分。
PHP Code:
PHP代碼:
<?php
//defining a variable
$marks = 75;
if ($marks>79){
echo "A";
}
elseif($marks<=79&& $marks>60) {
echo "B";
}
elseif($marks<=60&& $marks>50) {
echo "C";
}
elseif($marks=50) {
echo "D";
}
else{
echo "F";
}
?>
Output
輸出量
B
4)嵌套的if ... else語句 (4) The nested if...else statements)
When you find if...else statements inside an if...else statement the statements are nested. With this statement, you can get alternatives results when a condition is true or false.
當您在if ... else語句中找到if ... else語句時,這些語句將嵌套 。 使用此語句,當條件為true或false時,您可以獲得替代結果。
Syntax:
句法:
if (condition 1 )
{
if (condition 2 )
{
// code1 to be executed
}
else
{
// code 2 to be executed
}
}
else
{
// code 4 to be executed
}
Example:
例:
Let's compare tow numbers using the nested if statement.
讓我們使用嵌套的if語句比較兩個數字。
PHP code:
PHP代碼:
<?php
// defining variables
$number1 = 40;
$number2 = 12;
if ($number1 != $number2) {
echo 'number1 is different from number2';
echo '<br>';
if ($number1 > $number2) {
echo 'number1 is greater than number2';
} else {
echo 'number2 is greater than number1';
}
} else {
echo 'number1 is equal to number2';
}
?>
Output
輸出量
number1 is different from number2
number2 is greater than number1
5)switch語句 (5) The switch statement)
The switch statement is very similar to the if...else statement. But in the cases where your conditions are complicated like you need to check a condition with multiple constant values, a switch statement is preferred to an if...else. The examples below will help us better understand the switch statements.
switch語句與if ... else語句非常相似。 但是在您的條件很復雜的情況下(例如,您需要檢查具有多個常量值的條件),對于if ... else,首選使用switch語句 。 下面的示例將幫助我們更好地理解switch語句 。
Syntax:
句法:
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
Example:
例:
Let's rewrite the example of if…..else statements using switch statements,
讓我們使用switch語句重寫if ..... else語句的示例,
<?php
//variable definition
$gender = 'M';
switch ($gender) {
case 'F':
echo 'F is FEMALE';
break;
case 'M':
echo 'M is MALE';
break;
default:
echo 'Invalid choice';
}
?>
Output
輸出量
M is MALE
翻譯自: https://www.includehelp.com/php/conditional-statements.aspx
c語言條件語句示例