COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1.

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

Logic & program control part 3: Compound selection structures.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
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
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Java Programming: From the Ground Up
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP Mid-Term Review Yi Hong May 27, 2015.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures II
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Catie Welsh February 7,  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Review if Online Time For loop Quiz on Thursday.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
COMP 110 Review for midterm exam
Chapter 5: Control Structures II
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
While Statement.
Michele Weigle - COMP 14 - Spr 04
Control Statements Loops.
Self study.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Control Statements Loops.
Presentation transcript:

COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson 014 1

Announcements Program 1 due Lab 2 due Friday, 2pm Chapters 1 and 2 review solutions posted on web site Program 2 assigned today 2

Questions? 3

String’s substring method 4 UNCisGreat String output = myString.substring(1, 8);

String’s substring method 5 UNCisGreat String output = myString.substring(1, 8);

Today in COMP 110 switch statements loops

Java Example: if/else Is input greater than 10? YesNo Prompt user for integer Print: “big number” Print: “small number” import java.util.*; public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if (inputInt > 10) { System.out.println("big number"); } else { System.out.println("small number"); } } }

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

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

Example problem Which year? 1 Prompt user for year freshman sophomore 23 junior 4 senior Next step

With if/else 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”);

switch statement 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; default: System.out.println(“unknown”); break; } Controlling expression Case labels Break statements Default case: all other values

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

switch statement details 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

Loops Often you need to repeat an action in a program Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

Loops Loop: part of a program that repeats Body: statements being repeated Iteration: each repetition of body Stopping condition Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

Types of loops while do-while for

while loop start; while (not enough sandwiches) { make a sandwich; } distribute sandwiches; Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

while loop syntax while (boolean expression) { statements; }

while loop The stopping condition is a boolean expression. The body code is run if the boolean expression is true. The body must be able to change the value of the boolean expression from true to false. Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

while loop Start Enough sandwiches? Distribute sandwiches No Yes Do a backflip Infinite loop!

Using a while loop Output integers from 1 to 10 int n = 1; System.out.println(n); n = n + 1; System.out.println(n); n = n + 1; System.out.println(n);... // until n == 10

Using a while loop int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }

Program 2 Due Wednesday, September 24 at 2pm NEW: In addition to ing your program, print out your program and hand it in

Friday Lab 2 due Friday, 2pm Bring laptop and textbook 25