CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.

Slides:



Advertisements
Similar presentations
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 4: Control Structures II
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
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.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Application development with Java Lecture 6 Rina Zviel-Girshin.
COMP Loop Statements Yi Hong May 21, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
Control Statements: Part 2
Engineering Problem Solving with C++, Etter/Ingber
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecturer CS & IT Department UOS MBDIN
JavaScript: Control Statements.
Loop Control Structure.
Chapter 4 - Program Control
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow

How are the subtasks in an algorithm combined? They can be combined as a sequence: –first execute task1, then execute task2 They can be combined using selection: –check a condition, if the condition is true execute task1, if not execute task 2 They can be combined using repetition (aka iteration): –repeat task1 until a condition becomes true Structured programming languages (like C) have language constructs for composing tasks using sequence, selection and repetition –We call these control structures since they control the execution order of the statements

C has 7 basic control structures 1 st is trivial: sequence structure: statements listed sequentially are executed sequentially a = 4; b = 5; 3 choices of selection structures: –if –if/else –switch 3 choices of repetition structures: –while –for –do/while

Sequence in flowchart

if selection structure as a flowchart

if/else Selection Structure

Practice True/False questions Say int x=2, y=8, z=17; –What is ( x x && y > z )? –What is ( x < y - z )? –What is ( x + y > y + z / y )? What is ( --z == x * y++ )? –And after that statement executes, what is ( z > y * x )?

Practice True/False questions Say int x=2, y=8, z=17; –What is ( x x && y > z )? –What is ( x < y - z )? –What is ( x + y > y + z / y )? What is ( --z == x * y++ )? –And after that statement executes, what is ( z > y * x )? true false true false

Updated precedence summary

More Boolean expressions Note a difference from math descriptions: –In math: (0 < value < 10) that expression is always true in C no matter what value is –In C must be: (0 < value && value < 10) De Morgan’s Law – to simplify boolean expressions –Let A and B represent boolean values 1. !(A && B) is the same as !A || !B 2. !(A || B) is the same as !A && !B

switch selection structure

switch statement syntax switch (controlling expression) { case constant expression: statements; break; /* otherwise “fall through” */ case constant expression: statements; break;... default: statements without a condition; } –See …/demos/gradepoints.c …/demos/gradepoints.c

Nesting & indenting No such thing as multiple else blocks – others actually nested inside else block –e.g., if (grade >= 90) message = “ Excellent ” ; else if (grade >= 60) message = “ Pass ” ; else message = “ Fail ” ; –Gets messy, so usually else/if on same line: else if (grade >= 60) …

Nesting/indenting (cont.) Critical to test relations in the correct order –Sometimes means stating the negative condition Also watch out for “dangling else” problems if (first-level condition) if (second-level condition) do something; else (what level?) … –last else goes with the closest if

while Iteration Structure (Loop)

while statement syntax while (boolean expression) operation; /* or a block { } */ E.g., do something 10 times: int counter = 0; /* initialize */ while (counter != 10) { /* compare to limit */... ++counter; /* increment (or change) */ } –e.g., whiletab.c (4 th Ed. Program chapter3_2, p. 103)whiletab.c Note: better structure type for “counter-controlled” loop soon while is best type when termination is uncertain –e.g., processing the characters or “tokens” in a string –e.g., reading unlimited lines of data from a file

for Iteration Structure

for statement syntax More natural than while to implement counter- controlled loops : int c; for (c = 0; c < 10; c++) /* statement; or {…} */ Example: fortab.c (p. 106)fortab.c Note: header requires three fields (i.e., always two “ ; ”), but okay to leave blank or even use, to do multiple things initialize compare modify

do/while Iteration Structure

do/while do {... /* loop body */ } while ( boolean expression ); Example: dotab.c (p. 104)dotab.c Notes: –Always executes at least once e.g., good for user input checking –Don’t forget the semicolon at the end

break, continue statements Sanctioned ways to loosen the structures –Without using the dreaded goto statement! –Usually as a result of some condition: e.g., if ( /* a good reason */ ) break; break – says exit the structure now –Control diverts to 1 st statement after closing ` } ` Already saw how it skips remaining cases in switch structure continue – says terminate this iteration of loop –Repetition structures only – restarts at next iteration

Review: 7 basic control structures in C Iteration Selection Sequence

7 basic control structures in C Sequence structure: statements listed sequentially are executed sequentially a = 4; b = 5; 3 selection structures: –if –if/else –switch 3 repetition (loop) structures: –while –for –do/while