CISC105 – General Computer Science Class 4 – 06/14/2006.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

CS0007: Introduction to Computer Programming
Decisions If statements in C.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Topic R1 – Review for the Midterm Exam. CISC 105 – Review for the Midterm Exam Exam Date & Time and Exam Format The midterm exam will be Tuesday, 3 April.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS 201 Selection Structures (2) and Repetition
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Chapter 5 (Loop Statements)
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
COMP Loop Statements Yi Hong May 21, 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Chapter 5: Structured Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Week 4 – Repetition Structures / Loops
Control Structures Lecture 7.
Looping.
Structured Program
3 Control Statements:.
Chapter 6: Repetition Statements
Repetition and Loop Statements
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

CISC105 – General Computer Science Class 4 – 06/14/2006

Office Hours Michael Haggerty –When: Friday 12-1 (by appointment) –Where: Pearson 115B Robert Derelanko –When: M-F 11-1 (by appointment) –Where: Mitchell 219 By appointment? – –You will need to contact us to set up an appointment to meet with you –We are willing to help you as much as you need, however, we do not want to waste time away by sitting in an empty office! –If you cannot meet during the times above contact me and we will make alternate arrangements

Exam is next week! Format is True/False, Multiple Choice, Short Answer, Write a Program Exam will cover Chapters 1-4, class notes, Lab00 and Lab01 (except loops) –Vocabulary –Compiling, Linking & executing a program –Software Development Method –Coding – understand how to read/write a program Variable types ( int char double ) Functions – declaration, definition, use printf – understand formatting scanf if…else & switch (Selection) –Operator Precedence

Review Algorithm – series of steps to follow in order to solve a problem Program – the implementation of an algorithm written using the rules of the programming language –Can be in machine code or assembly –High-Level Language (like C) must be complied and linked before execution Process – a program in execution

Review – Problem to Program The Software Development Method Use the software development method to define your algorithm –Problem Definition – usually given to you in class/lab –Analyze Problem (inputs, outputs, relevant formulas) –Design Algorithm – what steps should you take to get input and transform it to the output –Implement Algorithm – write C code based on the algorithm –Test – Use test cases to determine if you implemented the algorithm correctly –Maintain – an important step used for “production” software (not necessary for what we are doing here)

Review Program Structure –Preprocessor Directives ( #include & #define ) –The main function ( return(0) ) –Variable Types and Declaration ( int double & char ) –Variable Manipulation –I/O Statements – printf() scanf() Function arguments Format String (placeholders – multiple placeholders) Print list Output Format –\n and the cursor –Numbers

Program Structure Review

Review Functions Function Declaration –What –Where –Why Function Definition –What –Where Function Use –What –Where –How Function Input and Output Arguments

Review Selection Structures ( if…else ) Execution of a statement (s) is dependent on a condition Single, compound, multiple alternatives General Structure if (condition) statement(s) if condition is true else statement(s) if condition is false if statements can be nested within other if statements – use {} to clarify which statement(s) belong together

Review Selection Structures ( switch() ) Used instead of nested loops where you can utilize jumps break; & default:

Review - switch Example switch(n) { case 0: printf("You typed zero.\n"); break; case 3: case 5: case 7: printf("n is a prime number\n"); break; case 2: printf("n is a prime number\n"); case 4: case 6: case 8: printf("n is an even number\n"); break; case 1: case 9: printf("n is a perfect square\n"); break; default: printf("Only single-digit numbers are allowed\n"); }

Lab and Projects Read the Lab Assignment BEFORE lab (Lab02 is posted now – I’ll try to get the rest of the labs posted ASAP) –I will no longer hand out a hardcopy of the lab! Refer to the book and class notes for syntax on writing C programming – the TA is there to help you not write your programs!

Repetition Control Structures Repetition Control Structures are used whenever we need to repeat a group of steps in a program (loop). The statements that are repeated are contained in the loop body (using {} again!) There are many types of loops that we can use in an algorithm – C has statements that can run all loop types

Loop Types Counting – when we know exactly how many time the loop will repeat (use while or for ) Sentinel-Control – Input a list of data in any length ended by a special value (use while or for ) Endfile-Control – input a list of data from a data file (use while or for ) Input Validation – Repeated interactive input of data until a value is within a valid range (use do-while ) General Conditional – Repeated input until a desired condition is met (use while or for )

How to choose the loop type?

Counting loop Algorithm Steps –Initialize loop control variable –Test condition Condition True –Execute Statement(s) –Update loop control variable Condition False –Exit Loop

Counting Loop and while The C identifier while can be used to control a counting loop: initialize loop variable; while (loop repetition condition) { statement(s) run while condition is true; update condition; } control flow continues here when condition is false; Without initialization an Updating the loop can: –Never run –Become endless

Counting Loop and while

What is the output of the following code: int i = 0; while (i <= 5) { printf(“i = %3d\n”, i); i = i + 1; } printf(“All done!\n);

Compute Payroll # emp = 3 Emp1 – Hours 50 Rate 5.25 Emp2 – Hours 6 Rate 5.00 Emp3 – Hours 15 Rate 7.00 Accumulator – value to store a value being computed in increments during the execution of a loop 1-Which variable is the accumulator in this program? total_pay 2-What would the output this program be? Pay is $ Pay is $ Pay is $ All employees processed Total payroll is $

Counting Loop and for The C identifier for can be used to control a counting loop for (initialization expression; loop repetition condition; update expression) statement Similar to a while loop except initialization, condition and update are together.

Counting Loop and for

Increment and Decrement Operator We have seen loop update expressions written as counter = counter + 1; or counter += 1; We can also use: –counter++; is postfix increment – it returns the value of counter then increments –++counter; is prefix increment – it increments counter and then returns the new value Substitute -- for ++ to get a decrement operator –counter--; –--counter;

Prefix vs. Postfix

for loop example

Using loops with increments other than 1 You can control loops with update expressions other than 1 –counter += 10; will increment counter by 10 –Counter -= 10; will decrement counter by 10 Incrementing/Decrementing by values other than 1 would be identified in your algorithm (the Design phase of the Software Development Method) – see Example 5.4 in Hanly/Koffman p.227.

Conditional Loops Sometimes you will not know the number of time that you need to run a loop –Number of repetitions relies on data entered –Keep requesting data until the data is valid The C identifiers while or for can be used to control conditional loops

Conditional Loop Example printf(“Enter a positive number >”); scanf(“%d”, value); while (value < 0) { printf(“Enter a positive number >”); scanf(“%d”, value); } Input validation Example

do…while loop The do…while loop can be use to reduce program input and take the form do { statement(s); } while (condition); Commonly used for input validation

Do…while example do { printf(“Enter a positive number >”); scanf(“%d”, value); } while (value < 0);

Sentinel Controlled Loops Loops that run until a sentinel value (an end marker that follows the last data item) is entered Sentinel Controlled loops still follow the sequence of: –Initialize –Check condition –Update

Sentinel Example

Using for in a Sentinel Loop Can we use for as a Sentinel Loop Control? printf(“Enter first score (or %d to quit)>”,SENTINEL); for (scanf(“%d”, &score); /* Initialize */ score != SENTINEL; /* Test Condition */ scanf(“%d”, &score)) /* Update */ { score += score; printf(“Enter next score (or %d to quit)>”,SENTINEL); }