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.

Slides:



Advertisements
Similar presentations
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Advertisements

Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
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.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Control Flow C and Data Structures Baojian Hua
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
The switch Statement, DecimalFormat, and Introduction to Looping
Introduction to Computer Programming in c
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4 Program Control Statements
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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 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.
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
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Week 4 Program Control Structure
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Control Flow Statements
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CPS120: Introduction to Computer Science Decision Making in Programs.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
‘C’ Programming Khalid Jamal.
Chapter 2.2 Control Structures (Iteration)
Structured Program Development in C
Chapter 6 Decision Making and Looping
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
Control Statements Paritosh Srivastava.
Repetition Structures
Presentation transcript:

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 Spring 20112Chapter 3

Statements and Blocks x = 0; { int y; x = 0; i++; printf(...); } Statement Block No semicolon Variable declared Spring 20113Chapter 3

if-else if (expression) statement1 else statement2 if (n > 0) if (a > b) z = a; else z = b; if (n > 0) { if (a > b) z = a; } else z = b; if (n > 0) if (a > b) z = a; else z = b; Spring 20114Chapter 3

else If Spring 20115Chapter 3

Exercise Spring 2011Chapter 36 Using else if statement, write a program that analyzes the variable SeaTemp (initialized to 75 ) and decides the sea-water condition based on the following table: Then set SeaTemp to different values to test your program. Sea Temperature Condition SeaTemp <= 60“cold” 60 < SeaTemp <= 80“pleasant” 80< SeaTemp <= 90“warm” 90 < SeaTemp“uncomfortably warm”

Switch Spring 20117Chapter 3

Switch Spring 20118Chapter 3

Exercise Spring 2011Chapter 39 Using a switch statement, write a program that analyzes the variable SeaTemp (initialized to 75 ) and decides the sea-water condition based on the following table: Then set SeaTemp to different values to test your program. Sea Temperature Condition SeaTemp <= 60“cold” 60 < SeaTemp <= 80“pleasant” 80< SeaTemp <= 90“warm” 90 < SeaTemp“uncomfortably warm”

Loops - While Spring 2011Chapter 310 Evaluate expression Expression = 0 Execute statement Exit the Loop and Continue YESNO loop exit

Exercise – while loop Read characters from the keyboard and copy to the monitor until pattern ‘##’ is entered! Spring 2011Chapter 311

Loops - While and For Spring 2011Chapter 312 Initialization Condition Loop index manipulation

Exercise – for loop Write a program to print out the squares and cubes of the first 100 positive integers. Display a proper heading for the generated table. Spring 2011Chapter 313

Nested for loops Spring 2011Chapter 314 Show the output of the following program by manually going through the code.

Loops - Do-While Spring 2011Chapter 315

Break and Continue Spring 2011Chapter 316  The break statement provides an early exit from for, while, and do, just as from switch.  A break causes the innermost enclosing loop or switch to be exited immediately

Break and Continue Spring 2011Chapter 317  The continue statement causes the next iteration of the enclosing for, while, or do loop to begin.  In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step.  The continue statement applies only to loops, not to switch.

Goto and labels Spring 2011Chapter 318  The goto statement branches to a statement designated by a label  Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it  There are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop Label