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语句。
单一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.")
}