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

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

Embed Size (px)

Citation preview

  • CS100J: Lecture 2Previous LectureProgramming Conceptsproblem, algorithm, program, computer, input, output, sequential execution, conditional executionReading: Lewis & Loftus, or Savitch: Chapter 1This LectureProgramming Conceptsvariables and declarations, assignment, expressionsJava Constructsassignment statementexpressionsconditional statementoutput statementcommentsboilerplateblock statementReading: Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, orSavitch, Chapter 2 and Section 3.1

  • Algorithm / Program AProblem statement An integer is given as input data. If it is even, output even, otherwise output odd.

    Program in English1. Get the integer and call it num2. Divide num by 2 and call the remainder rem3. If rem is 0, output even, otherwise output odd

    Program Segment in Java

    num = in.readInt(); rem = num % 2; if ( rem == 0 ) System.out.println("even"); else System.out.println("odd");

  • Initial ObservationsA segment is a part of a programJava is like stylized EnglishSequence of imperatives, known as statementsStatements are not numberedLayout 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");

    English version:names num and rem Java version:memory locations num and remMemory locations are known as variables

  • Variables and Assignmentvariable A place that can hold a value Graphically: name

    Italics designate placeholders, e.g., name stands for some name

    assignment The act of storing a value in a variable.

    num

    destructive update Assigning a value to a variable destroys the previous contents of the variable0value0rem0 70 1remnum

  • Assignment StatementTemplate:

    Meaning: store the value of the expression into the variableRead = as is assigned the value, or becomes equal to, or gets the valueAn expression is a formula (as in algebra)Examplesnum = in.readInt(); rem = num % 2; postage = 33;average = sum / count;count = count + 1;

    variable = expression ;

  • Expression EvaluationVariables: Current value in the variableExampleSuppose variable num contains 7Then in evaluating the expression num % 2 the value of num is 7Constants: Examples: 2, 33, 1Operations: As in algebraExamples+ addition - subtraction* multiplication/ division% remainderFunctions: As in mathematicsExamplessin(x)max(x, y)in.readInt()Parentheses: For grouping

  • Conditional StatementTemplate:

    Meaning: Execute statement1 if expression has value true, otherwise execute statement2 Logical operations:== equality!= inequality< less than greater than>= greater than or equal toelse statement can be omitted, meaning otherwise do nothingif ( expression ) statement1else statement2

  • Output StatementTemplate:

    Meaning: Output the value of expression After output, println advances to the next line, print stays on same lineExamples: System.out.println( 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 outputSystem.out.println( expression );System.out.print( expression );

  • CommentsTemplate:

    Meaning: Ignored by Java, but essential for human understanding Example:

    // Set num to be the input integer. num = in.readInt();// Set rem to be the remainder of num/2. rem = num % 2;if ( rem == 0 ) System.out.println("even");else System.out.println("odd");// any-text-to-end-of-line/* any-text */

  • Program A in Java/* Input an integer and output "even" if it is even, otherwise output "odd. */import java.io.*;public class OddEven{public static void main(String args[]){ int num; // Input integer. int rem; // Remainder of num / 2. // Initialize Text object in to read // from standard input. TokenReader in = new TokenReader(System.in);

    }}

    // Set num to be the input integer. num = in.readInt();// Set rem to be the remainder of num/2. rem = num % 2;if ( rem == 0 ) System.out.println("even");else System.out.println("odd");

  • Outermost Structure Template:

    Meaning: execute declarations, then execute statements, then stop.The comment should specify exactly what task the program performsDeclare each variable in declarationsDont try to understand the rest now/* comment */import java.io.*;public class name{ public static void main(String args[]){ declarations // Initialize Text object in to read // from standard input. TokenReader in = new TokenReader(System.in); statements }}

  • DeclarationsTemplate:

    Meaning: create a list of named variables that can contain int valuesUse meaningful variable names that suggests the variables purposeNames in list-of-variables are separated with commas.Other types of variables besides intExamples:float for numbers in scientific notationboolean for logical values true and falseetc.

    int list-of-variables ; // comment

  • Block StatementMotivation: Some syntactic contexts permit only a single statement, e.g., if ( expression ) statement1 else statement2 To do several things at statement, you need a way to make a sequence of statements act as one statement

    Template:

    i.e., a block statement is a list of statements in curly braces.Meaning: execute each statement in the list-of-statements in sequence

    { list-of-statements }

  • Block Statement, cont.Example:

    if ( expression ) { list-of-statements } else { list-of-statements }

  • Things to DoBuy a floppy disk if you will use public computersProgram 1: do itSections: pick one and attend first meetingReading: Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, orSavitch, Chapter 2 and Section 3.1