Control Flow (Chapter 3)

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

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.
1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It.
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.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Control Flow C and Data Structures Baojian Hua
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
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”
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
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.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
CHAPTER 4 DECISIONS & LOOPS
Review 1.
Java Programming Fifth Edition
CSE 220 – C Programming Loops.
Programmation impérative - Prof. Béat Hirsbrunner
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Java for Android is specific
EGR 2261 Unit 4 Control Structures I: Selection
Kontrola toka izvršenja
Chapter 4 - Program Control
Control Statements Lecture 6 from Chapter 5.
CiS 260: App Dev I Chapter 4: Control Structures II.
Conditionals, Loops, and Other Kinds of Statements
11/10/2018.
Java - Data Types, Variables, and Arrays
Chapter 18-3 Recursion Dale/Weems.
In this class, we will cover:
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Program Control Topics While loop For loop Switch statement
Lab5 PROGRAMMING 1 Loop chapter4.
Your questions from last session
Flow of Control.
Homework Any Questions?.
CISC124 Labs start this week in JEFF 155. Fall 2018
Arrays ICS2O.
CMPE212 – Reminders The other four assignments are now posted.
Program Flow.
Controlling Program Flow
In this class, we will cover:
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Chapter 4 - Program Control
Decision making and control functions
Flow of Control.
Arrays and Pointers CSE 2031 Fall May 2019.
CSC215 Lecture Control Flow.
Controlling Program Flow
Arrays and Pointers CSE 2031 Fall July 2019.
Arrays Introduction to Arrays Reading for this Lecture:
Programming Language  C Control Flow
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Chapter 13 Control Structures
Presentation transcript:

Control Flow (Chapter 3) CSE 2031 Fall 2010 6/9/2018 5:21 AM

Statements and Blocks (3.1) Statement: followed by a semicolon. Block enclosed between { and } syntactically equivalent to a single statement no semicolon after the right brace Variables can be declared inside any block.

Control Flow Statements Similar to Java if – else else – if switch while for do – while break break continue goto labels Show examples/code in Chapter 3.

if – else if (n > 0) if (a > b) z = a; else z = b;

if – else – if int binary_search( int x, int v[], int n ) { int low, high, mid; low = 0; high = n - 1; 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 */

switch while ((c = getchar()) != EOF) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; default: nother++; }

while and for Loops while ((c = getchar()) == ' ' || c == '\n' || c = '\t') ; /* skip white space characters */ for (i = 0; i < n; i++) ...

do – while do { s[i++] = n % 10 + '0'; } while ((n /= 10) > 0); Note: the above curly brackets are not necessary. They just make the code more readable.

continue Skip negative elements; increment non-negative elements. for (i = 0; i < n; i++) { if (a[i] < 0) /* skip negative */ continue; a[i]++; /* increment non-negative */ }

break Return the index of the first negative element. ... for (i = 0; i < n; i++) if (a[i] < 0) /* 1st negative element */ break; if (i < n) return i;

goto and Labels for (i = 0; i < n; i++) for (j = 0; j < m; j++) Determine whether arrays a and b have an element in common. for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */ ... found: /* got one: a[i] == b[j] */

Notes Code that relies on goto statements is generally harder to understand and to maintain. So goto statements should be used rarely, if at all. break and continue should be used only when necessary.

Next time ... Functions and program structures (4, C book) Arrays and pointers (5, C book) Testing (II.6, UNIX book)