Presentation is loading. Please wait.

Presentation is loading. Please wait.

5.04 Apply Decision Making Structures

Similar presentations


Presentation on theme: "5.04 Apply Decision Making Structures"— Presentation transcript:

1 5.04 Apply Decision Making Structures
Computer Programming I

2 Objective/Essential Standard
Essential Standard: Apply Programming and Conditional Logic Indicator: Apply decision-making structures. (3%)

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

4 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!

5 The If Decision Structure

6 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:

7 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

8 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.

9 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. 

10 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.

11 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 example If a = b then c= 10 Else c=13 End If Remember in C# that = is the assignment operator and is NOT used for comparisons of equality. A double equal (==) must be used to check for equality. This is not a requirement is VB and using == will produce an error. T F

12 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:

13 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.


Download ppt "5.04 Apply Decision Making Structures"

Similar presentations


Ads by Google