c语言条件语句示例_PHP中的条件语句和示例

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语句 。 因此在条件为truefalse时使用

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语句时,这些语句将嵌套 。 使用此语句,当条件为truefalse时,您可以获得替代结果。

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语言条件语句示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值