Decision making and control functions

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

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.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Algorithm Design.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
COMP Loop Statements Yi Hong May 21, 2015.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
CHAPTER 4 DECISIONS & LOOPS
Chapter 5: Structured Programming
The switch Statement, and Introduction to Looping
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Introduction to Functions
Loop Control Structure.
Lecture 2: Logical Problems with Choices
Flow of Control.
Chapter 4: Control Structures I (Selection)
IF if (condition) { Process… }
How to develop a program?
Flow of Control.
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Logical Operations In Matlab.
Lab5 PROGRAMMING 1 Loop chapter4.
Chapter 4: Control Structures I (Selection)
The System.exit() Method
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.
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Presentation transcript:

Decision making and control functions UNIT 4 Decision making and control functions

Study the functions that control the flow and execution of a program Aim and Agenda of unit 4 Study the functions that control the flow and execution of a program The aim of the presentation The agenda of the presentation Study comparison, Boolean and compound operators Present different kind of sequentially functions If () function If() else function For() function While() function Present different kind of control functions 2

COMPARISON OPERATORS Arduino knows how to make comparisons between numbers or the results of certain functions. Here are the various comparison operators as well as the signs that represent them OPERATOR SIGN Equal to = = Different to != Less than <  Greater than >  Less than or equal to < = Greater than or equal to >= 3

BOOLEAN OPERATORS It’s even possible to relate some of preceding comparisons to each other There are also only two possible results when two or more expressions are related using these logical operators: “true” o “false”. OPERATOR SIGN NOT ! AND && OR || EXAMPLES (Letter == ‘X’) && (A > 10) //False (A == 10+3) && (B >= 12345) && (Letter != ‘Q’) //True (B > 12300) || (PI = 3.1412) //True (A == B) || (A > 10 + 4) //False !(A == B) //True 4

COMPARISON OPERATORS Lots of times you’ll be doing very simple operations with a variable and the result will end up in the same variable. Remember that you can use the so-called “compound operators” to simplify these expressions OPERATION OPERATOR EXAMPLE EQUAL TO ++ increases a unit X++ X= X + 1 - - decreases a number Y- - Y = Y - 1 + = addition X+=Y X = X + Y -= subtraction X-= 3 X = X - 3 *= multiplication X *= Y X = X * Y /= division X /= 5 X = X / 5 5

CONTROL FUNCTION IF() FUNCTION This is the most basic and important control function If the result is “true” it executes all the functions inside the curly braces “{…}”. If the result is “false” the controller doesn’t execute them and the program keeps going. 6

CONTROL FUNCTION IF() FUNCTION EXAMPLE: if(expression) { …. } expression: establishes the condition that the Arduino controller has to assess. curly braces: they might look like the two slices of bread in a sandwich EXAMPLE: void loop() if((A>B) || (C < 25)) //If the condition is true… digitalWrite(6,HIGH); //Switches on pin 6 C=25; //A value of 25 is stored in the C variable .... //Continues the execution.... 7

CONTROL FUNCTION IF() ELSE FUNCTION it’s a derivative of the previous if(…) function If it’s “true”, all the functions in the curly braces “{…}” are executed just the same as with the if(…) function. If the condition is “false” all the functions enclosed by the else {…} braces are executed. Once the functions have been executed, whether or not the condition is true or false, the program keeps going 8

CONTROL FUNCTION IF()ELSE FUNCTION EXAMPLE: if(expression) { …. } else expression: establishes the condition that the Arduino controller has to assess. curly braces: they might look like the two slices of bread in a sandwich EXAMPLE: if(digitalRead(4) == 1) //If pin 4 is on “1” … digitalWrite(6,HIGH); //Enables pin 6 else //…and if not… digitalWrite(6,LOW); //Disables output 6 …. //Continues executing the program 9

CONTROL FUNCTION FOR() FUNCTION This function enables us to create controlled loops We declare an initial value The value variable is changed automatically If it's true, the statement block and the increment are executed and then the condition is tested again When the condition becomes false, the loop ends 10

CONTROL FUNCTION FOR() FUNCTION EXAMPLE: for(initialization,condition,increment) { …. } initialization: makes it possible to establish the value of a variable condition: it’s the condition that’s tested increment: makes it possible to change the value of the variable curly braces: they might look like the two slices of bread in a sandwich EXAMPLE: for (int N = 1; N < 5; N=N+1) //Establishes the loop conditions digitalWrite(6,HIGH); //Enables pin 6 delay (150); //Pauses the program for 0.15” digitalWrite(6,LOW); //Disenables pin 6 delay (1000); //Pauses the program for 1” …. //Continues executing the program 11

CONTROL FUNCTION WHILE() FUNCTION While loops are a variation on for() functions They are also used for loops where a group of functions are executed a certain number of times while(condition) { …. } condition: this is the conditional expression. It will loop continuously, and infinitely, until the expression inside the curly braces, {}, becomes false. When it does, the loop finishes and the program keeps going. 12

CONTROL FUNCTION WHILE() FUNCTION EXAMPLE: int N = 6 while (N > 0) //While N is greater than 0 … { digitalWrite(6,HIGH); //Enables pin 6 delay (150); //Pauses for 0.15” digitalWrite(6,LOW); //Disenables pin 6 delay (1000); //Pauses for 1” n--; //The next value for N ( N = N – 1) } …. …. //Continues executing the program 13

SWITCH() / CASE FUNCTION This function will enable you to choose between several “ways” of executing various groups of functions A switch statement compares the value of a variable to the values specified in case statements “If the value of the variable is X do these functions. If the value of the variable is Y do these other ones. If it’s Z do these other ones etc…” 14

SWITCH() / CASE FUNCTION switch(variable) { case X: ….; break; case n: default: } variable: this is the value of the variable that’s going to be compared with the ones in each “case” mentioned. case: this fixes all the values that are going to be compared to the contents of the variable. default: this is optional. If none of the values coincides all the functions are executed. 15

CONTROL FUNCTION 16

OTHER CONTROL FUNCTIONS DO…WHILE() FUNCTION The do loop works in the same way as the while() loop, with the exception that the condition is tested at the end of the loop do { …. } while(condition) BREAK FUNCTION break is used to exit from a for(),while() or do() loop, bypassing the normal loop condition. It is also used to exit from a switch() / case statement break; RETURN FUNCTION This one terminates a function and returns a value from any function created by the user to the calling function. return; return value; value: this is the value the function returns when it goes back to the calling program GO TO() FUNCTION Transfers program flow to a labelled point in the program. test: …. goto test: Jumps to wherever the label indicated. 17

UNIT 4 Decision making and control functions Thank You