CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Visual C++ Programming: Concepts and Projects
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CPS120: Introduction to Computer Science Operations Lecture 9.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
ICT Introduction to Programming Chapter 4 – Control Structures I.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CPS120: Introduction to Computer Science Decision Making in Programs.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Rational Expressions relational operators logical operators order of precedence.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Relational Operator and Operations
The Ohio State University
Chapter 3 Control Statements
Sequence, Selection, Iteration The IF Statement
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
A mechanism for deciding whether an action should be taken
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Arithmetic operations, decisions and looping
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
All the Operators 4-Dec-18.
Summary Two basic concepts: variables and assignments Basic types:
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
CprE 185: Intro to Problem Solving (using C)
Controlling Program Flow
Conditionals.
Presentation transcript:

CSci 125 Lecture 10 Martin van Bommel

Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”); Useless statements n1 + n2;

Embedded Assignments Assignment expression can be used as part of a larger expression in a statement Its value is the value assigned z = (x = 6) + y; x is assigned value 6, then z assigned 6 + y Difficult to read Used rarely and only when makes sense

Multiple Assignments Embedded assignments useful to set several variables to the same value n1 = n2 = n3 = 0; Assignment operator evaluated right to left Avoid mixed types; e.g. double d and int i d = i = 1.5; Assigns i value 1, thus 1 assigned to d

Statement Blocks Sequence of statements that specify a coherent unit enclosed in curly braces E.g. if, for, while statement bodies { statement 1 statement 2... }

Boolean Data Conditionals test expressions whose values are either TRUE or FALSE George Boole - developed algebra for T/F Standard C has no Boolean type We use the integer 0 for FALSE and the integer 1 for TRUE

Relational Operators Compare two atomic values (not strings) Precedence after +/- > Greater than < Less than >= Greater or equal <= Less or equal Next level of precedence == Equal != Not equal

Logical Operators Operate on other Boolean values (in order of precedence) !Not ( TRUE if operand FALSE ) && And ( TRUE if both TRUE ) || Or ( TRUE if either TRUE )

Example 1 “ x is not equal to either 2 or 3” if (x != 2 || x != 3) No, it should be if (!(x == 2 || x == 3)) or if (x != 2 && x != 3)

Example 2 “x is in the range from 0 to 10 exclusive” if (0 < x < 10) No, it should be if (0 < x && x < 10)

Short-Circuit Evaluation Evaluating exp1 && exp2 or exp1 || exp2 evaluates expressions from left to right If answer known before both evaluated, second expression not evaluated (x != 0) && (y % x == 0)

Flags Variables used for testing truth values int done; Assign value to indicate state of flag done = 0; or done = 1;

Setting Flags To set a flag on a condition, could use if (input == -1) { done = 1; } else { done = 0; } Better to simply use done = (input == -1);

Testing a Flag To test whether done has value TRUE, could use if (done == 1) Better to simply use if (done) Test for truth value 1 is redundant

Boolean Example Leap year every fourth year, except centuries, then just every fourth century –year is divisible by 4 but not by 100, or –year is divisible by 400 Try ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)