Download Computer Programming I – Conditional in Java | CS 121 and more Papers Computer Science in PDF only on Docsity!
CS121: Computer Programming I
Conditionals in Java
Dr Olly Gotel
[email protected]
http://csis.pace.edu/~ogotel
Having problems?
-- Come see me or call me in my office hours
-- Use the CSIS programming tutors
This Week’s Agenda
- Make sure you can do I/O
- Control structures in Java:
- Program flow & flowcharting/pseudocode
- Booleans – values & expressions
- Comparison (relational) operators
- Conditionals:
- if-else statements
- switch statements
- Nested conditionals
Scanner
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
String name;
int age;
name = scan.nextLine();
age = scan.nextInt();
System.out.println(name + age);
Reminder for input
Pick the method to
read in your type
From last week
- Area of a circle
- Characters etc. in a name
- Vending machine
- How about converting GBP to USD?
Write Another
- Write a Java program that:
- asks for a number & stores it in a variable
- asks for another number & stores it in another
variable
- prints out the sum of the two numbers
- prints out the average of the two numbers
- is commented (i.e. says what the program is
called, who wrote it, when, what it does &
anything else you consider useful)
Practice at home
Write Another
- Write a Java program that:
- asks for a user’s name & stores it it a variable
- asks for the user’s year of birth & stores it in a
variable
- prints out the user’s name & displays how old they
will be by the end of 2008
- is commented (i.e. says what the program is
called, who wrote it, when, what it does &
anything else you consider useful)
ALWAYS
DO THIS!
Practice at home
Control Structures
- Java:
- 2 conditional statements
- 3 loop statements
Conditionals enable
selection/choice
Loops enable
repetition
To Understand Conditionals…
- Program flow & how to control it
- Booleans
- Comparison operators
- Boolean operators
If desired, serve with Maple Mustard Sauce
About the Game
- Starting conditions
- Declares variables & sets them to initial values
- Outputs
- Inputs
- Checks the validity of input
- Checks for the choice of input
- Checks the value of variables
- Has conditional branches in the program flow
- Changes the value of variables
- Loops – repeating parts of the program flow
- Terminates
Today!
Controlling the flow of a program
Program Flow – General Concept
- There are statements in programming languages to
control the program flow
- Conditionals:
- execute statements if a condition is true (allows for
alternatives & choice)
- Loops:
- executes statements many times, either while a
condition is true or according to a counter
Flowcharts can help to describe program structure
Flowcharts cont…
Terminator symbol: marks start & end
points of program
Action box: can represent a single
action (e.g. "add x to y") or an entire
sub-process within a larger process
(e.g. “calculate average")
Document: a printed or electronic
document or file
Connector: indicates flow continues on
another page where a matching symbol
(containing the same letter) is placed
More examples
Draw a Flowchart
- Draw a simple flowchart to describe the process of
ordering a cup of coffee from a vending machine
- Think of the questions you may be asked
- Think of the decisions you might need to make
- Think of the actions the machine may need to
perform
Note: there are many ways to crack an egg!
Being Choosy
- Computers are great at handling choices
- Choice is generally made after testing a condition
- This condition can be the value of a variable or the
value of an expression:
- wantMilk equals yes -> addMilk
- sugarLumps less than 4 -> addSugarLump
- Note, these evaluate to true or false
“to be or not to be”
Booleans
- Used to control the flow of program execution
- Two boolean values:
- true (I.e. 1 or yes)
- false (I.e. 0 or no)
- A boolean variable or expression is either true or false
Java has a type
called boolean
Qubit?