Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Similar documents
Data Structure and Programming Languages

Research Group. 3: Loops, Arrays

Introduction to Programming. Lecture 2: Introduction to C

Visual Programming. Lecture 2: More types, Methods, Conditionals

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS 161: Object Oriented Problem Solving

Visual Programming. Lecture 4: Classes and Objects. Mahmoud El-Gayyar

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

CS 161: Object Oriented Problem Solving

Java Bytecode (binary file)

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

COMP 110/L Lecture 6. Kyle Dewey

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Conditionals, Loops, and Style

CS 161: Object Oriented Problem Solving

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

PROGRAMMING STYLE. Fundamentals of Computer Science I

PROGRAMMING FUNDAMENTALS

CS 161: Object Oriented Problem Solving

Introduction to Programming Using Java (98-388)

Lecture Set 4: More About Methods and More About Operators

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Assignment Marking Criteria

CS1150 Principles of Computer Science Loops (Part II)

CS111: PROGRAMMING LANGUAGE II

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Formatting & Style Examples

Scope of this lecture. Repetition For loops While loops

Review Ch 5 static Multiple Choice Test Creating Class Methods

Mobile App:IT. Methods & Classes

University of Palestine. Mid Exam Total Grade: 100

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Control Structures in Java if-else and switch

Topic 5: Enumerated Types and Switch Statements

Research Group. 2: More types, Methods, Conditionals

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

TESTING AND DEBUGGING

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

