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語言條件語句示例

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377740.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377740.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377740.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

十四、聚類實戰——圖片壓縮

對同一像素點值的像素點歸為一類&#xff0c;通過平均值進行取代&#xff0c;從而將圖像進行壓縮并且保證圖像盡可能不失真&#xff0c;關鍵信息仍保留。 from PIL import Image import numpy as np from sklearn.cluster import KMeans import matplotlib import matplotlib.…

步驟菜單使用css3實現

代碼庫&#xff1a;http://thecodeplayer.com/walkthrough/css3-breadcrumb-navigation 有興趣的可以看一下&#xff0c;看完絕對讓你大飽眼福。首先截圖&#xff0c;看效果看著很酷吧&#xff0c;其實實現起來也不是很難&#xff0c;里邊需要用的技術有:box-shadow,計數器&…

【嵌入式系統】STM32串口通信的四種方法(基于RTOS)

目錄1、串行通信的基本參數2、輪詢方式代碼效果3、中斷方式代碼效果4、中斷加上時間戳方式代碼及效果5、DMA空閑中斷方式接收數據1、串行通信的基本參數 串行端口的通信方式是將字節拆分成一個接一個的位再傳輸出去&#xff0c;接收方再將此一個一個的位組合成原來的字符&…

大數據 java 代碼示例_Java變量類型與示例

大數據 java 代碼示例Java變量 (Java variables) Variables are the user-defined names of the memory blocks, and their values can be changed at any time during program execution. They play an important role in a class/program as they help in to store, retrieve…

畢業設計

位置跟蹤系統工作原理&#xff08;博聞網&#xff09; http://science.bowenwang.com.cn/location-tracking.htm Azuma是這樣定義增強現實的 :虛實結合 ,實時交互 ,三維注冊 環境搭建&#xff1a; http://cvchina.net/thread-173-1-1.html http://blog.csdn.net/jdh99/article/…

十五、聚類的評估

一、Given Label 均一性homogeneity&#xff1a;一個簇中只包含一個類別樣本&#xff0c;Precision 完整性completeness&#xff1a;同類別樣本被歸到同一個簇中&#xff0c;Recall 將均一性h和完整性c進行結合(二者加權平均)得到V-Measure&#xff0c;&#xff0c;β為權重 …

SQL SERVER作業的Schedules淺析

SQL SERVER作業的計劃&#xff08;Schedules&#xff09;&#xff0c;如果你沒仔細研究過或沒有應用一些復雜的計劃&#xff08;Schedules&#xff09;&#xff0c;那么你覺得SQL SERVER作業的計劃(Schedules)非常好用&#xff0c;也沒啥問題&#xff0c;但是我要告訴你一個“殘…

leetcode 51. N 皇后 思考分析

目錄題目思考AC代碼題目 n 皇后問題研究的是如何將 n 個皇后放置在 nn 的棋盤上&#xff0c;并且使皇后彼此之間不能相互攻擊。 思考 首先以N4為例&#xff0c;畫出解空間樹的一部分&#xff1a; 根據模板&#xff1a; void backtracking(參數) {if(終止條件){存放結果…

Django實戰(18):提交訂單

前面的內容已經基本上涵蓋了Django開發的主要方面&#xff0c;我們從需求和界面設計出發&#xff0c;創建模型和修改模型&#xff0c;并通過scaffold作為開發的起點&#xff1b;在scaffold的基礎上重新定制模板&#xff0c;并且通過Model類和Form類對用戶輸入的數據進行校驗。我…

No module named ‘tensorflow.examples‘解決方案

想從tensorflow中導入mnist手寫數字數據集&#xff0c;結果報錯 from tensorflow.examples.tutorials.mnist import input_data import tensorflow.compat.v1 as tf tf.disable_v2_behavior()my_mnist input_data.read_data_sets("MNIST_data_bak/", one_hotTrue)&…

julia example_使用Julia中的Example的sign()函數

