Flow of Control.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Control Flow C and Data Structures Baojian Hua
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Logical Operators Jumps Logical Operators The different logical operators found in Java Rational Operators The different rational operators found in Java.
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.
Controlling Execution Dong Shao, Nanjing Unviersity.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
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 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
 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.
Control Flow Statements
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Statements: Part1  if, if…else, switch 1.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
CSE 220 – C Programming Loops.
The if…else Selection Statement
Chapter 6: Loops.
Chapter 4 - Program Control
JavaScript: Control Statements.
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 8: Control Structures
Flow of Control.
Chapter 4: Control Structures I (Selection)
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
Control statements Simple statements Basic structured statements
Chapter 4 - Program Control
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Flow of Control.
Homework Any Questions?.
Chapter 4: Control Structures I (Selection)
Program Flow.
Chapter 4 - Program Control
CprE 185: Intro to Problem Solving (using C)
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Flow of Control

Flow of Control C is a sequential language statements in a program are executed one after another To change flow of control, use choice instructions: if, switch iterative instructions: while, for OR recursion you may need operators

Operators for them < > <= >= == != Relational Operators 1 if the specified relationship is true, and 0 if false. i < 7 > j > 5 <= k <= 4 >= p >= 3 ==  if (x == 4) evaluates to true (or 1) if x is equal to four. if (x = 4) is taken to be true because (x = 4) evaluates to a nonzero value (4). The expression also assigns the value 4 to x. != nChoice != 'Y'

Relational Operators Expressions Evaluates as (3 == 3) && (4 != 3) (3 == 3)  && (4  !=  3) True (1) because both operands are true (4 > 2) || (7 < 11) True (1) because (either) one operand/expression is true (3 == 2)  &&  (7 == 7) False (0) because one operand is false  ! (4 == 3) True (1) because the expression is false NOT(FALSE) = TRUE NOT(TRUE) = FALSE The logical OR (||) should not be confused with the bitwise OR (|) operator. For example:    1 || 4 evaluates to 1 (or True || True = True) while    1 | 4 (0001 | 0100 = 0101) evaluates to 5

some trivial examples

some tricky examples

short-circuit the evaluation stops as soon as the outcome is known expr1 && expr2 if expr1 is evaluated to be false, expr2 needs not be evaluated expr 1 || expr 2

The Compound Statement A compound statement is a series of declarations and statements surrounded by braces { } { int a, b, c; a += b += c; printf (“a = %d, b = %d, c = %d\n”, a, b, c); } a compound is usually called “block” expression statements a + b + c; ; /* empty statement */

if statement if (expr) (then) statement | block statement can be an empty one same for else statement

Iterative Statements while, for, and do statements provide iterative action goto, break, continue, return statements cause an unconditional transfer SE people hate these (except return)

for statements comma operators for (sum = 0, i = 1 ; i <= n ; ++i) for (sum = 0, i = 1 ; i <= n ; sum += i, ++i)

do statement a variant of while statement do { statements } while expr the block is executed first, and then the expr is evaluated you should be able to convert do statement to while statement, and vice versa

goto statement jump to a label it is considered harmful, but ..... goto label; label: /* label is an identifier */ it is considered harmful, but .....

break statement an exit from a loop

continue statement stop the current iteration and goto the next iteration

switch statement

conditional operators x = (y < z) ? y : z if (y < z) x = y; else x = z; equals