Catie Welsh February 7, 2011 1.  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Logic & program control part 3: Compound selection structures.
SELECTION II CSC 171 FALL 2004 LECTURE 9. Sequential statements start end Block{…}
Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
1 CS161 Introduction to Computer Science Topic #8.
Digesting a big problem ONE BYTE AT A TIME. Quiz YOU WILL WORK WITH YOUR PARTNER (LAB PARTNER NOT FULL TEAM). FOR SOLOISTS OR THOSE WHOSE PARTNER IS NOT.
Char ch; ch ‘L’‘X’‘V’‘I’ As in Roman numerals Want to give each a value, n say switch (ch) { case ‘I’:n = 1; break; case ‘V’:n = 5; break; … default:cout.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Branching statements.
Switch, Rounding Errors, Libraries
Conditionals & Boolean Expressions
Chapter 4: Making Decisions.
Chapter 5: Control Structures II
Control Statement Examples
Starting Out with Java: From Control Structures through Objects
While Statement.
Selection (if-then-else)
SELECTION STATEMENTS (2)
Lec 4: while loop and do-while loop
The Java switch Statement
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Fundamental Programming
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Branching statements Kingdom of Saudi Arabia
Announcements Program 1 due noon Lab 1 due noon
Presentation transcript:

Catie Welsh February 7,

 Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2

3

 Variable is something that can store a piece of data ◦ Can have variables of type int, double, String, Scanner  Objects are variables that perform actions when you call its methods. ◦ Can have objects of type String, Scanner, Integer, Double  Scanner keyboard = new Scanner(System.in);  int input = keyboard.nextInt(); ClassObject Argument Object Method variable

 var1 = var2 (assignment statement) ◦ Error!!!!!!!  var1 == var2 (boolean expression)  Do NOT use == to compare Strings ◦ string1 == string2 //BAD ◦ string1.equals(string2); //GOOD 5

if (boolean expression); DO NOT DO THIS!!!!!!!

7  More if / else statements  Case statements

 I give you code and input  You give me output 8

 input = 5? input = 6? int days = 0; if (input < 6) System.out.print(“I worked “ + input + “ days this week”); else { days = 6 – input + 1; System.out.print(“I worked “ + days + “ days of overtime”); } 9

 Write a program that takes as input your year in college (as an integer) and outputs your year as freshman, sophomore, junior, senior, or super senior 10

Which year? 1 Prompt user for year freshman sophomore 23 junior 4 senior Next step 5 super senior

if (year == 1) System.out.println(“freshman”); else if (year == 2) System.out.println(“sophomore”); else if (year == 3) System.out.println(“junior”); else if (year == 4) System.out.println(“senior”); else if (year == 5) System.out.println(“super senior”); else System.out.println(“huh?”);

switch(year) { case 1: System.out.println(“freshman”); break; case 2: System.out.println(“sophomore”); break; case 3: System.out.println(“junior”); break; case 4: System.out.println(“senior”); break; case 5: System.out.println(“super senior”); break; default: System.out.println(“unknown”); break; Controlling expression Case labels Break statements Default case: all other values

switch (controlling expression) { case case label: statements; break; case case label: statements; break; default: statements; break; }

 Only int and char can be used in the controlling expression  Case labels must be of same type as controlling expression  The break statement ends the switch statement, go to the next step outside the braces in the code  The default case is optional

 Write a switch statement that takes as the controlling expression the number of siblings a person has (as an int) and outputs an appropriate messages as follows: Number of SiblingsMessage 0An only child 1Just one you say 2Two siblings! 3Big Family! 4 or moreI don’t believe you

switch (numOfSiblings) { case 0: System.out.print(“An only child”); break; case 1: System.out.print(“Just one you say”); break; case 2: System.out.print(“Two siblings!”); break; case 3: System.out.print(“Big family!”); break; default: System.out.print(“I don’t believe you”); break; } 17

if (n1 > n2) max = n1; else max = n2; can be written as max = (n1 > n2) ? n1 : n2;  The ? and : together are call the conditional operator or ternary operator.

 Read  Loop Statements 19