julia exampleJulia| sign()函數 (Julia | sign() function) sign() function is a library function in Julia programming language, it returns the sign of the given value in the form of -1/1. sign()函數是Julia編程語言中的庫函數&#xff0c;它以-1 / 1的形式返回給…

.NET通用基本權限系統

DEMO下載地址&#xff1a; http://download.csdn.net/detail/shecixiong/5372895 一、開發技術&#xff1a;B/S(.NET C# ) 1、Windows XP以上 (支援最新Win 8) 2、Microsoft Visual Studio 2010/2012 C#.NET 3、.NET Framework 4.0以上 (支援最新4.5版本) 4、SQL Server 2005以…

leetcode 37. 解數獨 思考分析

目錄題目核心思路的不斷細化1、核心框架2、考慮到每個位置的工作3、考慮到到達最后一列、該位置的數已經預置的情況4、判斷是否符合規則的函數5、確定遞歸終止條件確定函數返回值AC代碼題目 編寫一個程序&#xff0c;通過填充空格來解決數獨問題。 一個數獨的解法需遵循如下規…

快速完成兼職外包開發任務

做了很多年的開發相關的工作&#xff0c;做過兼職開發&#xff0c;也做過外包一些開發項目。 兼職人員角色時 正是經歷這些事情時&#xff0c;每次就要提前很費經的跟公司溝通&#xff0c;讓他們把公司內部的svn開發出去&#xff0c;但是就是很難&#xff0c;會涉及到安全各方的…

使用YOLOv5訓練NEU-DET數據集

一、下載YOLOv5源碼和NEU-DET(鋼材表面缺陷)數據集 YOLOv5源碼 NEU-DET(鋼材表面缺陷)數據集 這里的數據集已經經過處理了&#xff0c;下載即可 若通過其他途徑下載的原始數據集標簽為xml格式&#xff0c;需要轉化為txt格式XML轉txt格式腳本 二、數據集準備 NEU-DET(鋼材表…

kotlin獲取屬性_Kotlin程序獲取系統MAC地址

kotlin獲取屬性The task is to get system MAC address. 任務是獲取系統MAC地址。 package com.includehelpimport java.net.InetAddressimport java.net.NetworkInterface//Function to get System MACfun getSystemMac(): String? {return try {val OSName System.getProp…

帶分頁功能的SSH整合,DAO層經典封裝

任何一個封裝講究的是&#xff0c;使用&#xff0c;多狀態。Action&#xff1a;任何一個Action繼承分頁有關參數類PageManage&#xff0c;自然考慮的到分頁效果&#xff0c;我們必須定義下幾個分頁的參數。并根據這個參數進行查值。然后在繼承ServiceManage&#xff0c;Service…

在windows phone Mango中使用原生代碼開發程序

本文不討論創建可執行的exe程序,主要想說明怎么在silverlight程序里面調用由原生代碼所編寫的DLL(C / ARM). 原生代碼可以調用更多的API,但是這并不是說你就能隨意獲得那些你沒有權限的資源,比如,你可以使用CopyFile這個API,但是如果你試圖把文件Copy到\Windows文件夾,就會得到…

leetcode 198. 打家劫舍 思考分析

目錄1、題目2、求解思路3、代碼1、題目 你是一個專業的小偷&#xff0c;計劃偷竊沿街的房屋。每間房內都藏有一定的現金&#xff0c;影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統&#xff0c;如果兩間相鄰的房屋在同一晚上被小偷闖入&#xff0c;系統會自動…

找不到Windows照片查看器解決方法

桌面創建一個txt文本 復制這些命令&#xff0c;之后將后綴改為.reg&#xff0c;右擊管理員身份運行即可 Windows Registry Editor Version 5.00 ; Change Extensions File Type [HKEY_CURRENT_USER\Software\Classes\.jpg] "PhotoViewer.FileAssoc.Tiff" ; Change E…