NQC: Control Structures: Branching Last updated 10/6/05 10:00am References: Baum Chapter 3 pp. 50-52 NQC Tutorial pp. 14-15 Baum Appendix D pg 368.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Chapter 4: Control Structures I (Selection)
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
NQC: Control Structures: Loops Last updated 10/5/05 References: NQC Tutorial Baum Chapter 3 Baum Appendix D pg 368.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
NQC: Not Quite C (last updated 9/14/05 2:24pm) Each NQC program has a task main () The program is contained within curly braces { ….. } task main() { SetSensor(SENSOR_1,
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Repeating Blocks of Code (updated 9/20/05 7:35pm) Reference NQC Tutorial pp 9-12.
Preprocessor Directives (last modified 9/19/05 2:47pm) Statements beginning with # are directives to the preprocessor. –They DO NOT end with ; –Before.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Fortran 2- More Basics Chapter 3 in your Fortran book.
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.
Simple Control Structures IF, IF-ELSE statements in C.
Logic Structures. If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Activity 2 Practice.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
While and If-Else Loops ROBOTC Software. While Loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Chapter 2 Programming with PHP Part 2. handle_form.php Script 2.3 on pages 46 cript_02_03/form.html
CSI 3125, Preliminaries, page 1 Control Statements.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
 Type Called bool  Bool has only two possible values: True and False.
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
WELCOME to….. The If Statement.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Bools & Ifs.
Control Structures – Selection
JavaScript Selection Statement Creating Array
Relational and Logical Operators
2-1 Making Decisions Sample assignment statements
IF if (condition) { Process… }
Relational Operators Operator Meaning < Less than > Greater than
Computers & Programming Languages
Programming with PHP Part 2
Repetition Control Structure
IF Statements.
Relational & Logical Operators, if and switch Statements
Conditional Logic Presentation Name Course Name
Relational and Logical Operators
if-else Structures Principles of Engineering
Logic of an if statement
Relational Operators.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Relational and Logical Operators
Relational and Logical Operators
Relational and Logical Operators
CSCI 1100/1202 February 6, 2002.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Objective The student will be able to:
Types, Truth, and Expressions
Presentation transcript:

NQC: Control Structures: Branching Last updated 10/6/05 10:00am References: Baum Chapter 3 pp NQC Tutorial pp Baum Appendix D pg 368.

Conditions (Relational Operators) Recall the statement if(condition) { Body } A condition is an expression which when evaluated has a value of true or false. if(SENSOR_1 == 1)…… // == is the equality operator if(SENSOR_1 != 1)…… // != is the not equality operator if(SENSOR_2 < 10)…… if(SENSOR_2 > 20)…… if(SENSOR_3 >= 25)…… If SENSOR_1 is a touch sensor, the first until statement will execute the body if SENSOR_1 has a value of 1. What do the other statements do?

Conditions (Logical Operators) Relational operators produce binary values true, false Suppose x has a value of 5, then the condition x>2 has a value of true x<3 has a value of false The operator && is the logical AND The operator || is the logical OR true && true is true true && false is false true || true is true true || false is true false || false is false What happens in these statements? if(SENSOR_1 >10 && SENSOR_1<20)… if (SENSOR_1 == 1 || SENSOR_2 ==1)…

Forms for the if If(condition) body; if(SENSOR_1 == 1) Off(OUT_A); if(SENSOR_1 == 1) Off(OUT_A); // this is the same as above What about this? if(SENSOR_1 == 1) Off(OUT_A); Off(OUT_B); // Indentation has no effect on execution; // In all cases, OUT_B will be turned off. What about this? if(SENSOR_1 == 1) // This will turn off both OUT_A and OUT_B { // only if SENSOR_1==1. Off(OUT_A); Off(OUT_B); }

Two forms for the if if(condition) ss1 if(condition) ss1 else ss2 ss1 statement or statements { } when condition is true. ss2 statement or statements { } when condition is false if(SENSOR_1==1) Off(OUT_A); else { OnRev(OUT_A); Off(OUT_B); } This can also be written if(SENSOR_1==1)Off(OUT_A); else{OnRev(OUT_A);Off(OUT_B);} However, the first form is easier to read!