Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Similar documents
Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Visual C# Instructor s Manual Table of Contents

Operators. Java operators are classified into three categories:

SECTION II: LANGUAGE BASICS

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Programming in C++ 5. Integral data types

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

LECTURE 3 C++ Basics Part 2

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 2: Using Data

Chapter 2: Data and Expressions

Ex: If you use a program to record sales, you will want to remember data:

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

JAVA OPERATORS GENERAL

Variables and Functions. ROBOTC Software

CIS 110: Introduction to Computer Programming

Mr. Monroe s Guide to Mastering Java Syntax

CMPT 125: Lecture 3 Data and Expressions

Operators & Expressions

COMP Primitive and Class Types. Yi Hong May 14, 2015

Midterms Save the Dates!

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 2: Data and Expressions

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 4. Operations on Data

CIS133J. Working with Numbers in Java

Chapter 3: Operators, Expressions and Type Conversion

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Chapter 2: Using Data

Chapter 2: Data and Expressions

Expressions and Precedence. Last updated 12/10/18

Basics of Java Programming variables, assignment, and input

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Computer Programming CS F111

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Arithmetic and IO. 25 August 2017

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CSC 1214: Object-Oriented Programming

Full file at

DEPARTMENT OF MATHS, MJ COLLEGE

Introduction to Java & Fundamental Data Types

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Lecture 3 Operators MIT AITI

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A flow chart is a graphical or symbolic representation of a process.

Basics of Java Programming

3. Java - Language Constructs I

Computers Programming Course 6. Iulian Năstac

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

The Warhol Language Reference Manual

CT 229. Java Syntax 26/09/2006 CT229

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

The type of all data used in a C (or C++) program must be specified

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

EXPRESSIONS AND ASSIGNMENT CITS1001

ISA 563 : Fundamentals of Systems Programming

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

ECE 122 Engineering Problem Solving with Java

Getting started with Java

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming Lecture 3

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

4 Programming Fundamentals. Introduction to Programming 1 1

C/C++ Programming for Engineers: Working with Integer Variables

Differentiate Between Keywords and Identifiers

COMP 202 Java in one week

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

1 Lexical Considerations

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

Computational Expression

1.00 Lecture 4. Promotion

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

Declaration and Memory

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

13 th Windsor Regional Secondary School Computer Programming Competition

1007 Imperative Programming Part II

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Lecture 3. Input, Output and Data Types

Fundamentals of Programming CS-110. Lecture 3

COMP 202. Java in one week

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Java Simple Data Types

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Lesson 3: Basic Programming Concepts

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Transcription:

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Second Year Edition Name: Tutorial Group: Groovy can be obtained freely by going to http://groovy-lang.org/download Page 1 of 8

Variables Variables are characters or words which hold a data value. For example, Int X = 12; defines an integer value of 12 which is assigned to the value of x. You must declare the type of variable before initialization which can be done before putting the data type before the variable. There are different data types that you could use; Data type Usage Int For numbers where there are no decimal points e.g. 12, 13 or 14 Double Also known as real numbers which can include negatives. For numbers where there is a decimal point e.g. 12.5, 13.5 Float For numbers where there is a decimal point e.g. 12.5, 13.5 Char For the use of single characters String For multiple character use as long as the data supplied is in quotation marks. Boolean Returns as a true or false depending on data supplied Using Equals When using variables, it is important to understand the use of the equals sign as it has two main functions. A singular equals sign is used to assign a variable to data whereas a pair of equals signs are used to say if a variable is equal to another. e.g. x= 4 Used for assigning variables When x is used with the print command, only the number 4 will be printed. x==4 Is X equal to 4? Printing To print data out in Groovy, we use println() or the print() statements. The difference is println() prints the contents and then starts a new line where as print() prints out the content on the same line as the previous statement. e.g. int x=4; int y=5; Type print() statement println() statement Code Print(4); Print(5); Println(4); Println(5); Output 4 5 4 5 As much as it is subtle, it can make a big difference in your desired output of a system. Page 2 of 8

Expressions and Operators (Part 1) Operator Meaning Example Result + Addition 3+5 8 - Subtraction 10-5 5 * Multiplication 5*5 25 / Division 25/3 8 % Modulo (Remainder) 14%4 2 - or + Unary Unary means it just works with one operand -y Y=5-5 You can also increment a variable by using two plus signs; e.g. int x=5; x++ You can also do the same for decrementing a variable by using two minus signs; e.g. int x=5; x-- Flow of Control A program that does the same thing as previously is a waste of time. Programs have to do different things based on the data they have to work with. What you cannot see when you program is that one instruction is a plus and it runs a machine instruction with decisions being made behind the scenes. These are commonly known as if statements and we are checking if something is true about the data we have on hand. The conditional expression is a test of data if for example, we are checking that x>5. We are not looking at input statement as we have not looked at the input statements. This is because the incantation is more complicated then what we want to look at. Page 3 of 8

