Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Solving Multi-Step Equations with Like Terms and Parentheses.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Visual C++ Programming: Concepts and Projects
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
CHAPTER 5 IF STATEMENT 5.1 Relational Operators Relational Operator Math Symbol Operation < or.LT. Or GT>Greater than
Chapter 4: Basic C Operators
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Computer Science Selection Structures.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Lecture 18: 10/31/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Selection Control Structure. Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
ITEC113 Algorithms and Programming Techniques
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Basic Control Structures
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
1 © 2002 John Urrutia. All rights reserved. Qbasic Chapter 4 IF Statements and READ & DATA.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
CPS120: Introduction to Computer Science Decision Making in Programs.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
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.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
Control Structures: Part 2
Operator Precedence Operators Precedence Parentheses () unary
Decision Making in Code Logical Tests & Truth Logical Expressions
Topics The if Statement The if-else Statement Comparing Strings
Bools & Ifs.
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
The C++ IF Statement Part 2 Copyright © Curt Hill
Program Flow Control Selection & repetition
Introduction to Programming Using Python PART 2
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Relational Operators.
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Matlab Basics.
Chapter 3: Selection Structures: Making Decisions
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential algorithms, where each instruction is executed once, immediately after the one above it

Selection We use selection to handle making choices A condition that is evaluated to be either true or false determines the next action Is a > b? Print “Yes” Print “No” TrueFalse IF 1 is greater than 2 THEN Print “Yes” ELSE Print “No” ENDIF

Relational Operators Used to test the relationship between two expressions or two variables Condition evaluates as.FALSE..TRUE.

FORTRAN’s Relational Operators = =Equal uses two equal signs >Greater than <Less than >=Greater than or equal to <=Less than or equal to /=Not equal

FORTRAN’s Relational Operators = = or.EQ.Equal uses two equal signs >or.GT.Greater than <or.LT.Less than >= or.GE.Greater than or equal to <= or.LE.Less than or equal to /= or.NE.Not equal

Examples x = 500, y = 300 x= =y x > 100 y >=250 x /= 500 ((x + y) > 700) ((x / 2).LT. 99)

Compound Logical Expressions Used to combine or negate expressions containing logical operators. –used with compound conditions.AND. And.OR.Or.NOT.Not.EQV.Equivalence.NEQV.Nonequivalence

Logical Operators ((x == 5).AND. (y != 10))both conditions must be true for statement to evaluate as true ((x == 7).OR. (y > 10))either condition can be true for statement to evaluate is true. Only evaluates as false if both false (.NOT. (x >= 7))reverses the value of expression to its immediate right. same as if (x < 7) then

Order of Precedence ( ) Parentheseshighest **Exponentiation *, /Multiplication,Division +, -Addition, Subtraction >= Greater than, less than, …or equal = = /=Equal, not equal.AND.And.OR.Or =Assignment statementlowest Rule: Use parentheses to make evaluation order clear

Conditions Practice 1.“A” < “F” “cat” < “can” “June” > “July” “cat_ _ _” < “cattle” “c” = = “C”

Conditions Practice 1. Given a = 12, b = 6, c = 8 true or false? ((a < b).AND. (b /= c) (.NOT.(a c)) ((a > b).AND.(a > c).OR.(b > c)) 2. What would the values of a, b, and c need to be for this statement to be true? (use 1,2,3) ((a >c.OR. b > c).AND. (a < b))