Free php web host - CHAPTER 6 MAKING DECISIONS WITH CONDITIONAL LOGIC
CHAPTER 6 MAKING DECISIONS WITH CONDITIONAL LOGIC 165 Single Line If Then Statements In many cases, all you need to set up a conditional test is a single line If Then statement. For example, each of the following statements is a single line If Then statement. If strUserName = “Molly” Then MsgBox(”Hello Molly”) If strUserName = “Molly” MsgBox(”Hello Molly”) If strUserName = “Molly” Then MsgBox(”Hello Molly”) Else MsgBox(”Hello”) In the first example, the value of strUserName is tested to see if it is equal to Molly and if it is (for example, if the test result evaluates to True), the MsgBox function that follows the Then keyword is executed. However, if the result of the text is False, the MsgBox function is not processed. The second statement shown in the previous code is almost identical to the first statement, except the Then keyword is omitted. The Then keyword is optional on single line If Then statements. The third example shown in the previous code demonstrates how to provide an alternate course of action in case the test result is False. In this example, if strUserName equals Molly , then MsgBox( Hello Molly ) is executed and the rest of the statement is skipped. However, if strUserName does not equal Molly , then the first instance of the MsgBox function is skipped and the second instance is executed (for example, MsgBox( Hello ). Note When using the single line form of the If Then statement, the closing End If statement, which normally follows an If Then statement, is omitted. Multiple Line If…Then Blocks In most cases, you won t be able to fit everything on one line that you want to achieve with an If Then statement. In these cases, you can set up an If…Then code block, as the following shows. If blnExitApp = True Then MsgBox(”You have elected to close the application.”) Quit End If As this example shows, you can embed more than one statement inside an If Then block. In this example, if the value assigned to the blnExitApp variables is equal to True, then all of the statements in between the opening If Then statement and the closing End If statement are executed. However, if the value assigned to blnExitApp is equal to False, then the code statements inside the If Then block are skipped and program execution continues with the next programming statement following the If Then block. Tip Good programming practice is to use code blocks in place of single line If Then statements. This way, if you have to come back later and insert additional code statements, the necessary structure will already be in place.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.