If Statements An if statement is a conditional statement which only prints if it is true or false. We use assigned variables to do comparisons. e.g. Int x = 10; If( x <11){ Println( X is smaller than 11 ); This code makes an integer variable called x and assigns the value of 10. Then we use an if statement to say that if x is smaller than 11 then we print out X is smaller than 11. It is important that when using if statements that you use the curly brackets for declaring the block contents. The contents of this if statement is println( X is smaller than 11 ); Expressions and Operators (Part 2) Equality and Relational operators; Operator Meaning < Less than > More than <= Less than or equal to >= More than or equal to!= Not equal to Conditional operators; Operator Meaning Or && and e.g. Int x = 24; Int y = 20; If ((x >= 20) && (y!= 19)){ print( Statement ); This code has 2 conditions with an and statement. It says if X is greater than or equal to 20 and Y is not equal to 19 Then print out statement. If the if statement had instead of && then it would say if X is greater than or equal to 20 or Y is not equal to 19 Then print out statement. Page 4 of 8

While Loops With while statements they loop a block of code until a condition is no longer true. For this type of loop we have a condition and then we have to increment a variable so that the condition will eventually become false. e.g. Int x = 0; While (x < 10){ Println( X is still smaller than 10 ); X++; If x<10 evaluates to true, then the statement is executed. Then, the x<10 is evaluated again. If it evaluates to true, statement is executed again. This repetition continues until the x<10 evaluates to false. We declare a variable called X which has the starting value of 0. Then we say while x is smaller than 10 we want to print of X is still smaller than 10. Then we use the increment operator to add 1 to x after every loop. So this loop will run until x is greater than 10 in which the condition (X<10) is no longer true. Every time the loop runs it adds 1 to x. (x++ increments x by 1). For Loops: For loops are very similar to while loops however they use 3 conditions and are all within the statement line. e.g. For (int i = 0; i < 10; i++){ Println( X is still smaller than 10 ); This code is doing the exact same thing as the while statement except it uses 2 lines of code rather than 4. To begin with, it starts by making a variable i in the first condition (int i = 0). The second condition is whilst i is smaller than 10. And then the final condition is saying to increment i by 1 every time. So to clarify, you make a variable (named i) with a value of 0. Then whilst i < 10 the condition is true. And what we do after each time of running the code is we increment i by 1 (i++). So whilst i<10 we print out x is still smaller than 10. This will run 10 times. If you still don t understand it then here is a groovy screen shot showing what it is doing every time. Page 5 of 8

(Image 1: For loop in Groovy) Arrays Arrays are essentially lists which store set amounts of data. They look like this: int [] array = [1,2,3,4,5]; Arrays are declared like this; int [] array = new int[6]; type Creates array Number of elements in array Page 6 of 8

Array Traversal Here is an array which has been declared in a program called int_array1.groovy. It creates an array of ten integers with each value (including zero but excluding ten) assigned to an integer. The for loop at the bottom of the program allows us to traverse the array and print each value with text. e.g. int [] marks = new int[10]; marks [0] = 23; marks [1] = 5; marks [2] = 17; marks [3] = 0; marks [4] = 2; marks [5] = 9; marks [6] = 10; marks [7] = -4; marks [8] = 3; marks [9] = -8; for (int i = 0; i < marks.length; i++){ println("marks "+ i + " value is: "+ marks[i]); Output marks 0 value is: 23 marks 1 value is: 5 marks 2 value is: 17 marks 3 value is: 0 marks 4 value is: 2 marks 5 value is: 9 marks 6 value is: 10 marks 7 value is: -4 marks 8 value is: 3 marks 9 value is: -8 We start by saying what data type is going into our array (in this case the data type int is going into our array). Then, we declare that it is an array using the square brackets [] and finally we name the array marks. Then, assign the number of values we want to hold (in this case 10) so it will be =new int[10]. Each number uses up a data slot so therefore his array has 10 data slots containing integers which will print values from index 0 to index 9 (including 0, excluding 10) To call a data value you call the name of the array, followed by the data slot number. You should remember that a computers index starts from zero. Let s see how this works for the above shown on the previous page; e.g. int [] marks = new int[10]; marks [0] = 23; marks [1] = 5; marks [2] = 17; Page 7 of 8

marks [3] = 0; marks [4] = 2; marks [5] = 9; marks [6] = 10; marks [7] = -4; marks [8] = 3; marks [9] = -8; School of Computer Science println(marks[5]) Output 9 If we wanted to, could fill an array with integers and request it to print the index of the array. This could be done like so; e.g. int [] marks = new int[10]; for (int i = 0; i < marks.length; i++){ marks[i] = i; println(marks); Output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Be careful when traversing arrays however as languages like Java checks if the program will cause an out of bounds error when you attempt to access an array element that does not exist. Remember that you can declare, create, and initialize an array with a list of literals. e.g. int[] counts = {12, 22, 17, 16, 4, 11; You can use an initializer list only when the array is first declared. Each value must match the type of the array. The values go into the array in the order given and determine the length of the array. References: Max Chapman Notes on Groovy Dr. Margaret Reid-Miller, Carnegie Mellon University - https://www.cs.cmu.edu/~mrmiller/15-110/ Page 8 of 8