COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Java Control Statements
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Chapter 4: Control Structures I (Selection)
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
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
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Chapter 4: Control Structures II
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Catie Welsh February 7,  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Loops October 10, 2017.
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
LOOPS BY: LAUREN & ROMEO.
Control Statements Loops.
Lab5 PROGRAMMING 1 Loop chapter4.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Friday - No Lab! Bring questions on Project 2 Grades online next week 2

Questions? 3

Switch Statements 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; } 4 Controlling expression int or char ONLY! Break statement Case label

Practice with Switch statements Write a switch statement that takes as controlling expression your year in college (as int) and outputs your year in college as freshman, sophomore, junior, senior, or super senior

Shorthand for if / else 6 if (n1 > n2 ) max = n1; else max = n2; max = (n1 > n2) ? n1 : n2;

Loops Loop - part of program that repeats Body - statements being repeated Iteration - each repetition of body Stopping condition 7

Types of Loops while Safest choice Not always most elegant do-while Loop iterates at least ONCE for Numeric computation changes by equal amount 8

While Loops count = 1; while (count <= num) { System.out.print(count + “, “); count++; } 1, 2, 3, 4, 5, 6, … num 9

While Loops Evaluate Boolean Expression Execute Body End loop true false 10

Do-while Loops count = 1; do { System.out.print(count + “, “); count++; } while (count <= num); 1, 2, 3, 4, 5, 6, … num 11

Do-while Loops Evaluate Boolean Expression Execute Body End loop true false 12 Execute Body

For Loops int count; // initializing action; boolean expression; update action for (count = 1; count <= num; count++) { System.out.print(count + “, “); } 1, 2, 3, 4, 5, 6, … num 13

For Loops Evaluate Boolean Expression Execute Body End loop true false 14 Execute Initializing Action Execute Update Action

Types of Loops while Safest choice Not always most elegant do-while Loop iterates at least ONCE for Numeric computation changes by equal amount 15

Bugs Problem with program preventing correct execution Infinite Loops!!!!!! 16

Infinite Loops count = 1; while (count <= num) { System.out.print(count + “, “); //count++; } 17

Infinite Loops count = 1; while (count <= num); { System.out.print(count + “, “); count++; } 18

Infinite Loops int count; // initializing action; boolean expression; update action for (count = 1; count >= num; count++) { System.out.print(count + “, “); } 19

Friday Help with Program 2 20