The switch Statement, and Introduction to Looping

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
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 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
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.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
CNG 140 C Programming (Lecture set 3)
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Selections Java.
CS161 Introduction to Computer Science
Lecture 3- Decision Structures
JavaScript: Control Statements I
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Outline Altering flow of control Boolean expressions
Chapter 7 Additional Control Structures
Chapter 6: Repetition Structures
Chapter 6: Repetition Statements
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
The System.exit() Method
Lecture Notes – Week 2 Lecture-2
CprE 185: Intro to Problem Solving (using C)
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chap 7. Advanced Control Statements in Java
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

The switch Statement, and Introduction to Looping

Review Binary logical operators combine… You can test a series of condition with the… if-else if statement Nesting is… enclosing one structure inside of another. Binary logical operators combine… two boolean expressions into one. Binary logical operators: && || Unary logical operator: ! What does the == operator compare when the operands are strings? Their references What should you use instead of logical operators when comparing strings? equals compareTo

Comparing if versus switch Statements if (x == 1) y = 4; if else (x == 2) y = 9; else y = 22; Is the same as… switch (x) { case 1: y = 4; break; case 2: y = 9; default: y = 22; }

The switch Statement It is often the case that you want the value of a single variable decide which branch a program should take: if (x == 1) statement or block 1 if else (x == 2) statement or block 2 if else (x == 3) statement or block 3 else statement or block 4 This is tedious and not very aesthetically pleasing. Java provides a structure that lets the value of a variable or expression decide which branch to take This structure is called a switch statement.

The switch Statement switch (SwitchExpression) { case CaseExpression1: General form of a switch statement: switch (SwitchExpression) { case CaseExpression1: //One or more statements break; case CaseExpression2: default: } switch – keyword that begins a switch statement SwitchExpression – a variable or expression that has to be either char, byte, short, or int. case – keyword that begins a case statement (there can be any number of case statements) CaseExpression1 – a literal or final variable that is of the same type as SwitchExpression . After all of the case statements, there is the default case, which begins with the keyword default.

The switch Statement Notes The CaseExpression of each case statement must be unique. The default section is optional. Again, the SwitchExpression and all of the CaseExpressions must be either char, byte, short, or int. Without the break; at the end of the statements associated with a case statement, the program “falls through” to the next case statement’s statements, and executes them. If this is what you actually want, then leave out the break;, but often it isn’t. Why doesn’t default statement have a break; at the end?

Loops So far we have used decision structures to determine which statements are executed and which are not depending on a condition. We used this for: Validation General control flow More specifically, we’ve used decision structures to execute statements that follow the condition one or zero times. What if we want the user to keep trying to put in valid input until she succeeds? How would we do this with decision structures? Can we? Answer: No Solution: Loops A loop is a control structure that causes a statement or group of statements to repeat. We will discuss three (possibly four) looping control structures. They differ in how they control the repetition.

The while Loop while (BooleanExpression) Statement or Block The first looping control structure we will discuss is the while loop. General Form: while (BooleanExpression) Statement or Block First, the BooleanExpression is tested If it is true, the Statement or Block is executed After the Statement or Block is done executing, the BooleanExpression is tested again If it is still true, the Statement or Block is executed again This continues until the test of the BooleanExpression results in false. Note: the programming style rules that apply to decision statements also apply.

The while Loop Flowchart The do-while Loop Flowchart Boolean Expression Statement or Block True False Boolean Expression Statement or Block True False

The do-while Loop while loops are considered pretest, but Java also provides a posttest loop called the do-while loop: do Statement or Block while (BooleanExpression); Here, the Statement or Block is executed first Next, the BooleanExpression is tested If true, the Statement or Block is executed Then the BooleanExpression is tested This continues until the BooleanExpression is false. Again, this is a posttest loop, meaning the BooleanExpression is tested at the end. Note that this means the Statement or Block will ALWAYS be executed at least once. Also not the semicolon at the end of the last line.

while Loop Example New Topics: while Loop Here, number is called a loop control variable. A loop control variable determines how many times a loop repeats. Each repetition of a loop is called an iteration. The a while loop is known as a pretest loop, because it tests the boolean expression before it executes the statements in its body. Note: This implies that if the boolean expression is not initially true, the body is never executed. number <= 5 Print “Hello!” True False number++

Infinite Loops In all but rare cases, loops must contain a way to terminate within themselves. In the previous example number was incremented so that eventually number <= 5 would be false. If a loop does not have a way of terminating it’s iteration, it is said to be an infinite loop, because it will iterate indefinitely. This is a bad logic error! ...often, but always in this class If we removed number++ from the previous example, it would be an infinite loop. Can also be created by putting a semicolon after the loop header or not using brackets properly.