Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while
|
|
- Amber Patrick
- 5 years ago
- Views:
Transcription
1 Pull Lecture Materials and Open PollEv Poll Everywhere: pollev.com/comp110 Lecture 12 else-if and while loops Once in a while Fall 2016
2
3 if-then-else Statements General form of an if-then-else statement: if (<test condition>) { <then block - runs if test condition is true> else { <else block - runs if test condition is false> We re going to learn a handy special case of if-then-else
4 if s Optional Curly Brace Syntax When you do not use curly braces with an if, Java implicitly wraps curly braces around your SINGLE next statement: if (<condition A>) System.out.println( Hello ); else System.out.println( World ); Is equivalent to: if (<condition A>) { System.out.println( Hello ); else { System.out.println( World );
5 if s Optional Curly Brace Syntax Even though this can lead to terse code, it is dangerous and discouraged because it leads to mistakes A common innocent mistake: if (<condition A>); System.out.println( Hello ); This is equivalent to: if (<condition A>) { ; System.out.println( Hello ); The scary thing is: both are completely valid Java programs.
6 if s Optional Curly Brace Syntax It s also harder to maintain. Say your code looks like this: if (<condition A>) somefile.delete(); For some reason, the file isn t deleting. So you print a message: if (<condition A>) System.out.println( somefile is deleted ); somefile.delete(); Your print message doesn t show but your file is gone. Why? if (<condition A>) { System.out.println( somefile is deleted ); somefile.delete();
7 if s Optional Curly Brace Syntax COMP110 Rule of thumb: ALWAYS use curly braces except in one special case, of course.
8 Nested if-else Statements if (<condition A>) { <runs when condition A is true> else { if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false> When there are more than 2 paths our program can take we ve used if-else statements nested within an else
9 Nested If Else Flow if (<condition A>) { <A s truth body> else { if (<condition B>) { <B s truth body> else { <B s else> A false B false B s else true true B s truth body A s truth body
10 else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else { if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false>
11 else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false>
12 else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false> Dropping the curly braces of an else-block containing a nested if-then statement is encouraged Especially if there s a lot of nesting This is commonly called an else-if statement. Try it in PS2!
13 Follow Along: 8 Ball Pull lecture materials Open Magic8BallRunner.java and Magic8Ball In readanswer, let s change one nested if-then-else to an else-if together
14 Hands-on: 8 Ball 1.In the readanswer method 2.Convert each of the nested if-then-else statements into an else-if statement 3.Make sure your code still runs 4.Check in at pollev.com/comp110
15 Changing Gears
16 PollEv: How many times does the word print? public class Example { public static void main(string[] args) { public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3);
17 Preview: Recursion public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); You can call a method from within the same method. This is called recursion. The recursive call must occur in an if-then-else statement that eventually becomes false. Otherwise? Infinite recursion.
18 How does this work? public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); CPU Example e = new Example(); e.pushups(3); Output: Call Stack:
19 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
20 How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
21 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
22 How does this work? CPU public void pushups(int n) { 2 if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
23 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
24 How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
25 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
26 How does this work? CPU public void pushups(int n) { 1 if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
27 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
28 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
29 How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
30 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
31 How does this work? public void pushups(int n) { 0 if (n > 0) { System.out.println( "); CPU this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
32 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
33 How does this work? CPU false public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
34 How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(0) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
35 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) notice we re back in pushups(1) n: 1 n: 2 pushups(3) n: 3
36 How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(1) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
37 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) notice we re back in pushups(2) n: 2 pushups(3) n: 3
38 How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(2) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
39 How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: notice we re back in pushups(3) pushups(3) n: 3
40 How does this work? CPU public void pushups(int n) { if (n > 0) { done System.out.println( "); with pushups(3) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
41 How does this work? public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); CPU Example e = new Example(); e.pushups(3); Output: Call Stack:
42 Recursion is Powerful public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); We ll explore recursion in more depth after Fall Break Typically when we want to run a block of code repeatedly in Java: We ll use a loop Today: the while loop! Computer Science Fun(damentals) Fact: With recursion we don t actually need loops. You ll appreciate having them, though.
43 while Loop Statement
44 while Loop Statement General form of a while loop statement: while (<boolean expression>) { <while-block: code to repeat> You can use while loops inside any method or constructor Like if-then, the test condition must be a boolean expression A while loop repeatedly runs the code in the while-block while the test condition is true 46
45 while Loop Semantics General form of a while loop statement: while (<boolean test condition>) { <while block: code to repeat> Step 1. Test whether the test condition is true or false. If true, go to step 2. If false, go to step 4. Step 2. Run the code within the while block. Step 3. Go back to Step 1. Step 4. Continue running code after the closing curly brace. 47
46 while Statement Flow In a method, somewhere while Test Condition is false Continue after end of while body true Run code in while block 48
47 Forever and Ever!!" $%&'()*+ Humans: a diamond is forever. while(true) { // Code Computers: A while(true) loop is forever. Computers <3 looping. 49
48 Follow Along Let s Tinker with while Loops Open up lecture comp110.lecture12 s WhileLoopRunner Then open up WhileLoopExamples.java and I ll walk through Example 1 50
49 To End an Infinite Loop Click the RED STOP button in Eclipse. This will force kill your program. Otherwise your battery will be gone real quick. 51
50 How do we write a while loop that will not loop forever? 52
51 How do we write a while loop that will not loop forever? The test condition must eventually become false due to actions taken from within while-block. Here is a common pattern for looping (repeating some set of instructions) N times: int i = 0; while (i < N) { <code to repeat> i = i + 1; 53
52 Let s Tinker with Example 2 54
53 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: declare an local integer variable named i and assign it 0. 0 Output i 55
54 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: is i s value less than 3? If so, enter while loop body. 0 Output i 56
55 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 0 Output i: 0 i 57
56 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 1 Output i: 0 i 58
57 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 1 Output i: 0 i 59
58 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 1 Output i: 0 i 60
59 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 1 Output i: 0 i: 1 i 61
60 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 2 Output i: 0 i: 1 i 62
61 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 2 Output i: 0 i: 1 i 63
62 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 2 Output i: 0 i: 1 i 64
63 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 2 Output i: 0 i: 1 i: 2 i 65
64 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 3 Output i: 0 i: 1 i: 2 i 66
65 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 3 Output i: 0 i: 1 i: 2 i 67
66 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 3 Output i: 0 i: 1 i: 2 i 68
67 cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println( Done:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 3 i Output 69 i: 0 i: 1 i: 2 Done!
68 Follow-along #3: 8 Ball In Magic8BallRunner Let s add the following if-then statement. Substitute your lover / crush name We ll add this after an8ball.shake(); if (question.contains( <lover> )) { // TODO
69 Hands-on #2: 8 Ball Within the if-then statement: 1. Write a while loop whose test condition is true when The String returned by an8ball.readanswer() does not contain the String definitely Hint #1: This: an8ball.readanswer().contains( definitely ) evaluates to true if readanswer() does contain the word, otherwise false. Hint #2: What can you check to see if hint #1 is equal or not equal to using == or!= 2. Within the while-block, call an8ball.shake(); 3. Run your program a few times asking the 8Ball questions that do and don t contain your lovers name. Does your program behave as expected? Check-in on PollEv.com/comp110 when done or stuck.
70 if (question.contains("carol")) { while (an8ball.readanswer().contains("definitely") == false) { an8ball.shake();
71 Follow-along #2: 8 Ball Let s make it so that we can run the 8Ball program forever without having to restart it each time
72 public static void main(string[] args) { Console userinput = new Console(); Magic8Ball an8ball = new Magic8Ball(); while (true) { System.out.println("Ask a yes or no question..."); String question = userinput.askforastring(); an8ball.ask(question); an8ball.shake(); if (question.contains("kris")) { while (an8ball.readanswer().contains("definitely") == false) { an8ball.shake(); System.out.println("====================="); System.out.println(" The 8 Ball Says... "); System.out.println(an8Ball.readAnswer()); System.out.println("=====================");
73 while Loop Recap
Controls Structure for Repetition
Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power
Chapter 1: Introduction to Computers, Programs, and Java
Chapter 1: Introduction to Computers, Programs, and Java 1. Q: When you compile your program, you receive an error as follows: 2. 3. %javac Welcome.java 4. javac not found 5. 6. What is wrong? 7. A: Two
Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals
CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,
Repe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection
STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration
STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the
Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv
Go ahead and PULL Lecture Materials & Sign-in on PollEv Poll Everywhere: pollev.com/comp110 Lecture 16 static methods and Console & JavaFX App Intros Real World Spring 2016 Today Our first apps without
School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn
CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...
COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
Changing an Object s Properties
Go ahead and PULL Lecture Materials & Sign-in on PollEv Right Click Lecture > Team > Pull Poll Everywhere: pollev.com/comp110 Lecture 4 Changing an Object s Properties Fall 2016 Announcements Review Session
CS1150 Principles of Computer Science Loops (Part II)
CS1150 Principles of Computer Science Loops (Part II) Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Review Is this an infinite loop? Why (not)?
Introduction to the Java Basics: Control Flow Statements
Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared
COMP110 Jump Around. Go ahead and get today s code in Eclipse as shown on next few slides. Kris Jordan
Go ahead and get today s code in Eclipse as shown on next few slides COMP110 Jump Around Fall 2015 Sections 2 & 3 Sitterson 014 November 19th, 2015 Kris Jordan kris@cs.unc.edu Sitterson 238 Classroom Materials
COMPUTER PROGRAMMING LOOPS
COMPUTER PROGRAMMING LOOPS http://www.tutorialspoint.com/computer_programming/computer_programming_loops.htm Copyright tutorialspoint.com Let's consider a situation when you want to write five times. Here
Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics
WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java
Following is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.
Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop.
Repe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
Loops. CSE 114, Computer Science 1 Stony Brook University
Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?
CS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work
Introduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto
Open and respond to this poll: PollEv.com/comp110 Lecture 1 Calling Methods Mr. Roboto Fall 2016 Special thanks to Dr. Andy van Dam, my grad school advisor, and Brown s CS15 course for this fantastic method
Repetition CSC 121 Fall 2014 Howard Rosenthal
Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
CS111: PROGRAMMING LANGUAGE II
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
CS 101 Fall 2005 Midterm 2 Name: ID:
This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts (in particular, the final two questions are worth substantially more than any
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR
Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
Condi(onals and Loops
Condi(onals and Loops 1 Review Primi(ve Data Types & Variables int, long float, double boolean char String Mathema(cal operators: + - * / % Comparison: < > = == 2 A Founda(on for Programming any program
Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.
Java SE11 Development Java is the most widely-used development language in the world today. It allows programmers to create objects that can interact with other objects to solve a problem. Explore Java
Java Review. Fundamentals of Computer Science
Java Review Fundamentals of Computer Science Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf Outline Data Types Arrays Boolean Expressions
Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char
Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in
Text Input and Conditionals
Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()
CONDITIONAL EXECUTION
CONDITIONAL EXECUTION yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Conditional Execution if then if then Nested if then statements Comparisons
CSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
TESTING AND DEBUGGING
TESTING AND DEBUGGING zombie[1] zombie[3] Buuuuugs zombie[4] zombie[2] zombie[5] zombie[0] Fundamentals of Computer Science I Outline Debugging Types of Errors Syntax Errors Semantic Errors Logic Errors
Mobile App:IT. Methods & Classes
Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -
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.
CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in
APCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few
COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016 Learn about cutting-edge research over lunch with cool profs January 18-22, 2015 11:30
Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015
Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015 Inside a method The body of a method is a sequence of statements. A statement ends in a semi- colon Types of statements: DeclaraPon of
Programming in the Small II: Control
Programming in the Small II: Control 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University Agenda Selection
Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
Mr. Monroe s Guide to Mastering Java Syntax
Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.
Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1
Tutorials Inf1-OP Conditionals and Loops 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Start this week please let the ITO know if you need to switch tutorial groups. Labs
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
Programming with Java
Programming with Java Variables and Output Statement Lecture 03 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives ü Declare and assign values to variable ü How to use eclipse ü What
CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions
COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements
COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money
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 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8
Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How
Java Programming Fundamentals - Day Instructor: Jason Yoon Website:
Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused
CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions
CMSC 330: Organization of Programming Languages OCaml Expressions and Functions CMSC330 Spring 2018 1 Lecture Presentation Style Our focus: semantics and idioms for OCaml Semantics is what the language
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled
Getting started with Java
Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving
Repetition with for loops
Repetition with for loops So far, when we wanted to perform a task multiple times, we have written redundant code: System.out.println( Building Java Programs ); // print 5 blank lines System.out.println(
Scope of this lecture. Repetition For loops While loops
REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form
CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable
CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to
Java Control Statements
Java Control Statements An introduction to the Java Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhan Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/
Basic Java Syntax. COMP 401, Fall 2015 Lecture 2 1/13/2014
Basic Java Syntax COMP 401, Fall 2015 Lecture 2 1/13/2014 Program OrganizaDon and ExecuDon All code in Java is organized into classes and interfaces One public class or interface per file. Not strictly
Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)
2.1 Functions Functions Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing) Return one output value Input Arguments x y z f Return Value f (x, y, z)
Java Programming Basics. COMP 401, Spring 2017 Lecture 2
Java Programming Basics COMP 401, Spring 2017 Lecture 2 AverageHeightApp take 2 Same as before, but with Eclipse. Eclipse Workspace CreaJng new project CreaJng a new package CreaJng new class Running a
Chapter 7. Iteration. 7.1 Multiple assignment
Chapter 7 Iteration 7.1 Multiple assignment You can make more than one assignment to the same variable; effect is to replace the old value with the new. int bob = 5; System.out.print(bob); bob = 7; System.out.println(bob);
8. Control statements
8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon
CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8
CS 302: INTRODUCTION TO PROGRAMMING Lectures 7&8 Hopefully the Programming Assignment #1 released by tomorrow REVIEW The switch statement is an alternative way of writing what? How do you end a case in
Lec 3. Compilers, Debugging, Hello World, and Variables
Lec 3 Compilers, Debugging, Hello World, and Variables Announcements First book reading due tonight at midnight Complete 80% of all activities to get 100% HW1 due Saturday at midnight Lab hours posted
Computer Programming I - Unit 5 Lecture page 1 of 14
page 1 of 14 I. The while Statement while, for, do Loops Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly. The while statement is one of three looping statements
Repetition, Looping. While Loop
Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions
Flow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled
Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while
All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva
All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..
CS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
COMP 110/L Lecture 5. Kyle Dewey
COMP 110/L Lecture 5 Kyle Dewey Outlines Methods Defining methods Calling methods Methods Motivation Motivation Input Program Output -Start off with some high-level motivation -You write your program,
public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {
Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they
CS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting
Decision Making in C
Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed
CS 177 Week 5 Recitation Slides. Loops
CS 177 Week 5 Recitation Slides Loops 1 Announcements Project 1 due tonight at 9PM because of evac. Project 2 is posted and is due on Oct. 8th 9pm Exam 1 Wednesday Sept 30 6:30 7:20 PM Please see class
Course Outline. Introduction to java
Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.
Introduction to Java https://tinyurl.com/y7bvpa9z
Introduction to Java https://tinyurl.com/y7bvpa9z Eric Newhall - Laurence Meyers Team 2849 Alumni Java Object-Oriented Compiled Garbage-Collected WORA - Write Once, Run Anywhere IDE Integrated Development
COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015
COMP-202: Foundations of Programming Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015 Assignment Due Date Assignment 1 is now due on Tuesday, Jan 20 th, 11:59pm. Quiz 1 is
CS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 01 / 23 / 2015 Instructor: Michael Eckmann Today s Topics Questions? Comments? Review variables variable declaration assignment (changing a variable's value) using
COMP 202 Java in one week
COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010
CS 102 / CS 107 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each)
Object-Oriented Programming
Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,
About this exam review
Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review
University of Palestine. Mid Exam Total Grade: 100
First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int
Java Programming Basics. COMP 401, Fall 2017 Lecture 2
Java Programming Basics COMP 401, Fall 2017 Lecture 2 A Java Program Line number where first loop starts * line number where an array is indexed + number of Jmes line 6 will execute total number of local
CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013
CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 7 problems on the following 6 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the
Midterms Save the Dates!
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump
CS1004: Intro to CS in Java, Spring 2005
CS1004: Intro to CS in Java, Spring 2005 Lecture #16: Java conditionals/loops, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Midterms returned now Weird distribution Mean: 35.4 ± 8.4 What
Java Bytecode (binary file)
Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.
Java Basic Programming Constructs
Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This
Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring
Michele Van Dyne Museum 204B mvandyne@mtech.edu http://katie.mtech.edu/classes/csci136 CSCI 136: Fundamentals of Computer Science II, Spring 2016 1 Review of Java Basics Data Types Arrays NEW: multidimensional
COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 6: Conditionals Jackie Cheung, Winter 2016 This Lecture Finish data types and order of operations Conditionals 2 Review Questions What is the difference between
1.00 Lecture 6. Java Methods
1.00 Lecture 6 Methods and Scope Reading for next time: Big Java: sections 2.6-2.10, 3.1-3.8 Java Methods Methods are the interface or communications between program components They provide a way to invoke
Tools : The Java Compiler. The Java Interpreter. The Java Debugger
Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,