1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tutorial 12 Working with Arrays, Loops, and Conditional Statements
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Statement-Level Control Structures Sections 1-4
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
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”
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
sequence of execution of high-level statements
Controlling Execution Dong Shao, Nanjing Unviersity.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
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,
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
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.
Control Structures sequence of execution of high-level statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Review 1.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Structures.
Flow of Control.
FLOW OF CONTROL.
Flow of Control.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
By Hector M Lugo-Cordero September 3, 2008
CSC215 Lecture Control Flow.
Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008

2 The if-else Statement Form: if ( boolean-expression ) statement 1 else statement 2 If boolean-expression is true, statement 1 is executed; otherwise, statement 2 is Either statement may be compound Either statement may be an if or if - else statement If expression is not of type bool, it is converted to bool

3 The switch Statement Form: switch ( integer-expression ) { case integer-value 1 : action 1 case integer-value 2 : action 2... default: default-action } If expression = value k, action k executed Use break to exit switch statement default optional; default-action executed if no case applies

4 Fall-through switch(x) { case 0: std::cout << "x is zero\n"; case 1: std::cout << "x is one\n"; default: std::cout << "x is something else\n"; } If x==0, then all 3 cases will apply Should use break in case s 0 and 1 Otherwise, will "fall through" to next case and execute its action too

5 Several case s at Once Actions can be empty, allowing multiple case s to use same action Not all values need case ; if none apply, and no default, nothing happens switch(x) { case 0: case 1: case 2: std::cout << "x is 0, 1 or 2\n"; break; default: std::cout << "x is 3 or more\n"; }

6 The do-while Loop Form: do statement while ( boolean-expr ) statement executed at least once Usually a compound statement If boolean-expr is true, loop will continue; otherwise, it will terminate If boolean-expr is the value true, loop will run forever, so a break statement should be used inside the loop

7 The while Loop Form: while ( boolean-expr ) statement Usually a compound statement Iteration will continue as long as boolean-expr is true Statement will not execute at all, if boolean-expr is immediately false Previous cautionary note about infinite loop applies here

8 The for Loop Best for iterating fixed number of times Form: for ( expr 1 ; boolean-expr ; expr 2 ) stmt expr 1 evaluated before loop begins boolean-expr evaluated before each iteration; if false, loop terminates stmt executed during each iteration expr 2 evaluated after each iteration Typical loop that executes n times: for (i = 0; i < n; i++) stmt

9 Example: Gauss-Seidel This program uses Gauss-Seidel iteration to numerically solve a two-point boundary-value problem Illustrates while and for loops, the const qualifier, arrays, math functions, typical code for scientific computing Also available in FORTRAN 90 (Linux archive only), use gfortran command to compile

10 break, continue and goto The break; statement will cause an immediate exit from an enclosing for, do, while or switch statement The continue; statement will interrupt an iteration of a for, do or while loop and begin the next iteration –for loop: expr 2 is evaluated –do loop: boolean-expr is evaluated goto label; jumps to the statement with given label, which is an identifier

11 Example: Tokenizing Input Reads from standard input, recognizes certain C++ tokens (e.g. operators, literals, punctuation, etc.) and prints a list of them (preview of Project 2!) Example of first stage of compiling: interpreting text as language constructs Illustrates: switch statements, "fall- through", while loops, break statements, other aspects of flow control, strings, standard input ( cin )

12 Next Time Namespaces (such as std ) More on the C++ Standard Library Your questions on Project 1 –Don't procrastinate! –Don't hesitate to ask for help!