LOGICAL CONTROL STRUCTURES (chp. 8)

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Introduction to C Programming
8-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
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.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Lesson NUMERIC-FIELDPIC 9(5). 05 ALPHANUMERIC-FIELDPIC X(5).... IF NUMERIC-FIELD = 10...(valid entry) IF NUMERIC-FIELD = ‘10’... (invalid entry)
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
The world of Constructs Control Structures. The three Structures Sequence Selection Loop Entry Exit.
More Syntax in COBOL  Moving Data  Selection Statements  System Date  Indicators in Display files.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CIS 020 Assembly Programming Chapter 06 - Comparisons & Logical control Procedures © John Urrutia 2012, All Rights Reserved.5/27/20121.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter 4 – C Program Control
THE SORT STATEMENT for files (chp. 14)
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
The Selection Structure
Chapter 4 - Program Control
Engineering Problem Solving with C++, Etter/Ingber
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Lecture 2-3 C++ Basic Constructs
Unit 2 Programming.
Boolean Expressions and If statements
Chapter 3 The DATA DIVISION.
Chapter 8 JavaScript: Control Statements, Part 2
CS1100 Computational Engineering
Control Structures: Selection Statement
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Agenda Collating sequence / Sorting data
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Controlling Program Flow
C Programming Getting started Variables Basic C operators Conditionals
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Using Decision Structures
Chapter 3: Selection Structures: Making Decisions
Chapter 4 - Program Control
Control Structures: Selection Statement
Any Questions?.
The Selection Structure
Decision Making Using the IF and EVALUATE Statements
REPETITION Why Repetition?
Presentation transcript:

LOGICAL CONTROL STRUCTURES (chp. 8) 1. Sequence – move, add, write, etc. 2. Selection – if 3. Iteration – perform Function A Function B Entry Exit true Function A Exit condition Entry Function B false true condition Entry Exit Function A false Statement Syntax

IF STATEMENTS Examples: Syntax Definition: IF condition-1 [THEN] For Your Information A condition should evaluate to TRUE or FALSE. If the condition is true, the statements are executed. If the condition is false, the ELSE section of statements is executed, if it exists. Remember, the ELSE is optional Relational operators have equivalent worded clauses. For example, “is less than” and “is greater than or equal to”. AND and OR go between two conditions that evaluate to true or false. Conditions are evaluated Syntax Definition: IF condition-1 [THEN] statement(s)-1 [ELSE statement(s)-2] [END-IF] Conditions: Relational operators: <, >, <=, >=, = Logical operators: AND, OR, NOT Reminder: left right AND OR NOT left T T T T F T F F T F T F T T F F F F Examples: See example code Statement Syntax

CONDITIONS CONDITION-1 can be defined as: identifier-1 IS [NOT] { identifier-2 } GREATER THAN (>) LESS THAN (<) EQUAL TO (=) LESS THAN OR EQUAL TO (<=) GREATER THAN OR EQUAL TO (>=) { } NUMERIC ALPHABETIC POSITIVE NEGATIVE ZERO ALPHABETIC-UPPER ALPHABETIC-LOWER Statement Syntax

MORE ON CONDITIONS COLLATING SEQUENCE – EBCDIC (ibm) and ASCII (unix) LOW spaces spaces special characters special characters a-z 0-9 A-Z A-Z HIGH 0-9 a-z PRECEDENCE conditions surrounding AND conditions surrounding OR AND from left to right OR from left to right FYI: use parenthesis to override the above precedence Examples where a=1, b=2, c=2, d=3 a > c and b > c (true ….. false) a > c or b > c (true ….. false) a = b and b = c and a = d (true ….. false) a = b or b = c or a = d (true ….. false) a = b or b = c and a = d (true ….. false) a = b and b = c or a = d (true ….. false) Examples “smith” _____ “saunders” “Smith” _____ “saunders” “hi” ________ “hello” “YES” ______ “yes” “abc123” ______ “123abc” “aBc” _______ “AbC” Statement Syntax

IMPLIED CONDITIONS OTHER! The first test in a condition must be a “full test” meaning it evaluates to TRUE or FALSE on its own The subsequent conditions can have implied operands and/or operators if a = b and a = c if a = b and = c if a = b and c see example code… OTHER! Numeric/alphabetic is a CLASS test Positive/negative/zero is a SIGN test should have an S in the pic clause absolute value example (also in book) not negative is not equal to positive not greater than or equal to is equivalent to less than Statement Syntax

CONDITION NAMES Syntax Definition: 88 condition-name VALUE literal For Your Information Since a condition should evaluate to true or false, a condition name should also evaluate to true or false. Like all other level numbers, the condition name level number 88 also appears in the working-storage section of the data division. Each condition name has an equivalent condition using the 01 level data name. For instance, for example 1: read input-file at end move “yes” to eof-switch. perform para-1 until eof-switch = “yes” For example 2: if college-rank = 1 move “FR” to rank-desc if college-rank = 1 or 2 or 3 or 4 move “UG” to rank-course. if college-rank > 4 move “G” to rank-course. Syntax Definition: 88 condition-name VALUE literal […] | [THRU | THROUGH literal] Examples: 01 eof-switch pic xxx value “no”. 88 eof-cond value “yes”. perform para-1 until eof-cond. 01 college-rank pic 9. 88 rank-fresh value 1. 88 rank-soph value 2. 88 rank-junior value 3. 88 rank-senior value 4. 88 under-grad value 1 2 3 4. If rank-fresh move “FR” to rank-desc. If rank-soph move “SO” to rank-desc…. If under-grad move “UG” to rank-course. If not under-grad move “G” to rank-course. Statement Syntax

EVALUATE STATEMENTS Examples: Syntax Definition: EVALUATE TRUE {WHEN condition-1 stmt-group-1…} [WHEN OTHER stmt-group-2] [END-EVALUATE] Examples: Evaluate true when college-rank = 1 move “FR” to rank-desc move “UG” to rank-course when college-rank = 2… when college-rank = 3… when college-rank = 4… when college-rank > 4… when other perform 500-error-routine End-evaluate For Your Information Equivalent to nested IF structures. Only the first condition that is true is executed (like an IF structure!). Can use condition names i.e instead of condition-1 being college-rank = 1, you can use rank-fresh. Other evaluate syntax definition… Statement Syntax

PERFORM STATEMENTS (chp.9) For Your Information “with test before” is the default. FROM is the initial value of identifier-1 BY is the modifying value for identifier-1 Initialize, test, execute, modify, test, execute,… Move 1 to a. Perform until a > 10 display “a = “ a add 1 to a End-perform Perform varying a from 1 by 1 until a > 10 Q1. What if “”with test after”? Q2. What is value of “a” once loop has finished executing?! Q3. What if add/display in different order? TYPES: Perform-Until Perform # Times Perform Varying Syntax Definition: PERFORM [paragraph-name-1] {identifier-1 | integer-1} TIMES PERFORM [paragraph-name-1] [ {THROUGH | THRU} paragraph-name-2 ] [WITH TEST {BEFORE | AFTER} ] [VARYING identifier-1 FROM { identifier-2 | integer-2} BY {identifier-3 | integer-3} ] UNTIL condition-1 AFTER Init, exec, test Mod, exec, test,… identifier-1| integer-1 must be positive integer or zero identifier-2 | integer-2 and identifier-3|integer-3 can be positive or negative real values Statement Syntax