Swift getting started tutorial Series 7-control flow, swift getting started tutorial Series 7-

Source: Internet
Author: User

Swift getting started tutorial Series 7-control flow, swift getting started tutorial Series 7-
Control Flow
In general, the usage is similar to that of C, C ++, and Objective C.
The description here may be a little less, and you will use it when writing code. It is very simple. You can focus on switch. swift has more swift functions.
Note that the results of conditional expressions in swift must be of the Bool type.
That is to say
Var a = 19
If {
}
This expression is incorrect because a is of the Int type.

1. if

There are also three common if

if bool{}if bool{}else{}if bool{}else if{}else{}

2.
In swift, the for loop is divided into two types: for and for in.
For in
Here the index is implicitly declared as a constant and does not need to be declared in advance.
for index in 1...10{    println(index)}
If you do not need the specific index value, you can use underscores instead.
for _ in 1...10{    println("hello hwc")}
For in is most commonly used to traverse arrays and dictionaries.
var dictionary = [1:"one",2:"two",3:"three"]for (key,value) in dictionary{println("\(key)->\(value)")}
Of course, the for condition can also be used to increase progressively
for var index = 0;index < 10;++index{    println(index)}
3. while
while condition{    statements}var i = 0while  i < 10{   println(i)   i++}do{}while conditionvar i = 0do{    println(i)    i++}while i < 10

4 switch

switch temp{case value1:println(value1)case value2:println(value2)defalut:println(default)}

The switch statement in swift can automatically jump out without a break.
In swift, each case statement must execute at least one Local Area
So
Case value1:
Case value2:
Println ("")
Yes, an error is reported.

In swfit, If You Want To match multiple values,
switch oenValue{    case value1:println("1")    case value2...value4:println("234")    default:        println("default")}

In swift, Switch can directly match tuples, which is very convenient
The values in the tuples can be intervals or _ can be used to match any value.
Case (0, 0): Match (0, 0)
Case (0 ..) matching)
Case (_, 1) matches any (Int, 1)
For example
let somePoint = (1, 1)switch somePoint {case (0, 0):println("(0, 0) is at the origin")case (_, 0):println("(\(somePoint.0), 0) is on the x-axis")case (0, _):println("(0, \(somePoint.1)) is on the y-axis")case (-2...2, -2...2):println("(\(somePoint.0), \(somePoint.1)) is inside the box")default:println("(\(somePoint.0), \(somePoint.1)) is outside of the box")}


The branch statement of case can be used to add additional judgment through where
let somePoint = (1, 1)switch somePoint {case let(x,y) where x == y:println("x is equal to y")default:println("x is not equal to y" )}

5 continue break fallthrough return
Continue, terminate this operation in the loop body, and continue the next operation
for index in 1...10{   if index % 2 == 1{continue   }   println(index)}
Break to immediately end the execution of the current control flow
let x = 10switch x{    case 10:println("right")    default:        break}
Fallthrough, from a case in swift to the next case
In swift, after a case ends by default, the switch statement is displayed no matter whether there is a break or not. If you want to use the fallthrough keyword like other languages
let x = 0switch x{    case 0:println(0)fallthrough    case 1:println(1)    case 2:println(2)    default:    println("defalult")}
The output value is 0 1.

Statements with tags
When we have multi-layer loops or multi-layer switches, how do we know that a continue or break terminates the control flow? Swift provides a convenient method: tag-based statements
var x = 1var y = 1firstLoop:while x < 5{secondLoop:while y < 6{  //continue firstLoop or continue secondLoop}}





How to write the S7-300 PID continuous control flow program?

Give you a PID-controlled FC module written in Step 7. Variables with "_ IN" and with "_ OUT" must be connected to the same variable if the prefix is the same.

FUNCTION FC1: VOID

VAR_INPUT
Run: BOOL; // True-Run, False-stop
Auto: BOOL; // True-automatic, False-Manual
ISW: BOOL; // True-credit is valid, False-credit is invalid
DSW: BOOL; // True-differential valid, False-differential invalid
SetMV: REAL; // set the opening degree in manual mode.
SVSW: REAL; // when the set value is lower than SVSW, the opening degree is zero.
PV: REAL; // measured value
SV: REAL; // set the value.
DeadBand: REAL; // dead zone size
PBW: REAL; // proportional tape size
IW: REAL; // The credit band size.
DW: REAL; // the size of the differential band.
DErr_IN: REAL; // Error Accumulation
LastPV_IN: REAL; // measurement value of the previous control period
END_VAR

VAR_OUTPUT
MV: REAL; // output opening degree
DErr_OUT: REAL; // Error Accumulation
LastPV_OUT: REAL; // measurement value of the previous control period
END_VAR

VAR
Err: REAL; // Error
DErr: REAL; // Error Accumulation
PBH: REAL; // upper limit of the Ratio
PBL: REAL; // lower limit of the Ratio
PVC: REAL; // the change rate of the measured value within a control period, that is, the change rate of the measured value.
P: REAL; // proportional item
I: REAL; // points
D: REAL; // Differential Item
END_VAR

IF Run = 1 THEN
IF Auto = 1 THEN
If sv> = SVSW THEN
Err: = SV-PV;
PBH: = SV + PBW;
PBL: = SV-PBW;
If pv <PBL THEN
MV: = 1;
Elsif pv> PBH THEN
MV: = 0;
ELSE
P: = (PBH-PV)/(PBH-PBL); // calculate proportional items
//////////////////////////////////////// /////////////////////&...... Remaining full text>

DZ-10-shaped automatic air switch control motor, its transient element of the rated value should be 17-2 times the starting current of the motor (this

DZ-10 is the serial code of Air Switch, model 10 is national unified model but very old. The model 10 is a square pier of the black shell of the rubber wood. There are many new models, and they are not very uniform. Some are enterprise models.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.