1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”

Slides:



Advertisements
Similar presentations
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Advertisements

Introduction to C Programming
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Control Flow C and Data Structures Baojian Hua
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
true (any other value but zero) false (zero) expression Statement 2
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
C++ for Engineers and Scientists Third Edition
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 4 Program Control Statements
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
CPS120: Introduction to Computer Science Decision Making in Programs.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Programmation impérative - Prof. Béat Hirsbrunner
Week 3 - Friday CS222.
Control Flow (Chapter 3)
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Flow of Control.
Chapter 6 Decision Making and Looping
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Your questions from last session
Flow of Control.
Homework Any Questions?.
Chapter 4: Control Structures I (Selection)
Control Statements Paritosh Srivastava.
CSC215 Lecture Control Flow.
Controlling Program Flow
Programming Language  C Control Flow
Presentation transcript:

1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization” of details Use for reference to tables, figures, etc. Look up a specific syntax detail you’ve forgotten Don’t expect to copy code for the answer to a problem!! If you find yourself copying code – STOP! THINK! Start Chapter 4 for class after Exam

2 Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and statements into a compound statement { x = 0; y = 1; }/* Note: No semicolon after right brace */

3 if statements, Section 3.2 Shortcut for “equal and not equal to 0” tests Can use: if (expression)if (!expression) In place of if (expression != 0)if (expression == 0)

4 if-else Matching, Section 3.2 Else is matched with the “closest previous else-less if” Example (compiler won’t interpret as the code has been indented on the left. See right side):if (a) if (b)if(b)statement;elsestatement;

5 Switch, Section 3.4 Consider cascading else-if sequence: if (i == 1) /* NOTE: Only one will execute */ statement-1; else if (i == 2) statement-2;... else if (i == 49) statement-49; else statement-50; /* Default or "catch-all" */

6 Switch Also have switch statement **LIKE JAVA** switch (i) { /* in special limited situations */ case 1: statement-1; break; case 2: statement-2; break;... case 49: statement-49; break; default: statement-50; }

7 Switch The way the if-else cascade works is to test –Test if i == 1 –If that fails, test if i == 2. If that fails, test if i == –When we test i == 27, we have done 26 prior tests. With the switch statement, we achieve the same effect, but probably faster: –Usually compiled into assembly language as a jump table –Array of "go to" instructions subscripted by the value of i –If i = 27 we "look up" the “go to” at address 27 in table –And execute only that one “go to” –Note the need for break statements. The default action is to cascade down to the code for the next case!!

8 ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++) with ending 2 */ switch (s[i]) { case '0': case '1': case '2': case '3': case '4': /* fall... */ case '5': case '6': case '7': case '8': case '9': /* through */ n = 16*n + (s[i] - '0'); break; /* need this so don't fall through */

9 ASCII Hex to Integer (Ending 1) case 'a':case 'b':case 'c':case 'd':case 'e': case 'f': n = 16*n + (s[i] - 'a' + 10); break; case ‘A’: case ‘B’: case ‘C’: case ‘D’: case ‘E’: case ‘F’: n = 16*n + (s[i] - ‘A' + 10); break; default: /* stop “for” loop on non hex digit */ flag = 0; break; } return n; }

10 ASCII Hex to Integer (Ending 2) case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': n = 16*n + (s[i] - 'a' + 10); break; case ‘A’: case ‘B’: case ‘C’: case ‘D’: case ‘E’: case ‘F’: n = 16*n + (s[i] - ‘A' + 10); break; default: /* stop “for” loop on non hex digit */ goto ret; } ret: return n; }

11 Loops –while and for, Section 3.5 This “for” statement” for (expr 1 ; expr 2 ; expr 3 ) statement; Is equivalent to this “while” statement: expr 1 ; while (expr 2 ) { statement; expr 3 ; }

12 For loop Note any part of for loop can be left out. for (init; loop-test; increment) If init or increment expression is left out, just not evaluated (program must initialize and increment by other means) If loop-test is left out, assumes permanently true condition and loops forever. (program must break or goto to end to exit the loop)

13 Comma operator: "," Most often used in for loop statement Might have been useful for reverse () in hw1 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) Pairs of expressions separated by "," are evaluated left-to-right and type/value of expression is type value of result. See K & R, pg 53, Precedence Table

14 Do … While, Section 3.6 The do … while tests at the end of the loop do { statement(s); } while (expression); Executes the statement(s) once even if the “while” loop expression is false upon entry Used much less often than “for” and “while”

15 Break and Continue, Sections 3.7 The break statement works for: –for loop / while loop / do loop and switch. –Brings you to end of loop or switch statement ONE LEVEL ONLY. The continue statement works for: –for loop / while loop / do loop, but not switch! –It causes next iteration of enclosing loop to begin

16 GOTO and Labels, Section 3.8 The goto statement breaks TWO OR MORE levels It “goes to” a statement with a label, e.g. ret: It may be OK to use goto if you always do it in a FORWARD direction. (See axtoi, if use a goto ret; then don't need flag or test in “for” loop.) K&R, pg66: “goto should be used rarely, if at all.” Note: Project coding standards may prohibit “goto”!

17 GOTO and Labels, Section 3.8 Don’t implement if-else this way! if (!condition) goto elselabel; printf("if clause\n"); goto nextlabel; elselabel: printf("else clause\n"); nextlabel: /* next statement in program */

18 Binary Search /* binsearch: find x in v[0] <= v[1] <=.. <= v[n-1] returns subscript of x if found, -1 if not */ int binsearch ( int x, int v[ ], int n) { int low, high, mid; low = 0; high = n - 1;

19 Binary Search while ( low <= high) { mid = (low + high)/2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ }