Flow of Control.

Slides:



Advertisements
Similar presentations
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Advertisements

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.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) !right to left * / % left to right + -left.
Control Flow C and Data Structures Baojian Hua
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
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.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical.
CPS120: Introduction to Computer Science Decision Making in Programs.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
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”
Computers and Programming CPC The 2 nd lecture Jiří Šebesta.
Controlling Execution Dong Shao, Nanjing Unviersity.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control Flow Statements
CPS120: Introduction to Computer Science Decision Making in Programs.
6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
1 Chapter 6 Loops. Iteration Statements C’s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Chapter 6: Loops.
Programmation impérative - Prof. Béat Hirsbrunner
C Program Controls + Flow Structure
Chapter 4 - Program Control
JavaScript: Control Statements.
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 8: Control Structures
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company. 1
Flow of Control.
Lecture 2: Logical Problems with Choices
Flow of Control.
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
CS1100 Computational Engineering
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Program Control Topics While loop For loop Switch statement
Relational, Logical, and Equality Operators
Homework Any Questions?.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
Dale Roberts, Lecturer IUPUI
ECE 103 Engineering Programming Chapter 18 Iteration
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Chapter 4 - Program Control
Decision making and control functions
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Programming Language  C Control 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

Equality Operators equality expression ::= examples common mistakes expression == expression | expression != expression examples c == ‘A’ k != 2 x + y == 3 * z -7 common mistakes = instead of == = = =!

Equality Operators Examples

Logical Operators logical expressions examples common mistakes negative !expr or expr || expr and expr && expr examples !a !(x + 7.3) !(a < b || c < d) a && b a ||b !(a<b) && c common mistakes a! a&& a & b & b --- this is serious

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)

#include <stdio.h> int main(void) { int blank_cnt = 0, c, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while ((c = getchar()) != EOF) /* braces not necessary */ if (c == ' ') ++blank_cnt; else if (c >= '0' && c <= '9') ++digit_cnt; else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') ++letter_cnt; else if (c == '\n') ++nl_cnt; else ++other_cnt; printf("%10s%10s%10s%10s%10s%10s\n\n", "blanks", "digits", "letters", "lines", "others", "total"); printf("%10d%10d%10d%10d%10d%10d\n\n", blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt, blank_cnt + digit_cnt + letter_cnt + nl_cnt + other_cnt); return 0; }

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 switch (expr1) /* must be integral */ goto the matched case label

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