Glossary. (Very) Simple Java Reference (draft, v0.2)
|
|
- Aileen Lloyd
- 5 years ago
- Views:
Transcription
1 (Very) Simple Java Reference (draft, v0.2) (Very) Simple Java Reference (draft, v0.2) Explanations and examples for if and for (other things to-be-done). This is supposed to be a reference manual, so we expect that you re somewhat familiar with stuff already. (Please send requests for improvements, corrections and expansions to anya@ii.uib.no.) Glossary Statement (utsagn eller setning) code that does something; either calling a method (e.g., System.out.println ("hei!");, or deciding which statement to execute next (e.g., if, for,...) Expression (uttrykk) code that yields a value when the program is running. For example, ( 7), a < b ( true or false), i ( the value of variable i), i++ ( the value of variable i, and increase i after), a[i] ( the value stored at index i in the array stored in the variable a). Value (verdi) the result of a computing an expression. In Java, it will be a number or character (7, 'a', 2.0,...), or a reference to an array or object. All values have types. Variable (variabel) variables store values (either primitive ones like numbers, or references to objects). All variables have types only a value with a compatible type can be put into a variable. Type (type) classification of values, expression and variable. In Java, all expressions will have a type this tells us roughly what sort of value it can have when the program is running. For variable, the type tells you what values you are allowed to store in it. For example, if our program says int a, we know that a will be an integer, even if we don t know which exact integer it will be, and we know that in an assignment like a = e;, the expression e must also compute an integer, otherwise we wouldn t be allowed to put it into a. Types are used to determine what expressions you are allowed to write 1. For example, the things you can do with a string would probably be different from what you can do with a number. So, with String s; int a;, it makes sense to say s.length() (length of a string), but not a.length() (?); and you can say s+"b" (string concatenation) and a+2 (addition), even though you re not allowed to say s-"b". The primitive types are boolean, byte, short, int, long, float, double, char. All other types are reference types, such as String (a reference to a string), int[] (a reference to an array of integers), String[] (a reference to an array of reference to strings). Parameter (parameter) (also argument) a variable containing input to a method. The value used in the method call is placed in the parameter variable before the method starts. Apart from this, they behave like normal variables. Field variable (feltvariabel) (also field, member variable) a variable which is part of an object. Inside methods, you can refer to them directly, or by this.x. 1 This is typically called static typing the compiler checks that all your expressions are well-formed, with compatible types, before the program is run. 1
2 If Statement If Statement Syntax if(condition1) { then-branch1 } else if(condition2) { then-branch2 }... else { else-branch } Condition is tested first and decides which branch should be executed. Must be a Boolean expression (e.g., a logic expression or a comparison) Then-branch is executed if the condition was true. Else-branch (optional) is executed if the condition was false. Else if the else branch may contain another if, for an arbitrarily long sequence of alternatives. The next condition is tested only if the previous one was false. Curly braces around the then and else branches are recommended, but not necessary if the branch has only one statement. Definitely recommended for multi-line statements. Typical Examples 1 if(a > b) 2 return a; 3 else 4 return b; 1 if(s.equals("help")) 2 givehelp(); 3 else if(s.equals("quit")) 4 System.exit(0); 5 else if(s.equals("start")) 6 start(); 7 else 8 error("unknown command: " + s); More If examples Test even/odd: 15 int a = 3; 16 if (a % 2 == 0) { 17 System.out.println("a is even!"); 18 } else { 19 System.out.println("a is odd!"); 20 } (Example 1) a is odd! if vs else if: 24 // what does this print? 25 a = 1; (Example 2) 2
3 26 if (a < 1) 27 System.out.println("less than one!"); 28 else if (a < 2) 29 System.out.println("less than two!"); 30 if (a < 3) 31 System.out.println("less than three!"); less than two! less than three! Empty then-branch (sometimes useful): (Example 3) 35 // here we avoid having to say "a!= 0 &&..." in every condition 36 if (a == 0) 37 ; // do nothing for zero 38 else if (a < 10) 39 System.out.println("between one and ten!"); 40 else if (a < 20) 41 System.out.println("ten and twenty!"); between one and ten! For Loops Syntax label: for(initialiser; condition; update) { body } Initialiser is executed before the first loop iteration starts. (Does nothing if empty.) Loop condition is tested before each iteration (loop exits if false). (Always true if empty.) Loop update is executed after each loop iteration, before the condition is tested. (Does nothing if empty.) Loop body is executed once per iteration. (Does nothing if empty.) Loop label (hardly ever used) used with continue/break if you have nested loops Typical Example 1 for(int i = 0; i < n; i++) { 2 doonething(i); 3 doanotherthing(i); 4 } Strange Examples 3
4 1 // infinite loop: 2 for(;;) { 3 // (same as while(true) {...}) 4 } 5 6 // multiple variables 7 for(int i = 0, j = 100; j > 0; i++, j--) {... } Scope of variables in a for-loop Scope when loop variable is declared in loop initialiser (Example 4) 1 for(int i = 0; i < 10; i++) { // i=0,...,9 2 i for(int j = 0; j < 10; j++) { // j=0,..,9 for each i=0,...,9 3 j System.out.println(" (" + i + "," + j + ")"); 4 } 5 } 6 // new loop, different i: 7 for(int i = 0; i < 10; i++) { // i=0,...,9 8 i System.out.println(" " + i); 9 } Scope when loop variable is declared outside (Example 5) 1 { 2 int i = 0; // loop variable can be declared before: 3 ifor (i = 2; i < 10; i++) { 4 // i=2,...,9 5 System.out.print(" " + i); 6 } 7 // i is now 10: 8 System.out.println(" Value of i after loop: " + i); } A bunch of loop examples Counting : 11 for (int i = 0; i < 10; i++) { // repeat 10 times 12 System.out.print(" " + i); // i=0,1,...,9 13 } (Example 6) Counting : 17 for (int i = 10; i > 0; i--) { // repeat 10 times 18 System.out.print(" " + i); // i=9,8,...,1 19 } (Example 7) Counting again: 23 for (int i = 0; i < 10; i++) { // this is a different i 24 System.out.print(" " + i); 25 } 26 // Note: i is no longer available after the loop: 27 // System.out.println(i); => "i cannot be resolved to a variable" (Example 8) 4
5 Printing with commas between: (Example 9) 31 // many ways to solve this: 32 // if(!first) {print(", "); first=false;}, to avoid printing comma before first number 33 // if(i!=0) print (", ");, to avoid printing comma before first number 34 // if(i!=n 1) print (", ");, to avoid printing comma after last number 35 // or store text of comma, as we have done 36 String comma = ""; 37 for (int i = 0; i < 10; i++) { // repeat 10 times 38 System.out.print(comma + i); // i=0,1,...,9 39 comma = ", "; // next i will get a comma in front of it 40 } 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Do-nothing loop: 44 // this loop stops before it does anything (0 > 0 == false) 45 for (int i = 0; i > 0; i++) { 46 System.out.print(" " + i); 47 } (Example 10) Nested loops: 51 for (int i = 0; i < 10; i++) { // i=0,...,9 52 for (int j = 0; j < 10; j++) { // j=0,..,9 for each i=0,...,9 53 System.out.print(" (" + i + "," + j + ")"); 54 } 55 System.out.println(); // new line for each iteration of outer loop 56 } (Example 11) (0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7) (0,8) (0,9) (1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9) (2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9) (3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9) (4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9) (5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9) (6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9) (7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9) (8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9) (9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9) Two loops after each other: (Example 12) 60 for (int i = 0; i < 10; i++) { // i=0,...,9 61 System.out.print(" " + i); 62 } 63 for (int j = 0; j < 10; j++) { // j=0,...,9 64 System.out.print(" " + j); 65 }
6 Variable declared outside loop: (Example 13) 69 // loop variable can be declared before: 70 int i = 0; 71 for (i = 2; i < 10; i++) { 72 // i=2,...,9 73 System.out.print(" " + i); 74 } 75 // i is now 10: 76 System.out.println(" Value of i after loop: " + i); 77 // i continues to exist ( be in scope ) for the rest of the block 78 // until we reach } 79 // 80 // we can't declare a new i in a loop now, because there's already 81 // an i in scope: 82 // for(int i = 0; i < 10; i++) { } => "i already declared" 83 // but we can reuse the same i: 84 // for(i = 0; i < 10; i++) { } => ok Value of i after loop: 10 Very silly loop: 88 for (int j = 0; j < 10; j++) { 89 for (j = 0; j < 10; j++) { // WTF happens now? 90 System.out.print(" " + j); 91 } 92 } (Example 14) Two equivalent loops: (Example 15) 96 System.out.print(" A for-loop: "); 97 for (int counter = 0; counter < 10; counter++) { 98 System.out.print(" " + counter); 99 } 100 System.out.println(); System.out.print(" A while-loop: "); 103 int counter = 0; 104 while (counter < 10) { 105 System.out.print(" " + counter); 106 counter++; 107 } A for-loop: A while-loop: A loop that operates on strings: (Example 16) 111 for (String s = ""; s.length() < 10; s = s + "*") { 112 System.out.println("'" + s + "'"); 113 } 6
7 '' '*' '**' '***' '****' '*****' '******' '*******' '********' '*********' A loop that computes min/max of a list: (Example 17) 117 int[] myarray = { 3, 2, -1, 9, 5, -10, 0, 8 }; 118 int maxseen = Integer.MIN_VALUE; // largest number seen so far 119 int minseen = Integer.MAX_VALUE; // smallest number seen so far 120 // what should we set maxseen and minseen to before we've seen 121 // any numbers? 122 // we can set them to the first number, myarray[0] 123 // => we must check that we have at least one element! 124 // or the smallest/largest number we can think of 125 for (int k = 0; k < myarray.length; k++) { 126 System.out.printf(" k=%4d, myarray[k]=%3d, minseen=%5d, maxseen=%5d\n", 127 k, myarray[k], minseen, maxseen); 128 if (myarray[k] > maxseen) { 129 maxseen = myarray[k]; 130 } 131 if (myarray[k] < minseen) { 132 minseen = myarray[k]; 133 } 134 } 135 System.out.println(" min=" + minseen + ", max=" + maxseen); k= 0, myarray[k]= 3, minseen= , maxseen= k= 1, myarray[k]= 2, minseen= 3, maxseen= 3 k= 2, myarray[k]= -1, minseen= 2, maxseen= 3 k= 3, myarray[k]= 9, minseen= -1, maxseen= 3 k= 4, myarray[k]= 5, minseen= -1, maxseen= 9 k= 5, myarray[k]=-10, minseen= -1, maxseen= 9 k= 6, myarray[k]= 0, minseen= -10, maxseen= 9 k= 7, myarray[k]= 8, minseen= -10, maxseen= 9 min=-10, max=9 A loop that computes min/max without index (a.k.a. enhanced for-loop"): (Example 18) 139 // same setup as above 140 // int[] myarray = {3,2, 1,9,5, 10,0,8}; 141 // int maxseen = 0; // largest number seen so far 142 // int minseen = 0; // smallest number seen so far 143 if (myarray.length == 0) { 144 throw new IllegalArgumentException("Can't compute min/max of empty array"); 145 } 146 maxseen = myarray[0]; // pick first value as min/max so far 147 minseen = myarray[0]; 148 System.out.println(" myarray=" + Arrays.toString(myArray)); 149 System.out.println(" Loop:"); 150 System.out.printf(" %4s %7s %7s\n", "x", "minseen", "maxseen"); 7
8 151 System.out.printf(" \n"); 152 for (int x : myarray) { // x = myarray[0],myarray[1],..myarray[n 1] 153 System.out.printf(" %4d %7d %7d\n", x, minseen, maxseen); 154 // Math.max/min does the job easier than with "if": 155 maxseen = Math.max(maxSeen, x); 156 minseen = Math.min(minSeen, x); 157 } 158 System.out.println(" result: min=" + minseen + ", max=" + maxseen); myarray=[3, 2, -1, 9, 5, -10, 0, 8] Loop: x minseen maxseen result: min=-10, max=9 Advanced examples A loop with break and continue: (Example 19) 164 for (int k = 0; k < 10; k++) { 165 if (k % 2 == 1) 166 { 167 continue; // jump to next iteration 168 } 169 if (k == 8) 170 { 171 break; // stop if we reach } 173 System.out.print(" " + k); 174 } A loop with continue, finds prime numbers: (Example 20) 178 // warning: this is somewhat bad style 179 // warning:...and it's inefficient. A guy called 180 // Eratosthenes found a better algorithm over 2200 years ago 181 // outer: for (int k = 2; k < 100; k++) { 183 // check if k is dividable by a smaller number 184 // if so, it's not a prime, move on to next 185 for (int divisor = 2; divisor < k; divisor++) { 186 if (k % divisor == 0) 187 { 188 continue outer; // go to next k (label "outer") 189 } 190 } 191 // if we get here, we didn't find any divisor 192 System.out.print(" " + k); 193 } 8
9 A Better Prime Number Generator: (Example 21) 202 // We need an ArrayList, since it can grow in size 203 List<Integer> primes = new ArrayList<>(); 204 outer: for (int k = 2; k < 100; k++) { 205 // we only need to try dividing by prime number; 206 // for any other number, there'll a smaller number / 207 // prime factor that k would be divisible with. 208 // The only possible divisors would be a smaller prime number, 209 // and they'll be in our list of primes! 210 for (int divisor : primes) { 211 if(divisor * divisor > k) { 212 break; // our other primes will be too large too 213 } else if (k % divisor == 0) 214 { 215 continue outer; // go to next k (label "outer") 216 } 217 } 218 // if we get here, we didn't find any divisor 219 primes.add(k); 220 } 221 System.out.println(primes); [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] Find longest consecutive series of positive or negative numbers: (Example 22) 227 int[] numbers = {0, 1, 3, 0, 4, -1, -3, -1, -2, -5, 1, -7, -8, -2}; 228 int last = 0; // last number seen 229 int current = 0; // length of current sequence 230 int longest = current; // longest seen so far 231 System.out.printf(" %4s %7s%7s %7s %7s %-9s %-9s\n", "k", "numbers[k]", "last", "current", "longest", "samesign?", "nonzero?"); 232 System.out.printf(" \n"); 233 for(int k = 0; k < numbers.length; k++) { 234 System.out.printf(" %4d %7d %7d %7d %7d %-9b %-9b\n", k, numbers[k], last, current, longest, last * numbers[k] > 0, numbers[k]!= 0); 235 // slightly better: 236 // Math.signum(numbers[k 1]) Math.signum(numbers[k]) > // (multiplies signs ( 1,0,1) rather than numbers themselves) 238 if(last * numbers[k] > 0) { // same sign 239 current++; 240 } 241 else if(numbers[k]!= 0) { // starting a new series 242 current = 1; 243 } 244 else { // zero is not part of a series 245 current = 0; 246 } 247 last = numbers[k]; 248 longest = Math.max(longest, current); 249 } 250 System.out.println(" Longest series: " + longest); 9
10 k numbers[k] last current longest samesign? nonzero? false false false true true true false false false true false true true true true true true true true true false true false true true true true true Longest series: 5 10
Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationJava 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/
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More informationControl 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
More informationFlow 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
More informationSchool 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...
More informationReview. 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
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)
More informationIntroduction to Software Development (ISD) Week 3
Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationExample: Monte Carlo Simulation 1
Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149
More informationObject Oriented Software Design
Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 30, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction
More informationSequence 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
More informationCSE 114 Computer Science I
CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?
More informationThe for Loop. Lesson 11
The for Loop Lesson 11 Have you ever played Tetris? You know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from
More informationChapter Goals. Contents LOOPS
CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common
More informationcis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1
topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is
More informationCSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!
CSCE 145 Exam 1 Review Answers This exam totals to 100 points. Follow the instructions. Good luck! Chapter 1 This chapter was mostly terms so expect a fill in the blank style questions on definition. Remember
More informationChapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );
Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program
More informationScope 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
More informationCONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationCS 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
More informationAPCS 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
More informationWarmup : Name that tune!
Warmup : Name that tune! Write, using a loop, Java code to print the lyrics to the song 99 Bottles of Beer on the Wall 99 bottles of beer on the wall. 99 bottles of beer. Take one down, pass it around,
More informationRepe$$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
More informationCISC-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
More informationCOMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.
More informationLoops. 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?
More informationAccelerating Information Technology Innovation
Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationLoops / Repetition Statements
Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example
More informationANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions
Birkbeck (University of London) Software and Programming 1 In-class Test 1.2 8 Feb 2018 Student Name Student Number Answer all questions 1. Consider the following sequence of Java statements: int i = 4;
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions
More informationMotivation of Loops. Loops. The for Loop (1) Learning Outcomes
Motivation of Loops Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print the Hello World message
More informationObject-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
More informationUnit 3 Decision making, Looping and Arrays
Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.
More informationLoops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG
Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for and while Primitive
More informationMITOCW watch?v=0jljzrnhwoi
MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To
More information(Refer Slide Time: 00:26)
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements
More informationCSE 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
More informationObject-oriented programming in...
Programming Languages Week 12 Object-oriented programming in... College of Information Science and Engineering Ritsumeikan University plan this week intro to Java advantages and disadvantages language
More informationJava 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
More informationMotivation of Loops. Loops. The for Loop (1) Learning Outcomes
Motivation of Loops Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print
More informationCompSci 125 Lecture 11
CompSci 125 Lecture 11 switch case The? conditional operator do while for Announcements hw5 Due 10/4 p2 Due 10/5 switch case! The switch case Statement Consider a simple four-function calculator 16 buttons:
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationCONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays
CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202
More informationLoops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG
Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for
More informationGeneral Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;
General Syntax Statements are the basic building block of any C program. They can assign a value to a variable, or make a comparison, or make a function call. They must be terminated by a semicolon. Every
More informationCS111: 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
More informationComputer 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
More informationQ1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question
Page 1 of 6 Template no.: A Course Name: Computer Programming1 Course ID: Exam Duration: 2 Hours Exam Time: Exam Date: Final Exam 1'st Semester Student no. in the list: Exam pages: Student's Name: Student
More informationLecture 14. 'for' loops and Arrays
Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate
More informationCOMP-202 Unit 4: Programming with Iterations
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationThe Basic Parts of Java
Data Types Primitive Composite The Basic Parts of Java array (will also be covered in the lecture on Collections) Lexical Rules Expressions and operators Methods Parameter list Argument parsing An example
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More informationRepe$$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
More informationObject-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,
More informationChapter 4 Control Structures
Chapter 4 Control Structures Foundational Java Key Elements and Practical Programming 1 if - else switch Control Structures break and continue Ternary operator while and do-while loops for loops 2 Two
More informationLecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18
Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical
More informationChoose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.
Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points. Use the remaining question as extra credit (worth 1/2 of the points earned). Specify which question is
More informationAnnouncements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop
Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations
More information1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8
Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(
More informationIntroduction 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
More informationProgramming Basics. Digital Urban Visualization. People as Flows. ia
Programming Basics Digital Urban Visualization. People as Flows. 28.09.2015 ia zuend@arch.ethz.ch treyer@arch.ethz.ch Programming? Programming is the interaction between the programmer and the computer.
More informationAP COMPUTER SCIENCE A
AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks
More informationIntroduction 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;
More informationSelec%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
More informationExample. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.
Example Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. 1... 2 Scanner input = new Scanner(System.in); 3 int x = (int) (Math.random()
More informationChapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationAP Computer Science Homework Set 1 Fundamentals
AP Computer Science Homework Set 1 Fundamentals P1A. Using MyFirstApp.java as a model, write a similar program, MySecondApp.java, that prints your favorites. Your program should print your food, your favorite
More informationReminder the scope of a variable is the part of the program where that variable is visible Will this compile?
CS139 Nested Loops Loops and Scope Reminder the scope of a variable is the part of the program where that variable is visible Will this compile? while (number < 10) { String result = "latest " + number;
More information8. 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
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationChapter 6 SINGLE-DIMENSIONAL ARRAYS
Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk What is an Array? A single array variable can reference
More informationJava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions
Birkbeck (University of London) Software and Programming 1 In-class Test 1.1 8 Feb 2018 Student Name Student Number Answer all questions 1. Consider the following sequence of Java statements: int i = 3;
More informationSimple Java Reference
Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...
More informationCondi(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
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationMODULE 2: Branching and Looping
MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written
More informationChapter 4 Introduction to Control Statements
Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?
More informationAll 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..
More informationIntroduction to Java & Fundamental Data Types
Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains
More informationChapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions
Chapter 3 Flow of Control Branching Loops exit(n) method Boolean data type and expressions Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch 1 What is Flow of Control?
More informationAP Computer Science A
AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive
More informationPrinciples of Computer Science
Principles of Computer Science Lecture 2 Dr. Horia V. Corcalciuc Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering (IFIN-HH) January 27, 2016 Loops: do-while do-while loops do
More informationLoops (while and for)
Loops (while and for) CSE 1310 Introduction to Computers and Programming Alexandra Stefan 1 Motivation Was there any program we did (class or hw) where you wanted to repeat an action? 2 Motivation Name
More informationRepetition Through Recursion
Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something
More informationExpressions & Flow Control
Objectives Distinguish between instance and local variables 4 Expressions & Flow Control Describe how instance variables are initialized Identify and correct a Possible reference before assignment compiler
More information1007 Imperative Programming Part II
Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be
More informationBirkbeck (University of London) Software and Programming 1 In-class Test Mar 2018
Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 22 Mar 2018 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment
More informationComputer Science is...
Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic
More informationCS1004: 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
More informationBasic 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
More information