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.

Slides:



Advertisements
Similar presentations
Control Structures.
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.
Introduction to C Programming
Programming Languages and Paradigms The C Programming Language.
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.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
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.
Control Flow C and Data Structures Baojian Hua
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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:
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Fundamentals of C and C++ Programming Control Structures and Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
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 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.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
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.
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.
Controlling Execution Dong Shao, Nanjing Unviersity.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 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.
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.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Programmation impérative - Prof. Béat Hirsbrunner
Control Flow (Chapter 3)
Programming Languages and Paradigms
Programming Paradigms
Flow of Control.
Flow of Control.
Chapter 6 Decision Making and Looping
During the last lecture we had a discussion on Data Types, Variables & Operators
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Homework Any Questions?.
Chapter 4: Control Structures I (Selection)
C Programming Getting started Variables Basic C operators Conditionals
Decision making and control functions
Programming Languages and Paradigms
CSC215 Lecture Control Flow.
Controlling Program Flow
Programming Language  C Control Flow
Presentation transcript:

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 to group declarations and statements into a compound statement { x = 0; y = 1; }/* Note: No semicolon after right brace */

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)

Else-if, Section 3.3 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" */

Switch, Section 3.4 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; }

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!!

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 ; }

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)

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

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”

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

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, pg 66: “goto should be used rarely, if at all.” Note: Project coding standards may prohibit “goto”!

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;

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 */ }

Functions / Program Structure C makes functions efficient and easy to use Program != a few big functions Program = = many small functions Functions may reside in separate source files Source files compiled and loaded separately An Introduction to MAKEFILE is coming (A process that K&R does not go into)

Function Prototypes Return type, function name, and ( ) int foo ( ); float foo ( ); Argument List List of Types and optionally Variable Names int foo (int *, int, float); int foo (int array[], int i, float j); Output arguments usually listed first

Function Prototypes Special case for null argument lists Always code function prototype as: int foo (void); Keeps compiler parameter checking enabled Demonstration of bad example: int foo (); x = foo (i, j, k); /* compiler won’t catch! */

Function Declarations Same as function prototype, except: –Must have variable names in argument list –Followed by { function statements; } not ; Example: int foo (int array[], int i, float j) { function statements; }