Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use WeChat Mini Programs's if, switch, for and while statements

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use WeChat Mini Programs's if, switch, for and while sentences". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use WeChat Mini Programs's if, switch, for and while sentences.

If statement

In WXS, you can use if statements in the following format:

If (expression) statement: executes statement when expression is truthy.

If (expression) statement1 else statement2: executes statement1 when expression is truthy. Otherwise, execute statement2

If... Else if... Through this sentence pattern, else statementN can choose between statement1 and statementN to execute.

Example syntax:

/ / if... if (expression) statement; if (expression) statement; if (expression) {code block;} / / if. Else if (expression) statement; else statement; if (expression) statement; else statement; if (expression) {code block;} else {code block;} / / if. Else if... Else... if (expression) {code block;} else {code block;} switch statement

Example syntax:

Switch (expression) {case variable: statement; case number: statement; break; case string: statement; default: statement;}

The default branch can be omitted or not written.

Case keywords can only be used after: variables, numbers, strings.

Sample code:

Var exp = 10 switch (exp) {case "10": console.log ("string 10"); break;case 10: console.log ("number 10"); break;case exp: console.log ("var exp"); break;default: console.log ("default");}

Output:

Number 10for statement

Example syntax:

For (statement; statement) statement; for (statement; statement) {code block;}

The use of break,continue keywords is supported.

Sample code:

For (var I = 0; I

< 3; ++i) { console.log(i); if( i >

= 1) break;}

Output:

01while statement

Example syntax:

While (expression) statement; while (expression) {code block;} do {code block;} while (expression)

When the expression is true, the statement or block of code is executed in a loop.

The use of break,continue keywords is supported.

At this point, I believe you have a deeper understanding of "how to use WeChat Mini Programs's if, switch, for and while sentences". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report