Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,

Similar presentations


Presentation on theme: "CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,"— Presentation transcript:

1 CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution, conditional execution –Reading: Lewis & Loftus, or Savitch: Chapter 1 n This Lecture –Programming Concepts n variables and declarations, assignment, expressions –Java Constructs n assignment statement n expressions n conditional statement n output statement n comments n boilerplate n block statement –Reading: n Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, or n Savitch, Chapter 2 and Section 3.1

2 CS 100Lecture 22 Algorithm / Program A Problem statement An integer is given as input data. If it is even, output “even”, otherwise output “odd”. Program in English 1. Get the integer and call it num 2. Divide num by 2 and call the remainder rem 3. If rem is 0, output “even”, otherwise output “odd” Program Segment in Java num = in.readInt(); num = in.readInt(); rem = num % 2; rem = num % 2; if ( rem == 0 ) if ( rem == 0 ) System.out.println("even"); System.out.println("even"); else else System.out.println("odd"); System.out.println("odd");

3 CS 100Lecture 23 Initial Observations n A “segment” is a part of a program n Java is like stylized English n Sequence of imperatives, known as statements n Statements are not numbered n Layout is essential for readability, but irrelevant for meaning, e.g., this code segment has the same effect: num=in.readInt();rem=num%2;if(rem==0) System.out.println("even");else System.out.println("odd"); n English version: –names num and rem Java version: Java version: –memory locations num and rem n Memory locations are known as variables

4 CS 100Lecture 24 Variables and Assignment n variable A place that can hold a value Graphically: name name Italics designate placeholders, e.g., name stands for some name Italics designate placeholders, e.g., name stands for some name n assignment The act of storing a value in a variable. num n destructive update Assigning a value to a variable destroys the previous contents of the variable 0 value 0 rem 0 70 1 remnum

5 CS 100Lecture 25 Assignment Statement n Template: n Meaning: store the value of the expression into the variable n Read “=“ as “is assigned the value”, or “becomes equal to”, or “gets the value” n An expression is a formula (as in algebra) n Examples num = in.readInt(); rem = num % 2; postage = 33; average = sum / count; count = count + 1; variable = expression ;

6 CS 100Lecture 26 Expression Evaluation n Variables: Current value in the variable n Example –Suppose variable num contains 7 –Then in evaluating the expression num % 2 the value of num –Then in evaluating the expression num % 2 the value of num is 7 n Constants: Examples: 2, 33, 1 Examples: 2, 33, 1 n Operations: As in algebra n Examples + addition - subtraction * multiplication / division % remainder n Functions: As in mathematics n Examples sin(x) max(x, y) in.readInt() n Parentheses: For grouping

7 CS 100Lecture 27 Conditional Statement n Template: Meaning: Execute statement 1 if expression has value true, otherwise execute statement 2 Meaning: Execute statement 1 if expression has value true, otherwise execute statement 2 n Logical operations: == equality != inequality < less than <= less than or equal to > greater than >= greater than or equal to “ else statement ” can be omitted, meaning “otherwise do nothing” “ else statement ” can be omitted, meaning “otherwise do nothing” if ( expression ) statement 1 statement 1else statement 2 statement 2

8 CS 100Lecture 28 Output Statement n Template: n Meaning: Output the value of expression After output, println advances to the next line, print stays on same line After output, println advances to the next line, print stays on same line n Examples: System.out.println( ”even” ); System.out.println( ”even” ); System.out.println( num + ” is even”); System.out.println( num + ” is even”); In the second example, num is converted to a textual representation, which is then concatenated to the 8 letters “ is even”, and output In the second example, num is converted to a textual representation, which is then concatenated to the 8 letters “ is even”, and output System.out.println( expression ); System.out.print( expression );

9 CS 100Lecture 29 Comments n Template: n Meaning: Ignored by Java, but essential for human understanding n Example: // Set num to be the input integer. num = in.readInt(); num = in.readInt(); // Set rem to be the remainder of num/2. rem = num % 2; rem = num % 2; if ( rem == 0 ) System.out.println("even"); System.out.println("even");else System.out.println("odd"); System.out.println("odd"); // any-text-to-end-of-line /* any-text */

10 CS 100Lecture 210 Program A in Java /* Input an integer and output "even" if it is even, otherwise output "odd. */ is even, otherwise output "odd. */ import java.io.*; public class OddEven { public static void main(String args[]) { int num; // Input integer. int num; // Input integer. int rem; // Remainder of num / 2. int rem; // Remainder of num / 2. // Initialize Text object in to read // Initialize Text object in to read // from standard input. // from standard input. TokenReader in = new TokenReader(System.in); TokenReader in = new TokenReader(System.in);}} // Set num to be the input integer. num = in.readInt(); num = in.readInt(); // Set rem to be the remainder of num/2. rem = num % 2; if ( rem == 0 ) System.out.println("even"); System.out.println("even");else System.out.println("odd"); System.out.println("odd");

11 CS 100Lecture 211 Outermost Structure n Template: n Meaning: execute declarations, then execute statements, then stop. n The comment should specify exactly what task the program performs Declare each variable in declarations Declare each variable in declarations n Don’t try to understand the rest now /* comment */ import java.io.*; public class name { public static void main(String args[]) public static void main(String args[]){ declarations declarations // Initialize Text object in to read // Initialize Text object in to read // from standard input. // from standard input. TokenReader in = new TokenReader(System.in); TokenReader in = new TokenReader(System.in); statements statements }}

12 CS 100Lecture 212 Declarations n Template: Meaning: create a list of named variables that can contain int values Meaning: create a list of named variables that can contain int values Use meaningful variable names that suggests the variable’s purpose Use meaningful variable names that suggests the variable’s purpose Names in list-of-variables are separated with commas. Names in list-of-variables are separated with commas. Other types of variables besides int Other types of variables besides int Examples: –float for numbers in scientific notation –boolean for logical values true and false –etc. int list-of-variables ; // comment

13 CS 100Lecture 213 Block Statement n Motivation: Some syntactic contexts permit only a single statement, e.g., if ( expression ) statement 1 statement 1 else else statement 2 statement 2 To do several things at statement, you need a way to make a sequence of statements act as one statement To do several things at statement, you need a way to make a sequence of statements act as one statement n Template: i.e., a block statement is a list of statements in curly braces. i.e., a block statement is a list of statements in curly braces. n Meaning: execute each statement in the list-of-statements in sequence { list-of-statements }

14 CS 100Lecture 214 Block Statement, cont. n Example: if ( expression ) { list-of-statements list-of-statements } else else { list-of-statements list-of-statements }

15 CS 100Lecture 215 Things to Do n Buy a floppy disk if you will use public computers n Program 1: do it n Sections: pick one and attend first meeting n Reading: –Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, or –Savitch, Chapter 2 and Section 3.1


Download ppt "CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,"

Similar presentations


Ads by Google