60-140 Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Structured Program Development in C
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
The University of Texas – Pan American
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.
 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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 3 Dr. Robert D. Kent Program Control - Details.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Flow of Control Chapter 3 Flow of control Branching Loops
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Branching statements.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
JavaScript: Control Statements.
JavaScript: Control Statements I
MSIS 655 Advanced Business Applications Programming
Structured Program Development in C
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
3 Control Statements:.
Program Control Topics While loop For loop Switch statement
Chapter 3 - Structured Program Development
REPETITION STATEMENTS
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Structural Program Development: If, If-Else
Control Statements:.
Presentation transcript:

Lecture 2b Dr. Robert D. Kent

 Structured program development  Program control

 Structured program development ◦ Sequential nature of instruction logic ◦ Non-sequential instruction logic ◦ Control structures  Repetition  Decision  Selection

 Previously, we discussed program design from the perspectives of ◦ Top-Down Bottom-Up Stepwise-Refinement  In practice, all of these techniques are used ◦ However, the Top-Down approach emphasizes the recognition of processing modules, or units of logic, that must always be processed as complete units  Sometimes these units may be single statements  Usually they are expressed as compound statements { } ◦ The goal of structured programming is to apply formal language control structures that determine the execution of processing units.

 At the level of the machine (CPU hardware) ◦ The RAM address of the next instruction to be executed is stored in a CPU register (storage unit) called the Program Counter (PC) ◦ Machine language instructions (encoded bit strings) are loaded from RAM, using the address stored in the PC, to an Instruction Register (IR) in the CPU ◦ While the IR is being decoded, the PC is incremented by the length (in bytes) of the current instruction.  This last step assumes that the next instruction (in sequence) automatically follows the current instruction in RAM. The DEFAULT mode of process execution is SEQUENTIAL. Statement_1 ; Statement_2 ;..... Statement_N-1 ; Statement_N ;

 Hardware designers have long understood the need to perform logical steps “out of order” ◦ This is called non-sequential logic ◦ Must be able to change the value stored in the CPU Program Counter (PC) register  Requires hardware instructions for this purpose  This is called branching logic

 Forward Branch ◦ if process cond FALSE TRUE TO DO or NOT TO DO ? That is the question!

 Forward Branch ◦ if else cond Tprocess FALSE TRUE Fprocess EITHER OR

 Backward Branch (Code repetition) ◦ Pre-Form: while / for cond process FALSE TRUE Do ONCE or REPEATEDLY

 Backward Branch (Code repetition) ◦ Post-Form: do while cond process FALSE TRUE

 In the C language, several formal control structures have been designed ◦ Repetition  while do-while for ◦ Decision  if if-else ◦ Selection  if-else if-else switch ◦ Each of these structures guide how the compiler generates proper machine language codes that preserve the semantics of the control logic

Multiple selection and For control structures

 Program control ◦ Multiple selection  If-else if-else  switch ◦ Repetition  do-while  for ◦ Break and Continue

 Multiple selection logic arises when a choice between more than two possible outcomes may happen  C provides two control structures to deal with these situations ◦ if-else if-else ◦ switch

 Problem: ◦ Part of a calculator program requires the user to input a value from 1 to 4 indicating his/her choice of the operation to perform on two values A and B (assume A, B already entered)  RESULT: C = A operation B ; ◦ The interpretation of the inputs is defined as  1- Add  2- Subtract  3- Multiply  4- Divide

 Solution using if-else if-else : ◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; if ( Code == 1 ) C = A + B ; else if ( Code == 2 ) C = A – B ; else if ( Code == 3 ) C = A * B ; else C = A / B ; A bit difficult to understand.

 Solution using if-else if-else : ◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; if ( Code == 1 ) C = A + B ; else if ( Code == 2 ) C = A – B ; else if ( Code == 3 ) C = A * B ; else C = A / B ; Much easier. REMEMBER ! Indentation is only for programmers, compilers do not understand indents.

 Solution using switch : ◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; switch ( Code ) { case 1 : C = A + B ; break ; case 2 : C = A – B ; break ; case 3 : C = A * B ; break ; case 4 : C = A / B ; break ; default : printf ( “Error in input\n” ) ; break ; }

 Solution using switch : ◦ printf ( “Enter operation code >” ) ; scanf ( “%d”, &Code ) ; switch ( Code ) { case 1 : C = A + B ; break ; case 2 : C = A – B ; break ; case 3 : C = A * B ; break ; case 4 : C = A / B ; break ; default : printf ( “Error in input\n” ) ; break ; }

 Repetition logic may be of two forms ◦ Pre-condition testing : enter, or re-enter, the loop body if the condition is true. ◦ Post-condition testing : enter the loop body in all cases (performing the body a minimum of once), then repeat the loop body only if the condition is true.  C supports three forms of repetition control structures ◦ while ◦ do-while ◦ for

 while ( condition_expression ) statement ;  while ( condition_expression ) { statement1 ; statementN ; }

 do statement ; while ( condition_expression ) ;  do { statement1 ; statementN ; } while ( condition_expression ) ;

 for ( init_stmt ; cond_expr ; update_stmt ) statement ;  for ( init_stmt ; cond_expr ; update_stmt ) { statement1 ; statementN ; }

 Example: Find the sum of all integers from 1 to 10.  int Sum = 0, k ; for ( k = 1 ; k <= 10 ; k++ ) Sum = Sum + k ;

 Example: Find the sum of all integers from 1 to 10.  int Sum, k ; for ( k = 1, Sum = 0 ; k <= 10 ; k++ ) Sum = Sum + k ;

 C defines two instruction statements that cause immediate, non-sequential alteration of normal sequential instruction processing  Break Logic ◦ Execution of a break ; statement at any location in a loop- structure causes immediate exit from the loop-structure  Continue Logic ◦ Execution of a continue ; statement at any location in a loop-structure causes execution to continue at the beginning of the loop structure (at the next loop iteration) while skipping the remaining statements.

 Break Logic ◦ Execution of a break ; statement at any location in a loop- structure causes immediate exit from the loop-structure  for ( k = 0 ; k < 10 ; k++ ) { if ( k == 5 ) break ; printf ( “%d, ”, k ) ; }  Produces output : 0, 1, 2, 3, 4

 Continue Logic ◦ Execution of a continue ; statement at any location in a loop-structure causes execution to continue at the beginning of the loop structure (at the next loop iteration) while skipping the remaining statements.  for ( k = 0 ; k < 5 ; k++ ) { if ( k == 3 ) continue ; printf ( “%d, ”, k ) ; }  Produces output : 0, 1, 2, 4

 Data ◦ Types, Declarations, Literal values ◦ Input/Output specification codes  Operators  Structured program development ◦ Selection / Decision control logic  If If – else  Switch  Control structures ◦ Repetition (Loop) control logic  Counters Sentinels ◦ While Do-While ◦ For

 Reading ◦ Chapters 1 – 4 (All sections) ◦ Midterm Examination 1 will focus on concepts and techniques from  Chapters 1-3, completely  Chapter 4.1 – 4.4, 4.7, 4.8  Some material presented in Lecture 2 slides may not be tested at this time, but will be tested later  Students are responsible for all material presented in the Lecture slides, as well as all assigned reading and Laboratory exercises and Examinations.