CS111: PROGRAMMING LANGUAGE II

Similar documents
CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS110D: PROGRAMMING LANGUAGE I

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

CS1150 Principles of Computer Science Methods

Lecture 5: Methods CS2301

CS1150 Principles of Computer Science Methods

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

ECE 122. Engineering Problem Solving with Java

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

COMP 202 Java in one week

Introduction to Programming (Java) 4/12

Le L c e t c ur u e e 3 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 Control Statements

COMP 202. Java in one week

CP122 CS I. Chapter 6a: Iteration I

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

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

CMPT 125: Lecture 4 Conditionals and Loops

CP122 CS I. Iteration

The Basic Parts of Java

CS302: Self Check Quiz 2

Java Coding 3. Over & over again!

CSC 1051 Data Structures and Algorithms I

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

1007 Imperative Programming Part II

Scope of this lecture. Repetition For loops While loops

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Instructor: Eng.Omar Al-Nahal

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Control Structures in Java if-else and switch

CompSci 125 Lecture 11

CS 112 Introduction to Programming

CS 106 Introduction to Computer Science I

Chapter 3: Program Statements

( &% class MyClass { }

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

Chapter 4: Control structures. Repetition

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

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

CS1004: Intro to CS in Java, Spring 2005

CS-201 Introduction to Programming with Java

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

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

Chapter 4: Control structures

Algorithms and Conditionals

Simple Java Programming Constructs 4

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Week 6 CS 302. Jim Williams, PhD

Conditionals, Loops, and Style

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

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

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Java Flow of Control

Methods CSC 121 Fall 2014 Howard Rosenthal

AP CS Unit 3: Control Structures Notes

Recap: Assignment as an Operator CS 112 Introduction to Programming

FORM 2 (Please put your name and form # on the scantron!!!!)

Computer Programming I - Unit 5 Lecture page 1 of 14

Java Bytecode (binary file)

Programming with Java

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Object Oriented Programming. Java-Lecture 6 - Arrays

Practice Midterm 1 Answer Key

CS 112 Introduction to Programming

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

H212 Introduction to Software Systems Honors

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Condi(onals and Loops

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to the Java Basics: Control Flow Statements

Reviewing all Topics this term

CS171:Introduction to Computer Science II

Personal SE. Functions & Arrays

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CONDITIONAL EXECUTION

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSEN 202 Introduction to Computer Programming

CS159. Nathan Sprague

Method. Why Write Methods? Overview. CS256 Computer Science I Kevin Sahr, PhD. Lecture 21: Writing Class Methods. ClassName.methodName(arguments)

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

Getting started with Java

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

1 Lexical Considerations

CS61B Lecture #2. Public Service Announcements:

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Lecture #6-7 Methods

Introduction to Computer Science Unit 2. Exercises

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Transcription:

CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II)

Lecture Contents Java basics (part II) Conditions Loops Methods

Conditions & Branching

Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic decisions Java's conditional statements: the if and if-else statements the conditional operator the switch statement

The if Statement The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. e.g., a boolean variable, a == b, a <= b. It must evaluate to either true or false. if ( condition ) statement1; else statement2; If the condition is false, this statement is executed. If the condition is true, this statement is executed.

condition evaluated true false Statement 1 Statement 2 Logic of an if-else statement Several statements can be grouped together into a block statement A block is delimited by braces ( { } )

Relational operators

Logical Operators

Loops & Iterations

Loop Statements while statement while ( condition ) statement; do statement do { statement list; } while ( condition ); for statement for ( initialization ; condition ; increment ) statement;

While loops while ( condition ) statement;

Example Look at the code and describe the output!! System.out.print( Enter a month (1 to 12): ); int month = scan.nextint(); while (month < 1 month > 12) { } System.out.println( month + is not a valid month. ); System.out.print( Enter a month (1 to 12): ); month = scan.nextint(); // set initial value of month so that the while condition // below is false initially int month = -1; while (month < 1 month > 12) { } System.out.print( Enter a month (1 to 12): ); month = scan.nextint();

do loops do { statement; } while ( condition );

Example What is the difference here?? int month; // no need to initialize month do { System.out.print( Enter a month(1 to 12): ); month = scan.nextint(); } while (month < 1 month > 12); // beginning of the next statement

for loops for ( initialization ; condition ; increment ) statement;

Example int sum = 0; for (int counter = 1; counter <= max; counter++) sum += counter; // beginning of the next statement Establish initial value of control variable. int counter = 1 Determine if final value of control variable has been reached. counter <= max false true sum+= counter Body of loop (this may be multiple statements) counter++ Incremen the control variable.

Methods A method: groups a sequence of statement takes input, performs actions, and produces output In Java, each method is defined within specific class

Method Declaration: Header A method declaration begins with a method header public class MyClass { static int min ( int num1, int num2 ) properties return type method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument

Method Declaration: Body The header is followed by the method body: class MyClass { static int min(int num1, int num2) { int minvalue = num1 < num2? num1 : num2; return minvalue; } }

The return Statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type The return statement specifies the value that will be returned Its expression must conform to the return type

Calling a Method Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments int num = min(2, 3); static int min (int num1, int num2) { } int minvalue = (num1 < num2? num1 : num2); return minvalue;

Method Call Stack A method can call another method, who can call another method, main min(num1, num2, num3) println() min(1, 2, 3); println( )

That s all for today.. Text Book: Chap 4 Chap 5