Controlling Program Flow

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Flow of Control MINS298c Fall 1998 Chapter 9. Overview ABAP Programming Structures for: –Iteration –Decisions Control Flow –If … Then –Do & While loops.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4: Control Structures: Selection
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Geography 465 Assignments, Conditionals, and Loops.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
CONTROLLING PROGRAM FLOW
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
© 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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Control Flow (Python) Dr. José M. Reyes Álamo.
LOGICAL CONTROL STRUCTURES (chp. 8)
Making Choices with if Statements
Chapter 3: Decisions and Loops
Chapter 5- Control Structures: Part 2
Sequence, Selection, Iteration The IF Statement
Chapter 4 C Program Control Part II
Control Statements: Part 2
Control Structures: Part 2
Expressions and Control Flow in JavaScript
CSC115 Introduction to Computer Programming
CPS120: Introduction to Computer Science
Chapter 8 JavaScript: Control Statements, Part 2
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
` Structured Programming & Flowchart
Chapter 6 Control Statements: Part 2
Logical Operations In Matlab.
Three Special Structures – Case, Do While, and Do Until
Problem Solving and Control Statements
PowerShell Flow of Control Copyright © 2016 – Curt Hill.
Chapter 3: Selection Structures: Making Decisions
Controlling Program Flow
Chapter 3: Selection Structures: Making Decisions
Problem 1 Given n, calculate 2n
Presentation transcript:

Controlling Program Flow

Topics Conditional Operators Logical Operators If Select Case Loops

Operators manipulate data by combining or computing results.

Math and String Operators Precedence Default – Left to Right ( ) ^ * / MOD \ + - &

6 Conditional Operators = Equal to <> Not equal (less than or greater than) < Less than > Greater than >= Greater than or equal to <= Less than or equal to Like Is

You can compare strings “S” < “s” “Kev” < “Kevin” IMPORTANT: The computer ultimately gives comparisons a TRUE or FALSE answer. Console.write (1<2) Console.write (1=2)

Like Operator Used for seeing if strings are similar Uses Wildcards: * ? # [characters] [!not these characters] Console.WriteLine (“Kevin” like “Ke*”) Console.WriteLine (“Beet” like “B??t”) Console.WriteLine (“Employee 7” like “Employee #”)

Logical Operators

Not …Statements If Not((varA = varB) And (varC < varD)) then End if NEGATES a condition Use Sparingly!

And …Statements If (varA = varB) And (varC < varD) then End if If True and True then Condition 1 AND Condition 2

Or …Statements If (varA = varB) Or (varC < varD) then End if If True Or True then Condition 1 AND Condition 2

Xor …Statements If (varA = varB) Xor (varC < varD) then End if If True Xor True then Condition 1 AND Condition 2

Truth Table Condition Result AND TRUE FALSE OR XOR

Such a simple device… Computers only know: 1 or 0 ON or OFF Yes or No True or False We combine these choices to set the course of our computer program.

If then If (condition is true) then do this End if

If then Else If (condition is true) then do this Else do something else End If

If then ElseIf If (condition is true) then do this ElseIf (condition is true) then do something else Else do yet something else End If

Select Case Simpler, more logical than using several elses Select Case varAge Case Is <5 statements Case 5 Case 6 to 11 Case 12 to 13, 23 to 46, Is 65 Case Else End Select

For Next Loop Used for counting through a certain number of iterations (geek-speak for times). For Counter As Integer = 1 to 10 ‘statements Next You can dimension variables in the loop, as above.

Nesting Loops For outerCounter = 1 to 10 statements For innerCounter = 1 to 10 Next innerCounter Next outerCounter

Step value For counter = 0 to 10 step 2 ‘statements Next Counter steps = 0, 2, 4, 6, 8, 10

Step Values: negative For counter = 10 to 0 Step -1 ‘statements Next

Do Loops Execute WHILE a condition is true, or UNTIL it is true Do While strAnswer=“yes” statements Loop

Do Loops Do While Loop condition Until

Do Loops Do While condition statements Loop Do statements Loop While condition Do Until condition statements Loop Do statements Loop Until condition

An early exit… Exit Sub Exit Function Exit For Exit Do End (ends program) Use sparingly - It is most clear to have one exit for a program segment