Chapter 3 Decision Making. What is IF? The primary method of changing the flow of a program is by making decisions using the IF verb. The following example.

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

BY: JOSHUA THOMAS IGNATIUS TOWERS COBOL. Overview What is COBOL History Design Implementations What did it do Program structure Data types Syntax Sample.
Conditionals – if - else Dr. Sajib Datta
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 2 - Algorithms and Design
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
Chapter 4 Selection Structures: Making Decisions.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Lesson NUMERIC-FIELDPIC 9(5). 05 ALPHANUMERIC-FIELDPIC X(5).... IF NUMERIC-FIELD = 10...(valid entry) IF NUMERIC-FIELD = ‘10’... (invalid entry)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CPSC3111/CISM3111 COBOL Structured COBOL Programming Text: murach’s structured COBOL Authors: Murach, Prince, Menendez.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Copyright 2003 Scott/Jones Publishing Making Decisions.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 2 Using Variables and Constant. What is a Constant ? The data in COBOL programs falls in two broad categories: – Constants and Variables A constant.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Controlling Program Flow with Decision Structures.
Linux+ Guide to Linux Certification, Second Edition
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
IF statements.
The Selection Structure
If, else, elif.
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Chapter 6 Variables What is VBScript?
Alternate Version of STARTING OUT WITH C++ 4th Edition
Logical Operations In Matlab.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Relational Operators.
Chapter 3: Selection Structures: Making Decisions
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

Chapter 3 Decision Making

What is IF? The primary method of changing the flow of a program is by making decisions using the IF verb. The following example demonstrates the IF verb: Example IF condition PERFORM DO-SOMETHING.

What is IF? When COBOL sees an IF, it makes a decision about the condition, and then either requests a PERFORM of DO-SOMETHING or skips that line of the program.

What is IF? IDENTIFICATION DIVISION. PROGRAM-ID. YESNO01. DATA DIVISION. WORKING-STORAGE SECTION. 01 YES-OR-NO PIC X. PROCEDURE DIVISION. PERFORM GET-THE-ANSWER. PERFORM DISPLAY-THE-ANSWER. STOP RUN. GET-THE-ANSWER. DISPLAY "Is the answer Yes or No? (Y/N)". ACCEPT YES-OR-NO. DISPLAY-THE-ANSWER. IF YES-OR-NO IS EQUAL "Y“ DISPLAY "You answered Yes.". IF YES-OR-NO IS EQUAL "N“ DISPLAY "You answered No.“.

Using IF to control multiple statements IF condition PERFORM DO-SOMETHING PERFORM DO-SOMETHING-ELSE. An IF controls all statements under it until the sentence ends. When an IF tests true, all statements up to the next period are executed.

Using IF to control multiple statements DISPLAY-THE-ANSWER. IF YES-OR-NO IS EQUAL "Y“ PERFORM IT-IS-VALID DISPLAY "You answered Yes.". IF YES-OR-NO IS EQUAL "N“ PERFORM IT-IS-VALID DISPLAY "You answered No.".

What Can You Test with IF? The symbols used to compare two values are called comparison operators. The short and long versions of these comparisons are all comparison operators. IS NOT EQUAL, NOT =, =, IS EQUAL, NOT, GREATER THAN, and NOT GREATER THAN are all examples of comparison operators.

COBOL comparison operators. Comparison Operator Description IF x IS EQUAL yTrue if x equals y IF x IS LESS THAN yTrue if x is less than y IF x IS GREATER THAN yTrue if x is greater than y IF x IS NOT EQUAL yTrue if x does not equal y IF x IS NOT LESS THAN yTrue if x is not less than y (or is equal to or greater than y) IF x IS NOT GREATER THAN yTrue if x is not greater than y (or is equal to or less than y)

COBOL comparison operators. Optional OperatorShortest Version IF x EQUAL yIF x = y IF x LESS THAN yIF x < y IF x GREATER THAN yIF x > y IF x NOT EQUAL yIF x NOT = y IF x NOT LESS THAN yIF x NOT < y IF x NOT GREATER THAN yIF x NOT > y

COBOL comparison operators. IF YES-OR-NO = "y“ MOVE "Y" TO YES-OR-NO. IF YES-OR-NO = "n“ MOVE "N" TO YES-OR-NO.

COBOL comparison operators. IF YES-OR-NO = "Y“ PERFORM IT-IS-VALID DISPLAY "You answered Yes.". IF YES-OR-NO = "N“ PERFORM IT-IS-VALID.

COBOL comparison operators. For numeric values. Less than and greater than are both conditions that easily can be established when you are testing two numbers. When a condition test is performed on alphanumeric variables, the tests usually compare the characters in the two alphanumeric values on the left and right sides of the comparison operator, in ASCII order.

COBOL comparison operators. In ASCII order, – A is less than B, AB is less than ABC, and the uppercase letters are less than the lowercase letters; so, ABC is less than abc. – digits 0 through 9, the digits are less than the characters, so 1BC is less than ABC. – Spaces are the lowest of all, so three spaces are less than 00A. The complement of GREATER THAN is LESS THAN OR EQUAL

IF Statement Indent IF conditions carefully. An IF controls all statements up to the period at the end of the sentence. Correct indentation gives a good visual clue of which parts of the program are controlled by the IF. DON'T use sloppy indenting on an IF.

IF Statement DISPLAY "The words sorted in ASCII order are:". IF WORD-1 < WORD-2 DISPLAY WORD-1 DISPLAY WORD-2. IF WORD-1 NOT < WORD-2 DISPLAY WORD-2 DISPLAY WORD-1.

IF Statement DISPLAY "The words sorted in ASCII order are:". IF WORD-1 < WORD-2 DISPLAY WORD-1 DISPLAY WORD-2. IF WORD-1 NOT < WORD-2 DISPLAY WORD-2 DISPLAY WORD-1.

Testing Multiple Conditions An IF test also can be used to test more than one condition. Conditions can be combined by using AND, OR, or combinations of both. E.g. IF MENU-PICK 3 DISPLAY "Invalid selection". Note: If the entered MENU-PICK is less than 1 or greater than 3, it is invalid. The OR combines the two tests within one IF. An OR test is true if either of the tests is true.

Testing Multiple Conditions An AND test is true only if both conditions being tested are true. The valid range of entries for the statement below is 011 through 099. Excluding 10 and 100. IF THE-NUMBER > 10 AND THE-NUMBER < 100 DISPLAY "The number is in range.".

IF-ELSE When an IF test fails, none of the statements controlled by the IF test are executed. The program continues to the next sentence and skips all the logic. If you are testing a condition and you want to do one set of commands if the condition or conditions are true and another set if they are false, it is easier to use ELSE.

IF-ELSE Example IF A < B PERFORM ACTION-A PERFORM ACTION-B ELSE PERFORM ACTION-C PERFORM ACTION-D. period

IF-ELSE When the IF condition is true, all statements up to the ELSE are executed. Otherwise, all statements from the ELSE to the closing period are executed. The period is placed at the end of the last statement in the ELSE.

Quiz In the following paragraph DECIDE-WHAT-TO-DO, which lines are executed when THE-NUMBER equals 7? DECIDE-WHAT-TO-DO. IF THE-NUMBER = 7 OR THE-NUMBER < 4 PERFORM ACTION-1 PERFORM ACTION-2 ELSE PERFORM ACTION-3. Which lines are executed when THE-NUMBER equals 6? Which lines are executed when THE-NUMBER equals 2? Which lines are executed when THE-NUMBER equals 4?

IF-ELSE Reading Chapter 4 of text.