Lec 5 Nested Control Structures

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
Building Java Programs Chapter 5
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Chapter 5: Control Structures II (Repetition)
Lec 6 Logical Operators String comparison. Review – if statement syntax OR.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
More While Loop Examples CS303E: Elements of Computers and Programming.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Midterm 2 Review. Stars and why we use nested loops  Matrix problems (stars)  Other problems involving multiple dimensions  Loops within a process.
Hangman Code Brittany Moses.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
As you come in Take a whiteboard, markers, folder, name tags to your section area. Leave the folders closed.
CSS 290: Video Games and Computer Programming
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 5: Control Structures II (Repetition)
Building Java Programs Chapter 5
Week of 12/12/16 Test Review.
What’s cheating and what is not?
ECE Application Programming
Repetition-Sentinel,Flag Loop/Do_While
Lecture 07 More Repetition Richard Gesick.
CS 106A, Lecture 6 Control Flow and Parameters
Print slides for students reference
Introduction to Programming
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
CS 106A, Lecture 5 Booleans, Control Flow and Scope
Introduction to Programming
Decision Structures, String Comparison, Nested Structures
More Loops.
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Lec 4: while loop and do-while loop
EET 2259 Unit 6 Shift Registers
Go to =>
random number between 1 and 5.
Writing Functions( ) (Part 4)
Building Java Programs
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Review for Final Exam.
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
INC 161 , CPE 100 Computer Programming
Using Objects (continued)
Loops.
Welcome to Mountain View Elementary School!
Reading from and Writing to Files
Repetition Statements
Lec 6 Logical Operators String comparison
Repetition (While Loop) LAB 9
Agenda Remarks about last homework for loop Nested for loop Flowchart
Good Luck Today Make sure you picked up a scan-tron at the door
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
EET 2259 Unit 6 Shift Registers
Lec 21 More Fun with Arrays: For Loops
Reading from and Writing to Files
Week 5 - Friday CS 121.
Presentation transcript:

Lec 5 Nested Control Structures

Calculating the sum or average num k k < 10 Console int sum = 0; int k = 0, num; while ( k < 5) { num = scan.nextInt(); sum = sum + num; k = k + 1; } ...println("sum is " + sum); ...println("avg is " + sum/5.0);

Nesting The idea of nesting is to put one thing inside of another: nested: ( [ { } { } ] { } ) not nested: ( ) [ ] { } [ ] NOT nested: ( [ { ) ] }

Nesting control structures We have seen several control structures do loops while loops if statements (including compound if statements with else if and else ) We can nest any of these structures inside another to get different behaviors

While loop with if inside int i = 0; while ( i < 3) { if ( i == 0) { ...println("zero"); } else { ...println( i ); i = i + 1; i i<3 i==0 Console

While loop with if inside int i = 0, big=0, num; while ( i < 5) { num = scan.nextInt(); if ( num > 50) { big = big+1; } i = i + 1; ...println("number of big: " + big); big num i i<5 Console

Work on Loop Drills in class Due next Tuesday you may finish today these are exam style questions so make sure you master them

Lab 5 Guessing Game pick a random number and store it Do Loop: get user guess if guess is high or low, say it when guess is correct leave loop