Welcome to CS 100

Download Report

Transcript Welcome to CS 100

CS100J: Lecture 2

Previous Lecture
– Programming Concepts

problem, algorithm, program, computer, input,
output, sequential execution, conditional execution
– Reading: Lewis & Loftus, or Savitch: Chapter 1

This Lecture
– Programming Concepts

variables and declarations, assignment, expressions
– Java Constructs

assignment statement

expressions

conditional statement

output statement

comments

boilerplate

block statement
– Reading:
CS 100

Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, or

Savitch, Chapter 2 and Section 3.1
Lecture 2
1
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();
rem = num % 2;
if ( rem == 0 )
System.out.println("even");
else
System.out.println("odd");
CS 100
Lecture 2
2
Initial Observations





A “segment” is a part of a program
Java is like stylized English
Sequence of imperatives, known as
statements
Statements are not numbered
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");

English version:
– names num and rem
Java version:
– memory locations num and rem

Memory locations are known as variables
CS 100
Lecture 2
3
Variables and Assignment


variable A place that can hold a value
Graphically:
name value
Italics designate placeholders, e.g., name
stands for some name

assignment The act of storing a value
in a variable.

rem
0
num
0
destructive update Assigning a value
to a variable destroys the previous
contents of the variable
num
CS 100
0
7
rem
Lecture 2
0
1
4
Assignment Statement

Template:
variable = expression ;

Meaning: store the value of the
expression into the variable

Read “=“ as “is assigned the value”, or
“becomes equal to”, or “gets the value”

An expression is a formula (as in algebra)

Examples
num = in.readInt();
rem = num % 2;
postage = 33;
average = sum / count;
count = count + 1;
CS 100
Lecture 2
5
Expression Evaluation

Variables: Current value in the variable


Constants:


Examples
+
addition
subtraction
*
multiplication
/
division
%
remainder
Functions: As in mathematics


Examples: 2, 33, 1
Operations: As in algebra


Example
– Suppose variable num contains 7
– Then in evaluating the expression num % 2
the value of num is 7
Examples
sin(x)
max(x, y)
in.readInt()
Parentheses: For grouping
CS 100
Lecture 2
6
Conditional Statement

Template:
if ( expression )
statement1
else
statement2

Meaning: Execute statement1 if
expression has value true, otherwise
execute statement2

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”
CS 100
Lecture 2
7
Output Statement

Template:
System.out.println( expression );
System.out.print( expression );

Meaning: Output the value of expression

After output, println advances to the
next line, print stays on same line

Examples:
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 output
CS 100
Lecture 2
8
Comments

Template:
// any-text-to-end-of-line
/* any-text */


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");
CS 100
Lecture 2
9
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");
}
}
CS 100
Lecture 2
10
Outermost Structure

Template:
/* 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
}
}

Meaning: execute declarations, then
execute statements, then stop.
The comment should specify exactly what
task the program performs
Declare each variable in declarations

Don’t try to understand the rest now


CS 100
Lecture 2
11
Declarations

Template:
int list-of-variables ; // comment

Meaning: create a list of named variables
that can contain int values

Use meaningful variable names that
suggests the variable’s purpose

Names in list-of-variables are
separated with commas.

Other types of variables besides int
Examples:
– float for numbers in scientific notation
– boolean for logical values true and false
– etc.
CS 100
Lecture 2
12
Block Statement

Motivation: 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:
{ list-of-statements }
i.e., a block statement is a list of
statements in curly braces.

Meaning: execute each statement in the
list-of-statements in sequence
CS 100
Lecture 2
13
Block Statement, cont.

Example:
if ( expression )
{
list-of-statements
}
else
{
list-of-statements
}
CS 100
Lecture 2
14
Things to Do

Buy a floppy disk if you will use public
computers

Program 1: do it

Sections: pick one and attend first meeting

Reading:
– Lewis & Loftus, Chapter 2 and Sections 3.1-3.5, or
– Savitch, Chapter 2 and Section 3.1
CS 100
Lecture 2
15