Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,

Slides:



Advertisements
Similar presentations
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
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.
Control Structures Corresponds with Chapters 3 and 4.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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 (!)
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
DAT602 Database Application Development Lecture 5 JAVA Review.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
JavaScript, Fourth Edition
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.
Week 4 Program Control Structure
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
Application development with Java Lecture 6 Rina Zviel-Girshin.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
COMP Loop Statements Yi Hong May 21, 2015.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
REPETITION CONTROL STRUCTURE
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements I
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
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.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6 Control Statements: Part 2
Week 3 – Program Control Structure
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany, SUNY Fall 2004

Sanjay Goel, School of Business, University at Albany, SUNY 2 Review Statements and Control Flow Boolean Expressions Logical Operators Logical Statements –if, else if, nesting, switch –Loops (while, for, break) Object-oriented Programming Outline for the Class Topics

Sanjay Goel, School of Business, University at Albany, SUNY 3 Statements and Control Flow

Sanjay Goel, School of Business, University at Albany, SUNY 4 Statements and Control Flow Introduction We have learned how to write words in a language. –We need to learn how to make sentences and paragraphs now. What do you need to do when you are doing a complex problem? –You have certain process to follow you have decision points where you use your mind to make decisions. To build your own logic into a computer program you need to write statements that capture your logic. –How to introduce logic into the program?

Sanjay Goel, School of Business, University at Albany, SUNY 5 Statements and Control Flow Statements Statements are roughly equivalent to sentences in natural languages. –A statement forms a complete unit of execution. It has many different types –Variable Declaration Statement –Expression Statement Assignment Statement Method Call Statement –Control Flow Statement

Sanjay Goel, School of Business, University at Albany, SUNY 6 Statements and Control Flow Variable declaration statements Variable declaration statements are used to identify the type of the variable that is being declared –start with a type and end with a semicolon e.g. int width, height, area; String myString;

Sanjay Goel, School of Business, University at Albany, SUNY 7 Statements and Control Flow Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. –Also defined as segments of code that perform computations and return values. Data type of the value returned by an expression depends on the elements used in the expression. Compound expressions and statements can be constructed from various smaller expressions as long as the data types required by one part of the expression matches the data types of the other. If the order in which the operations in a compound expression need to be performed is not explicitly indicated, the order is determined by the precedence assigned to the operators

Sanjay Goel, School of Business, University at Albany, SUNY 8 Statements and Control Flow Expression Statements Expression statements are formed by adding a semicolon at end of expression. –These are used to compute and to assign values to variables and to help control the execution flow of a program. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;): –Assignment expressions –Any use of ++ or – –Method calls –Object creation expressions Examples –aValue = ; //assignment statement –aValue++; //increment statement –System.out.println(aValue); //method call statement –Integer integerObject = new Integer(4); //object creation statement

Sanjay Goel, School of Business, University at Albany, SUNY 9 Statements and Control Flow Assignment Statements Assignment statement can be any expression involving the assignment operator. –e.g. area = length * width; Method Call Statement invokes other functions –Method call expression involves assignment when the method returns a statement. –e.g. System.out.println(“This is a test”);

Sanjay Goel, School of Business, University at Albany, SUNY 10 Statements and Control Flow Blocks Block is a group of statements enclosed in braces. –Blocks can occur within blocks and are called nested blocks e.g. { float length = 2; float width = 2; float area = length * width; System.out.println(“area = “ + area); } Empty or Null Statement –It is just a semicolon all by itself and results in no action. e.g. Consider the following program – lines 2 & 3 are null statement x = 2;  Line 1 ;  Line 2 ;  Line 3

Sanjay Goel, School of Business, University at Albany, SUNY 11 Boolean & Logical Expressions

Sanjay Goel, School of Business, University at Albany, SUNY 12 Boolean Expressions Introduction These are expressions which evaluate to true or false. The simplest Boolean expressions are true and false All conditional statements require Boolean expressions to decide flow of logic

Sanjay Goel, School of Business, University at Albany, SUNY 13 Boolean Expressions Relational & Equality Operators Relational & Equality Operators –used for comparing numeric values = == != These operators can be used between any two numeric values, e.g. int a = 10; int b = 20; int c = 15 a > b  false a < b  true c < 10  false

Sanjay Goel, School of Business, University at Albany, SUNY 14 Logical Expressions Operators These are used for combining multiple logical statements Three logical operators –&& - and –! - not –|| - or Examples: (See next page)

Sanjay Goel, School of Business, University at Albany, SUNY 15 Boolean & Logical Expressions Exercises Assume the following: int i, j, k; boolean b; k = 10; j = 6; b = true; Give the values of the following expressions, or state if illegal. 1.b 2.!b 3.!!!!!!b 4.b || !b 5.b && !b 6.b ^ true 7.b++ 8.b = 1 9.b = 1 > 2 10.true || 1234/26%3==1 11.true | 1234/26%3==1 12.!b & (j < 100) 13.k > j > 3 14.k > j && > 3 15.k > j && k > 3 16.b ||= false 17.b |= false 18.k = (k>j)?k:j+1 19.(b && !b) | (!b & b) 20.1 > 2 > 3 21.b = j > 0 && (i = k/j)!=0 Source:

