TODAY’S LECTURE Review Chapter 2 Go over exercises.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Statement-Level Control Structures
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.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Creating PHP Pages Chapter 7 PHP Decisions Making.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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 (!)
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
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.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 3 More Flow Of Control.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
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,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Java Java Translation Program Structure
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
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.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Loops in Java.
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
Arrays, For loop While loop Do while loop
Loop Control Structure.
Statement-Level Control Structures
PROGRAM FLOWCHART Iteration Statements.
Controlling Program Flow
Presentation transcript:

TODAY’S LECTURE Review Chapter 2 Go over exercises

CONTROL FLOW Boolean expressions if / if-else switch while, do-while for (original, Iterator versions) (break, continue)

BOOLEAN EXPRESSIONS Control flow uses boolean expressions to navigate blocks of code. How do we get booleans? directly, with true and false. using relational operators:, >=, ==, != using boolean operators: &&, ||, ! calling a method that returns a boolean e.g. myScanner.hasNext() any expression, as long as it results in true or false.

BLOCK STATEMENT As we introduce blocks of code for the branches of if- else's and switch statements, and for the bodies of loops, we want to group many statements together. In Java, we place curly braces { } around multiple statements to group them. It is so common to use them with control structures that it seems like {}'s are part of their syntax, but it is a separate statement structure all on its own. Example:{ stmt1; stmt2; } If there is only one statement inside the curly braces, we often drop the curly braces for legibility. Can you think of a danger with this approach?

'ELIF' IN JAVA? There is no 'elif' in Java: just chain "if else" statements together: if (be1) s1 else if (be2) s2 else if (be3) s3 else s4

SWITCH STATEMENT Syntax: switch (expr) { case val1: stmt1 case val2: stmt2... default: stmtD// this 'default' case is optional } Semantics: expr must be integral (whole number) or char (no Strings or booleans or floats or objects!). All case values must be constants. evaluate expr, and compare against each case value in order until exact match is found. execute all stmt's after matching case! (thus break is common at the end of each case) default: no value; stmtD always runs if no other case values equaled the switch expression.

DO-WHILE LOOP Syntax: do stmt while (boolexpr); Semantics: evaluate stmt (no matter what). evaluate boolexpr; if true, repeat (evaluate stmt again). If false, do-while is done. Note: semicolon after (boolexpr) is required! ; Note: stmt runs at least once (unlike while loop, whose stmt might not run at all). Example:int x = 0; //consider also x = 500; do System.out.println(x++); while (x<100);

FOR LOOP Syntax: for (initializer; guard; incrementer) stmt Semantics: initializer is a statement. Runs exactly once, before everything else. (If a variable is declared, its scope is only within loop. Variable doesn't have to be declared, it can already exist). guard is a boolean expression. Each iteration (including first), this is checked: true => run stmt; false => exit loop. incrementer is a statement. Runs after stmt, each time that stmt runs. Note: initializer, guard, and incrementer could each be omitted! E.g., for (;;) stmt Example: for (int i = 0; i<10; i=i+1) System.out.println(i); foreach ( iterator : someList) is another version

LET’S GO OVER THE EXERCISES

ARRAYS

TODAY’S LECTURE Review Chapter 3 Go over exercises

ARRAY TYPES The array type is indicated with [ ] 's. Monomorphism: Just as variables can only hold one type of value, Java arrays can only hold one specified type of value, in every slot. Example array types: int[]double[]boolean[][]Person[] The type doesn't record the dimension lengths, but an array value will specify the (unchanging) lengths. int[][][] xs = new int[3][4][5]; //a 3x4x5 structure of ints.

ACCESSING/MODIFYING ARRAYS Brackets [ ] are used to access and update values in arrays. int a = xs [4] ;//accesses 5 th elt. of xs. xs [0] = 7;//replaces 1 st xs elt. with a 7. Any expression of type int may be used as an index, regardless of the type in the array: xs[ a+4 ]xs [ sc.nextInt() ] xs[ i ] ys[ i ][ j ] The length of an array is available as an attribute: xs. length ys[i]. length

LET’S GO OVER THE EXERCISES

QUESTIONS?