Hướng dẫn control statements in javascript - geeksforgeeks - câu lệnh điều khiển trong javascript - geeksforgeeks

Tuyên bố IF-Else hoặc có điều kiện sẽ thực hiện một số hành động cho một điều kiện cụ thể. Nếu điều kiện đáp ứng thì một khối hành động cụ thể sẽ được thực thi nếu không nó sẽ thực thi một khối hành động khác đáp ứng điều kiện cụ thể đó. Các tuyên bố kiểm soát như vậy được sử dụng để khiến dòng thực thi tiến lên và chi nhánh dựa trên các thay đổi đối với trạng thái của một chương trình. if-else or conditional statement will perform some action for a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition. Such control statements are used to cause the flow of execution to advance and branch based on changes to the state of a program.

Ví dụ: Ví dụ này mô tả if-statement trong javascript.This example describes the if-statement in Javascript.

JavaScript

Syntax::

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}

Sơ đồ:

Hướng dẫn control statements in javascript - geeksforgeeks - câu lệnh điều khiển trong javascript - geeksforgeeks

tuyên bố điều kiện

Tuyên bố IF-Else: Câu lệnh IF một mình cho chúng ta biết rằng nếu một điều kiện là đúng thì nó sẽ thực thi một khối các câu lệnh và nếu điều kiện sai thì nó sẽ thắng. Nhưng điều gì sẽ xảy ra nếu chúng ta muốn làm điều gì đó khác nếu điều kiện là sai? Đây là tuyên bố khác. Chúng ta có thể sử dụng câu lệnh khác với câu lệnh IF để thực thi một khối mã khi điều kiện là sai.: This example describes the if-else statement in Javascript.

Tuyên bố IF-Else

if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.
5
if(condition) 
{
   // Statements to execute if
   // condition is true
}
0
if(condition) 
{
   // Statements to execute if
   // condition is true
}
1

if(condition) 
{
   // Statements to execute if
   // condition is true
}
2
if(condition) 
{
   // Statements to execute if
   // condition is true
}
3
if(condition) 
{
   // Statements to execute if
   // condition is true
}
4

if(condition) 
{
   // Statements to execute if
   // condition is true
}
2
if(condition) 
{
   // Statements to execute if
   // condition is true
}
6
if(condition) 
{
   // Statements to execute if
   // condition is true
}
7
if(condition) 
{
   // Statements to execute if
   // condition is true
}
8
if(condition) 
{
   // Statements to execute if
   // condition is true
}
9

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
4
if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.
1
if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
6
if(condition) 
{
   // Statements to execute if
   // condition is true
}
9

if(condition) 
{
   // Statements to execute if
   // condition is true
}
2
if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
9

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
4
if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.
1
if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.
2
if(condition) 
{
   // Statements to execute if
   // condition is true
}
9

if(condition)
   statement1;
   statement2;

// Here if the condition is true, if block 
// will consider only statement1 to be inside 
// its block.
4

Các câu lệnh có điều kiện của JavaScript::

i is smaller than 15

if-else-if thang JavaScript allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement. A nested if is an if statement that is the target of another if or else. 

Syntax::

if (condition1) 
{
   // Executes when condition1 is true
   if (condition2) 
   {
      // Executes when condition2 is true
   }
}

Sơ đồ:

Hướng dẫn control statements in javascript - geeksforgeeks - câu lệnh điều khiển trong javascript - geeksforgeeks

tuyên bố điều kiện

Tuyên bố IF-Else: Câu lệnh IF một mình cho chúng ta biết rằng nếu một điều kiện là đúng thì nó sẽ thực thi một khối các câu lệnh và nếu điều kiện sai thì nó sẽ thắng. Nhưng điều gì sẽ xảy ra nếu chúng ta muốn làm điều gì đó khác nếu điều kiện là sai? Đây là tuyên bố khác. Chúng ta có thể sử dụng câu lệnh khác với câu lệnh IF để thực thi một khối mã khi điều kiện là sai.: This example describes the nested-if statement in Javascript.

Tuyên bố IF-Else

Hướng dẫn control statements in javascript - geeksforgeeks - câu lệnh điều khiển trong javascript - geeksforgeeks

tuyên bố điều kiện

Tuyên bố IF-Else: Câu lệnh IF một mình cho chúng ta biết rằng nếu một điều kiện là đúng thì nó sẽ thực thi một khối các câu lệnh và nếu điều kiện sai thì nó sẽ thắng. Nhưng điều gì sẽ xảy ra nếu chúng ta muốn làm điều gì đó khác nếu điều kiện là sai? Đây là tuyên bố khác. Chúng ta có thể sử dụng câu lệnh khác với câu lệnh IF để thực thi một khối mã khi điều kiện là sai.: This example describes the if-else-if ladder statement in Javascript.

Tuyên bố IF-Else