什么是if语句并在JavaScript,PHP,Python,Java,C / C ++,C#,PowerShell,Bash编程语言中使用...

本文详细介绍了编程语言中If语句的基本语法和使用,包括Java、PHP、Python、JavaScript、C/C++及C#的If语句示例。无论是在条件判断、逻辑控制还是流程决策中,If语句都是不可或缺的部分,它允许根据不同的条件执行不同的代码块。
摘要由CSDN通过智能技术生成

All programming languages provide conditional statements in order to change the flow of the programs according to given conditions. If statement is most popular conditional statement provided very similar most of the programming languages. If statements provide single or most flow path according to given conditions like count, size, text, etc.

所有编程语言都提供条件语句,以便根据给定条件更改程序流。 If语句是最流行的条件语句,它提供了与大多数编程语言非常相似的条件。 如果语句根据计数,大小,文本等给定条件提供单个或大多数流路径。

常规If语句语法 (General If Statement Syntax)

As stated previously If statement for different programming languages has very similar syntax. Below is the generic syntax which is used in If statement.

如前所述,用于不同编程语言的If语句具有非常相似的语法。 以下是If语句中使用的通用语法。

if (CONDITION){
   CODE
}
elseif(CONDITION){
   CODE
}

...
else{
   CODE
}
  • `if` is used for the start of the statement

    如果将if用作语句的开头
  • `CONDITION` is the condition of it should be met

    “条件”是应满足的条件
  • `elseif` is used to provide an extra branch according to new condition. There may be extra `elseif` with new conditions

    “ elseif”用于根据新条件提供额外的分支。 可能还有其他带有新条件的“ elseif”
  • `else` is used to but the last branch if previously defined branches or conditions do not meet.

    如果以前定义的分支或条件不满足,则使用“ else”来代替最后一个分支。

Java If语句 (Java If Statement)

We can use if statements in Java like below.

我们可以使用Java中的if语句,如下所示。

单一If陈述式 (Single If Statement)

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen.

我们将使用time条件,如果该time条件小于18,我们将在屏幕上打印Good Day

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}

多个If语句 (Multiple If Statements)

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen if not we will print Good evening to the screen.

我们将使用以下time条件:如果该time条件小于18,我们将在屏幕上打印“ Good Day ,否则将在屏幕上打印“ Good evening ”。

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
} 
else {
  System.out.println("Good evening.");
}

PHP If语句 (PHP If Statement)

We can implement PHP if statement like below.

我们可以实现PHP if语句,如下所示。

单一If陈述式 (Single If Statement)

We will use single if statement which will print salute according to the time of the day.

我们将使用单个if语句,该语句将根据一天中的时间打印致敬词。

$time = 22;
if ($time < 18) {
  echo "Good day.";
}

多个If语句 (Multiple If Statements)

We can also use multiple if statements like below.

我们还可以使用多个if语句,如下所示。

$time = 22;
if ($time < 18) {
  echo "Good day.";
} 
else {
  echo "Good evening.";
}

Python If语句 (Python If Statement)

单一If陈述式(Single If Statement)

We can use a single if statement in Python like below.

我们可以在Python中使用单个if语句,如下所示。

time = 22
if time < 18 :
  print("Good day.")

多个If语句 (Multiple If Statements)

We can also use multiple if statements like below.

我们还可以使用多个if语句,如下所示。

time = 22;
if  time < 18:
  print("Good day.")
else:
  print("Good evening.")

JavaScript If语句 (JavaScript If Statement)

We can implement Python if statement like below.

我们可以像下面这样实现Python if语句。

LEARN MORE  Powershell If-Elseif-Else Conditional Statement
了解更多Powershell If-Elseif-Else条件语句

单一If陈述式(Single If Statement)

We can use a single if statement in JavaScript like below.

我们可以在JavaScript中使用单个if语句,如下所示。

val time = 22;
if (time < 18) {
  console.print("Good day.");
}

多个If语句 (Multiple If Statements)

We can also use multiple if statements in JavaScript like below.

我们还可以在JavaScript中使用多个if语句,如下所示。

val time = 22;
if (time < 18) {
  console.print("Good day.");
}  else {
  console.print("Good evening.");
}

C / C ++ If语句 (C/C++ If Statement)

C and C++ programming languages provide if statement like below.

C和C ++编程语言提供如下的if语句。

单一If陈述式 (Single If Statement)

Single if statement will be like below.

单个if语句将如下所示。

int time=22;

if( time < 18 )
   printf("Good day.");

多个If语句 (Multiple If Statements)

Multiple if statements will be like below.

多个if语句将如下所示。

int time=22; 

if( time < 18 ){
   printf("Good day.");}
else{
   printf("Good evening.");}

C#If语句 (C# If Statement)

C# provides very similar syntax to the Java for the If statement.

C#为If语句提供了与Java非常相似的语法。

单一If陈述式 (Single If Statement)

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen.

我们将使用time条件,如果该time条件小于18,我们将在屏幕上打印Good Day

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}

多个If语句 (Multiple If Statements)

We can use multiple if statements for C# like below.

我们可以对C#使用多个if语句,如下所示。

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}  else {
  System.out.println("Good evening.");
}

PowerShell If语句 (PowerShell If Statement)

单一If陈述式(Single If Statement)

Single if statement for the PowerShell will be like below.

PowerShell的单个if语句如下所示。

$time = 22

if($time -le 18){
   write-host("Good day.")
}

多个If语句 (Multiple If Statements)

Multiple if statement for the PowerShell will be like below.

PowerShell的多个if语句如下所示。

$time = 22 

if($time -le 18){ 
   write-host("Good day.") 
}
else { 
   write-host("Good evening.") 
}
 

翻译自: https://www.poftut.com/what-is-if-statement-and-use-in-javascript-php-python-java-c-c-c-powershell-bash-programming-languages/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值