COMP 202 Java in one week

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Expressions & Flow Control

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Object-oriented programming in...

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Condi(onals and Loops

b. Suppose you enter input from the console, when you run the program. What is the output?

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

CS 231 Data Structures and Algorithms Fall 2018

Key Differences Between Python and Java

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CONDITIONAL EXECUTION

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Introduction to Java. Handout-1d. cs402 - Spring

COMPUTER PROGRAMMING LOOPS

CSE 142/143 Unofficial Style Guide

Array. Array Declaration:

Java Control Statements

Computer Programming I - Unit 5 Lecture page 1 of 14

Data Structure and Programming Languages

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Introduction to Software Development (ISD) Week 3

CS111: PROGRAMMING LANGUAGE II

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

1.00 Lecture 6. Java Methods

Bjarne Stroustrup. creator of C++

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Science II (20082) Week 1: Review and Inheritance

Lec 7. for loops and methods

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

MIDTERM REVIEW. midterminformation.htm

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10

Full file at

Scala Style Guide Spring 2018

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

COMP-202 Unit 9: Exceptions

Repetition with for loops

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

Lecture 04 FUNCTIONS AND ARRAYS

COMP-202 Unit 9: Exceptions

Introduction to Programming. Lecture 6: Functions & Program Structure

1. What does the following code fragment write to the monitor?

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

Beginning Style. 16-Nov-15

Syntax Errors; Static Semantics

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Transcription:

Visual Programming Lecture 3: Loops, Arrays Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 2

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 3

Frequent Issues (I) The signature of the main method cannot be modified. public static void main(string[] arguments) {... 4

Frequent Issues (II) Return values: if you declare that the method is not void, then it has to return something! public static int pay(double basepay, int hours) { if (basepay < 8.0) return -1; else if (hours > 60) return -1; else { int salary = 0;... return salary 5

Frequent Issues (III) Don't create duplicatevariableswith the same name public static int pay(double basepay, int hours) { int salary = 0; int salary = 0; // OK // salary already defined!! double salary = 0; //salary already defined!! 6

What we have learned so far Variables & types Operators Type conversions& casting Methods & parameters If statement 7

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 8

Good programming style The goal of good style is to make your code more readable. By you and by others. 9

Rule #1: use good (meaningful) names String a1; int a2; double b; // BAD!! String firstname; // GOOD String lastname; // GOOD int temperature; // GOOD 10

Rule #2: Use indentation public static void main (String[] arguments) { int x = 5; x = x * x; if (x > 20) { System.out.println(x + is greater than 20. ); double y = 3.4; Have a demo with no indentation Ctrl-shift-F to auto-format the file 11

Rule #3: Use whitespaces Put whitespaces in complex expressions: // BAD!! double cel=fahr*42.0/(13.0-7.0); // GOOD double cel = fahr * 42.0 / (13.0-7.0); 12

Rule #3: Use whitespaces Put blank lines to improve readability: public static void main (String[] arguments) { int x = 5; x = x * x; if (x > 20) { System.out.println(x + is > 20. ); double y = 3.4; 13

Rule #4: Do not duplicate tests if (basepay < 8.0) {... else if (hours > 60) {... else if (basepay >= 8.0 && hours <= 60) {... 14

Rule #4: Do not duplicate tests if (basepay < 8.0) {... else if (hours > 60) {... else {... 15

Good programming style (summary) Use good names for variables and methods Use indentation Add whitespaces Don't duplicate tests 16

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 17

Loops static void main (String[] arguments) { System.out.println( Rule #1 ); System.out.println( Rule #2 ); System.out.println( Rule #3 ); What if you want to do it for 200 Rules? 18

Loops Loop operators allow to loop through a block of code. There are several loop operators in Java. 19

The while operator while (condition) { statements 20

The while operator int i = 0; while (i < 3) { System.out.println( Rule # + i); i = i+1; Count carefully Make sure that your loop has a chance to finish. 21

The for operator for (initialization;condition;update){ statements 22

The for operator for (int i = 0; i < 3; i=i+1) { System.out.println( Rule # + i); Note: i = i+1 may be replaced by i++ 23

Branching Statements break terminates a for or while loop for (int i=0; i<100; i++) { if(i == 50) break; System.out.println( Rule # + i); 24

Branching Statements continue skips the current iteration of a loop and proceeds directly to the next iteration fo (int i=0; i<100; i++) { if(i == 50) continue; System.out.println( Rule # + i); 25

Embedded loops for (int i = 0; i < 3; i++) { for (int j = 2; j < 4; j++) { System.out.println (i + + j); Scope of the variable definedin the initialization: Respective for block 26

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 27

Arrays An array is an indexed list of values. You can make an array of any type int, double, String, etc.. All elements of an array must have the same type. 28

Arrays.. 0 1 2 3.. n-1 29

Arrays Example:double[ ].. 5.0 2.44 9.01 1.0-9.9 0 1 2 3.. n-1 30

Arrays The index starts at zero and ends at length-1. Example: int[] values = new int[5]; values[0] = 12; // CORRECT values[4] = 12; // CORRECT values[5] = 12; // WRONG!! compiles but // throws an Exception // at run-time 31

Arrays An array is defined using TYPE[]. Arrays are just another type. int[] values; // array of int int[][] values; // int[] is a type 32

Arrays To create an array of a given size, use the operator new: int[] values = new int[5]; or you may use a variable to specify the size: int size = 12; int[] values = new int[size]; 33

Array Initialization Curly braces can be used to initialize an array. It can ONLY be used when you declare the variable. int[] values = { 12, 24, -23, 47 ; 34

Quiz time! Is there an error in this code? int[] values = {1, 2.5, 3, 3.5, 4; 35

Accessing Arrays To access the elements of an array, use the [] operator: values[index] Example: int[] values = { 12, 24, -23, 47 ; values[3] = 18; // {12,24,-23,18 int x = values[1] + 3; // {12,24,-23,18 36

The length variable Each array has a length variable built-in that contains the length of the array. int[] values = int size = new int[12]; values.length; // 12 int[] values2 = {1,2,3,4,5 int size2 = values2.length; // 5 37

String Arrays public static void main (String[] arguments){ System.out.println(arguments.length); System.out.println(arguments[0]); System.out.println(arguments[1]); 38

Frequent issues / Review Good Programming Style Loops Arrays Outline Combining Loops and Arrays 39

Looping through an Array Example 1: int[] values = new int[5]; for (int i=0; i<values.length; i++) { values[i] = i; int y = values[i] * values[i]; System.out.println(y); 40

Looping through an Array Example 2: int[] values = new int[5]; int i = 0; while (i < values.length) { values[i] = i; int y = values[i] * values[i]; System.out.println(y); i++; 41

Good Programming Style Loops Arrays Summary Combining Loops and Arrays 42