Sanjay Goel, School of Business, University at Albany, SUNY 16 Boolean Expressions Exercises - Solutions 1trueb 2false!b 3true!!!!!!b 4trueb || !b 5falseb && !b 6falseb ^ true 7illegalb++ 8illegalb = 1 9falseb = 1 > 2 10truetrue || 1234/26%3==1 11truetrue | 1234/26%3==1 12false!b & (j < 100) 13illegalk > j > 3 14illegalk > j && > 3 15truek > j && k > 3 16illegalb ||= false 17trueb |= false 1810k = (k>j)?k:j+1 19false(b && !b) | (!b & b) 20illegal1 > 2 > 3 21trueb = j > 0 && (i = k/j)!=0 Source:

Sanjay Goel, School of Business, University at Albany, SUNY 17 Logical Statements

Sanjay Goel, School of Business, University at Albany, SUNY 18 Logical Statements If Statement If statement is a conditional statement which proceeds based on the evaluation of an expression, e.g. if ( temp < 32) System.out.println(“It is below freezing today”); System.out.println(“Temperature = “ + temp); For multiple statements depending on a conditional we use blocks, e.g. if (temp < 32) { System.out.println(“It is below freezing”); System.out.println(“Keep all the windows closed”); }

Sanjay Goel, School of Business, University at Albany, SUNY 19 Logical Statements Sorting Algorithm /* Bubble Sort Algorithm. In this * algorithm the smallest number */bubbles to the top public static void main(String[] args) { int a = Console.in.readInt(); int b = Console.in.readInt(); int c = Console.in.readInt(); int t; // Temporary variable if (a > b) { // Swap a and b t = a; a = b; b = t; } Example to sort three numbers if (b > c) { // swap b & c t = b; b = c; c = t; } if (a > b) { // Swap a & b t = a; a = b; b = t; } System.out.println(“The sorted numbers are: “ + a + “ “ + b + “ “ + c); }

Sanjay Goel, School of Business, University at Albany, SUNY 20 Logical Statements If else Statement If else statement is a bi-directional logic expression Syntax: if (Boolean expression) { Statement1 } else { Statement2 } Example: if (temp < 32) { System.out.println(“Close all the windows); } else { System.out.println(“Open all the windows”); }

Sanjay Goel, School of Business, University at Albany, SUNY 21 Logical Statements Nesting Nesting is allowed within if and if else statements if (temp < 32) { System.out.println(“Below Freezing”); if (temp < 20) System.out.println(“School is off”); else System.out.println(“Go To School”); } else { if (temp > 70) System.out.println(“Go to the beach”); else System.out.println(“Go to school”); }

Sanjay Goel, School of Business, University at Albany, SUNY 22 Logical Statements Dangling Else else goes with the closest if statement if (temp < 32) if (temp < 20) { System.out.println(“Stay indoors”); } else { System.out.println(“Keep Warm”); } Here the else statement goes with the second if statement

Sanjay Goel, School of Business, University at Albany, SUNY 23 Logical Statements Switch If (day == 1) { System.out.println(“Sunday”); } If (day == 2) {System.out.println(“Monday”); } If (day == 3) {System.out.println(“Tuesday”); } If (day == 4) {System.out.println(“Wednesday”); } If (day == 5){System.out.println(“Thursday”); } If (day == 6) {System.out.println(“Friday”); } If (day == 7) {System.out.println(“Saturday”); } Switch statement conditionally executes statements based on an integer expression – it is a multidirectional logic statement that can replace multiple if statements switch (day) { case 1: System.out.println(“Sunday”); break; case 2: System.out.println(“Monday”); break; case 3: System.out.println(“Tuesday”); break; case 4: System.out.println(“Wednesday”); break; case 5: System.out.println(“Thursday”); break; case 6: System.out.println(“Friday”); break; case 7: System.out.println(“Saturday”); break; default: System.out.println(“Illegal Value ” + day); }

Sanjay Goel, School of Business, University at Albany, SUNY 24 Logical Statements While While statement is used to continually execute a block of statements while a condition remains true. Syntax –while (expression) { statement } Operation –First, the while statement evaluates expression, which must return a boolean value. –If the expression returns true, then the while statement executes the statement(s) associated with it. –The while statement continues testing the expression and executing its block until the expression returns false. int i = 0; while (i < 100) { System.out.println(i); i++; }

Sanjay Goel, School of Business, University at Albany, SUNY 25 Logical Statements do-while do-while is similar to while statement that instead of evaluating the expression at the top of the loop, evaluates the expression at the bottom. –Statements associated with a do-while are executed at least once Syntax –do { statement(s) } while (expression); Operation –First the do statement executes the statement(s) associated with it. –Then the while statement evaluates expression that must return a boolean. –The do statement continues executing its block and the while statement continues testing the expression until the expression returns false. Example int i = 0; do { System.out.println(i); i++; } while (i < 100)

Sanjay Goel, School of Business, University at Albany, SUNY 26 Logical Statements For for statement provides a compact way to iterate over a range of values. Syntax –for (initialization; termination; increment) { statement } –The initialization is an expression that initializes the loop-it's executed once at the beginning of the loop. –The termination expression determines when to terminate the loop. This expression is evaluated at the top of each iteration of the loop. When the expression evaluates to false, the loop terminates. –Finally, increment is an expression that gets invoked after each iteration through the loop. e.g. Example for (int i = 0; i < 10; i++) { System.out.println(“My value is “+i); }

Sanjay Goel, School of Business, University at Albany, SUNY 27 Logical Statements Break Break causes an exit from the innermost enclosing loop. –Break statements in loops should usually be avoided as they alter the flow of control associated with loop statements from a clear "in at the top" to "out at the bottom" to something which is less obvious. –A break statement can sometimes be used "legitimately" to "break out" of a continuous loop. –However, in most cases there is an alternative strategy that will avoid the use of a break. Continue: causes the current iteration of the program to stop and the next to continue. e.g. for (int i=0; i<100; i++) { System.out.println(“ Square of “ + i + “ = “ + i*i); if (i*i > 10000) break; }

Sanjay Goel, School of Business, University at Albany, SUNY 28 Logical Statements Continue A continue statement returns to the beginning of the innermost enclosing loop without completing the rest of the statements in the body of the loop (for, while and do-while). –If you're in a for loop, the counter is incremented. –Rarely used in practice e.g. for (int i = 0; i < m.length; i++) { if (m[i] % 2 == 0) continue; // process odd elements... System.out.println(“Square of “ + i + “ = “ + i*i); }

Sanjay Goel, School of Business, University at Albany, SUNY 29 Recap

Sanjay Goel, School of Business, University at Albany, SUNY 30 Recap Statements Statements are roughly equivalent to sentences in natural languages. –A statement forms a complete unit of execution. It has many different types –Variable Declaration Statement –Expression Statement Assignment Statement Method Call Statement –Control Flow Statement Variable declaration statements are used to identify the type of the variable that is being declared –These statements start with a type and end with a semicolon

Sanjay Goel, School of Business, University at Albany, SUNY 31 Recap Expression Statements Expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. Expression Statements are formed by adding a semicolon at end of expression. Assignment statement –Any expression involving the assignment operator. –e.g. area = length * width; Method Call Statement –Method call expression does not involve assignment. –e.g. System.out.println(“This is a test”);

Sanjay Goel, School of Business, University at Albany, SUNY 32 Recap Blocks and Empty/Null Statements Block is a group of statements enclosed in braces. Blocks can occur within blocks and are called nested blocks e.g.{ float length = 2; float width = 2; float area = length * width; System.out.println(“area = “ + area); } Empty or Null Statement – It is just a semicolon all by itself and results in no action. e.g. x = 2; ;

Sanjay Goel, School of Business, University at Albany, SUNY 33 Recap Relational & Equality Operators Boolean Expressions –Evaluate to true or false –All conditional statements require Boolean expressions to decide flow of logic Relational & Equality Operators –used for comparing numeric values = == != These operators can be used between any two numeric values, e.g. int a = 10; int b = 20; int c = 15 a > b  false a < b  true c < 10  false

Sanjay Goel, School of Business, University at Albany, SUNY 34 Recap Logical Operators These are used for combining multiple logical statements Three logical operators –&& - and –! - not –|| - or

Sanjay Goel, School of Business, University at Albany, SUNY 35 Recap Logical Statements if statement –conditional statement which proceeds based on the evaluation of an expression –if (boolean-expression) then statement if-else statement –bi-directional logic expression –if (boolean expression) Statement11 else Statement2 Nesting of if and if else statements –Allows complex logic to be modeled Dangling else –else goes with the closest if statement

Sanjay Goel, School of Business, University at Albany, SUNY 36 Recap Switch Statement Switch is a multidirectional logic statement. Switch(op) { case a: statementa; break; case b:statementb; break; … default: statement; } Notes 1.Only one default statement in the switch 2.Case and Default cannot occur outside of the switch 3.break – exits the switch statement 4.Without break execution falls to next statement in the succeeding case 5.By not using break, you can combine multiple cases

Sanjay Goel, School of Business, University at Albany, SUNY 37 Recap Loops or iterations while Statement while (expression) statement do-while is similar to while where expression evaluated at bottom. –do { statement(s) } while (expression); for Statement for(init statement; termination expression; increment statement) { block of statements } break and continue –break: causes an exit from the innermost enclosing loop. –continue: causes the current iteration of the program to stop and the next to continue.