130 likes | 227 Views
5.04 Apply Decision Making Structures. Computer Programming I. Objective/Essential Standard. Essential Standard: 5.00- Apply Programming and Conditional Logic Indicator: 5.04 Apply decision-making structures. (3%). What is a decision?.
E N D
5.04 Apply Decision Making Structures Computer Programming I
Objective/Essential Standard • Essential Standard: 5.00- Apply Programming and Conditional Logic • Indicator: 5.04 Apply decision-making structures. (3%)
What is a decision? • Decisions are made by humans hundreds of times a day. • Computers can make simple decisions also. • What are some decisions you made today so far? Image taken from: learn2bebuddies.com
Boolean Logic • Remember, from the previous section Boolean logic? (Ex: intNum = 5) • Computers use it to make decisions! • There are only two answers: TRUE or FALSE • Computers do not know about Maybe!
Decisions… Decisions • So when you got up this morning what did you do? • This is a decision. Let’s put it in computer terms. • If the sun is out, then I will walk to school Image taken from: http://web.multco.us/sun/linkage
The If…Then Statement If…Then is a decision structure that executes a set of statements only when a condition is true. Form: If condition Then Statements End If T
The If…Then Statement • IF the sun is out (question) THEN I will walk to school. • Remember the computer uses Boolean logic (T/F). So either the sun is out (true) or ANY other state (false). No maybes or in-betweens. • When the question is True, the statements after THEN (and down to ENDIF) execute. They are skipped if the question is False.
The If…Then Statement • Let’s say there was a program that determined whether or not a number was guessed correctly. Further assume that the ‘correct’ number was 5. A conditional statement could be set up to notify the user if he/she guessed the correct answer: • In the above code, intGuess=5 represents the condition. The condition is the code statement that needs to be true in order for VB to execute the code between the If and the End If. • The = sign is called a conditional operator (more on this later). • Essentially, if the condition is true, the statements between the If and the End If statements are performed; if the condition is false, VB skips those lines and continues on with code that appears after the block of code.
The If…Then Statement • You will also notice that, when you type the first line of the conditional statement, and press Enter, the next line automatically indents. • While this doesn’t have any impact on the way the VB reads or executes the code, it serves as an easier way for programmers to read and understand the code structure (other coding structures are indented in this way). • VB also automatically adds the End If line. This indentation scheme should always be used with conditional statements.
IF..THEN..ELSE Statement • In Visual Basic we use the IF..THEN..ELSE statement to tell the computer we want it to make a decision. • For exampleIf a = b then c= 10Else c=13End If T F
Use of the ELSE • NOTE: Else is optional- if omitted and the “question” is false no action is taken. • If ELSE is used, the statements after the ELSE will execute if the “question” is NOT true (Boolean FALSE) • If…Then…Else statements are used when the programmer needs to choose between two different outcomes. For example:
IF..THEN and IF..THEN..ELSE Statements What’s next? In the formative assessment, you will complete a series of steps designed to help you understand the concepts developed in this lesson. Please follow each step as documented. If you have issues/questions, make sure you watch the videos to gain a better understanding.