1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) !right to left * / % left to right + -left.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
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.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Decision Making and Branching (cont.)
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Operators And Expressions
Chapter 4 - Program Control
JavaScript: Control Statements.
Flow of Control.
Looping.
Flow of Control.
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Loops in C.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Flow of Control.
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

1 Flow of Control

2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical Operators and Expressions The Compound Statement The Expression and Empty Statement The if and the if-else Statement

3 Outline (continued) The while Statement The for Statement Example The Comma Operator The do Statement Example: Fibonacci number The goto Statement The break and continue Statements

4 Outline (continued) The switch Statement The Conditional Operators

5 Relational, Equality, and Logical Operators Selection or branch statements are introduced to do alternative actions of computing. The selection is based on true or false Relational, equality, and logical operators are used to navigate the flow of control or structure

6 Relational operatorsLess than< Greater than> Less than or equal to<= Greater than or equal to>= Equality operatorsequal to== not equal to!= logical operators(unary) negation! logical and&& logical or||

7 OperatorsAssociativity ()++(postfix)--(postfix)left to right +(unary) - (unary) ++(prefix)--(prefix)right to left * / %left to right + - left to right >=left to right ==!=left to right &&left to right ||left to right ?:right to left =+=-=*=/=etc right to left,left to right These operators have rules of precedence.

8 Relational Operators and Expressions = Examples a<3 a>b -1.3>=(2.0*x+3.3) a<b<c Not: a=<b a< = b a>>b a-b<0(a-b)<0

9 Relational Operators and Expressions Values of relational expressions a-ba ba = positive0101 zero0011 negative1010

10 Equality Operators and Expressions Demonstrate the use of rules of precedence and associatibvity char c='w''; int i=1, j=2,k=-7; double x=7e+33, y=0.001; 'a'+1<c('a'+1)<c1 -i-5*j>=k+1((-i)-(5*j))>=(k+1)0 3<j<5(3<j)<51 x-3.33<=x+y(x-3.33)<=(x+y)1 x<x+yx<(x+y)0

11 Relational Operators and Expressions == and != are binary operators acting on expressions c=='A' k!=-2 x+y==3*z-7 not: a=b a= = b-1 (x+y) =!44

12 Relational Operators and Expressions expr1-expr2expr1==expr2expr1!=expr2 zero10 nonzero01 example: int i=1,j=2,k=3; i==jj==i0 i!=jj!=i1 i+j+k==-2*-k((i+j)+k)==(-2)*(-k))1

13 Relational Operators and Expressions a!=b!(a==b) a==b and a=b if(a=1) doing nothing … if(a==1) … functioning

14 Logical Operators and Expressions ! is unary operator, and && and || are binary operators !a !(x+7.7) !(a<b||c<d) !!5 not: a! a!=b expr!expr zero (false)1 (true) nonzero (true)0 (false)

15 Logical Operators and Expressions char c='A'; int i=7, j=7; double x=0.0, y=2.3; !c!c0 !(i=-j)!(i=-j)1 !i-j(!i)-j-7 !!(x+y)!(!(x+y))1 !x*!!y(!x)*(!(!y))1

16 Logical Operators and Expressions a&&b a||b !(a<b) &&c 3&&(-2*a+7) not a&& a| |b a&b &b expr1expr2expr1&&expr2expr1||expr2 zerozero00 zerononzero01 nonzerozero01 nonzerononzero11

17 Logical Operators and Expressions expr1expr2expr1&&expr2expr1||expr2 zerozero00 zerononzero01 nonzerozero01 nonzerononzero11

18 Logical Operators and Expressions char c='B'; int i=3,j=3,k=3; double x=0.0,y=2.3; i&&j&&k(i&&j) && k1 x||i&&j-3x||(i&&(j-3))0 i<j&&x<y(i<j)&&(x<y)0 i<j||x<y(i<j)||(x<y)1 'A'<=c&&c<='Z'('A'<=c) &&(c<='Z')1 c-1=='A' ||c+1=='Z'((c-1)=='A')||((c+1)=='Z')1

19 Relational Operators and Expressions short-cut evaluation expr1&&expr2if expr1=0 (false), no need to evaluate expr2 expr1||expr2if expr1 =1 (true), no need to evaluate expr2

20 Relational Operators and Expressions compound statement: series statements are put together inside a brace. It follows scope rule. { a+=b+=c; printf("a=%d, b=%d, c=%d\n", a,b,c); }

21 Relational Operators and Expressions inner and outer blocks, often used in if if, if- else, do-loop, while-loop, and do-while loops { a=1; { b=2; c=3; }

22 The Expression and Empty Statement expression statement is an expression followed by a semicolon (;) empty statement using ; for( ; a<b; ) { if (a<b) a+=2; }

23 The if and the if-else Statement general form if (expr) statement if (grade>=90) printf(Congratulations!\n"); printf("Your grade is %d.\n", grade);

24 The if and the if-else Statement general form if (expr) { statements } if (j<k) { min=j; printf("j is smaller than k\n"); }

25 The if and the if-else Statement general form if (expr) statement1 else statement2 if (x<y) min=x; else min=y;

26 The if and the if-else Statement general form if (expr) { statements (block1) } else { statements (block2) } if (c>'a' &&c<='z') ++lc_cnt; else { ++other_cnt; printf("c is not a lower case letter\n",c); }

27 The if and the if-else Statement else attaches to the nearest if if (a==1) if (b==2) printf("****\n"); else printf("###\n"); if (a==1) if(b==2) printf("****\n"); else printf("###\n"); if (a==1) { if(b==2) { printf("****\n"); } else { printf("###\n"); }

28 The while Statement while statement is introduced to do repetitive action while (expr) statement while (expr) { block } while (i++<n) factorial*=i; while ((c=getchar()!=EOF) { if(c>='a'&& c<='z') ++ lowercase_letter_cnt; ++total_cnt; }

29 The while Statement Make sure there should be at least one exit of the while loop; otherwise, it reaches the endless loop. Example:

30 /* Count blanks, digits, letters, newlines, and others. */ #include 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 = 'A' && c <= 'Z') ++letter_cnt;

31 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; } a.out<cnt_char.c blanks digits letters lines others total

32 The for Statement Alternative loop for repetitive action Usually used for accounting expr1 while (expr2) { statement expr3; } for (expr1; expr2; expr3) statement;

33 The for Statement example: for (i=1;i<=n;++i) factorial*i; for (j=2;k%j==0;++j) { printf(%d is a divior of %d\n", j,k); sum+=j; }

34 Example Boolean algebra plays a major role in design of computer circuits. In this algebra all variables have only the values zero or one.Transistors and memory technologies implement zero-one value schemes with currents, voltages, and magnetic orientations. The circuit designer has a function in mind and needs to check whether, for all possible zero-one inputs, the output has the desired behaviors

35 /* Print a table of values for some boolean functions. */ #include int main(void) { int b1, b2, b3, b4, b5; /* boolean variables */ int cnt = 0; /* headings */ printf("\n%5s%5s%5s%5s%5s%5s%7s%7s%11s\n\n", "Cnt", "b1", "b2", "b3", "b4", "b5", "fct1", "fct2", "majority");

36 for (b1 = 0; b1 <= 1; ++b1) for (b2 = 0; b2 <= 1; ++b2) for (b3 = 0; b3 <= 1; ++b3) for (b4 = 0; b4 <= 1; ++b4) for (b5 = 0; b5 <= 1; ++b5) printf("%5d%5d%5d%5d%5d%5d%6d%7d%9d\n", ++cnt, b1, b2, b3, b4, b5, b1 || b3 || b5, b1 && b2 || b4 && b5, b1 + b2 + b3 + b4 + b5 >= 3); putchar('\n'); return 0; }

37 Cnt b1 b2 b3 b4 b5 fct1 fct2 majority

39 The Comma Operator comma operator has the lowest precedence it is a binary operator It associates from left to right int a, b, c; a=0, b=1; for (sum=0,i=1;i<=n; ++i) sum+=i;

40 The Comma Operator int i,j,k=3; double x=3.3; i=1;j=2;++k+1((i=1), (j=2)), ((++k)+1)5 k!=1, ++x*2.0+1(k!=1), ((++x)*2.0)+1)9.6

41 The do Statement a variant of while-loop at least enter the loop once do { statement (block) } while (expr); i=0; sum=0; so { sum+=i; scanf("%d",&I); } while (i>0);

42 /* A test that fails. infinitive loop */ #include int main(void) { int cnt = 0; double sum = 0.0, x; for (x = 0.0; x != 9.9; x += 0.1) { /* trouble! */ sum += x; printf("cnt = %5d\n", ++cnt); } printf("sum = %f\n", sum); return 0; }

43 Example: Fibonacci number sequence of Fibonacci number is defined as f 0 =0f 1 =1f i+1 =f i +f i-1 (for i=1,2,3,…) 0, 1,1,2,3,5,8,13,21,34,55,89,144,233, … Fibonacci number has a lots of interesting properties, for example, Fibonacci quotient defined as q i ={f i }/{f i-1 } for (i=2,3,4,5,… ) converts to the gold mean (1+sqrt(5)/2

44 /* Print Fibonacci numbers and quotients. */ #include #define LIMIT 46 int main(void) { long f0 = 0, f1 = 1, n, temp; /* headings */ printf("%7s%19s%29s\n%7s%19s%29s\n%7s%19s%29s\n", " ", "Fibonacci", "Fibonacci", " n", " number", " quotient", "--", " ", " "); printf("%7d%19d\n%7d%19d\n", 0, 0, 1, 1); /* first two cases */

45 for (n = 2; n <= LIMIT; ++n) { temp = f1; f1 += f0; f0 = temp; printf("%7ld%19ld%29.16f\n", n, f1, (double) f1 / f0); } return 0; }

46 Fibonacci Fibonacci n number quotient

50 The goto Statement goto statement is used to skip to labeled line not suggest to use! examples while (scanf("%lf",&x)==1) { if (x<0.0) goto negative_alert; printf("%f %f\n", sqrt(x), sqrt(2*x)); } negative_alert: printf("Negative value encountered!\n");

51 The break and continue Statements both used to interrupt normal flow of control break causes to exit from the enclosing loop or switch statement continue causes to skip the current operation and go to the next loop

52 The break and continue Statements both used to interrupt normal flow of control break causes to exit from the enclosing loop or switch statement continue causes to skip the current operation and go to the next loop They both are used with a if statement examples

53 The break and continue Statements while(1) { scanf("%lf", &x); if (x<0.0) break; printf("%f\n", sqrt(x); } … for (i=0;i<TOTAL; ++i) { c=getchar(); if (c>='0' && c<='9') continue; /* skip digit */ … }

54 The switch Statement Multiple selection or conditions It can be generated using nested-if-else statements switch© { case ('a'): … break;

55 switch { case ('a'): … break; case ('b'): … break; case ('c'); … break; default: … }

56 The condition Operator ?: Just simplified if-else, but can be put in one statement expr1?expr2:expr3 x=((y<z)?y:z; if (y<z) x=y; else x=z;

57 The condition Operator ?: char a='a', b='b'; int i=1,j=2; double x=7.07; i==j ? a-1: b+1(i==j)?(a-1):(b+1)99 j%3==0?I+4:x((j%3)==0)?(i+4):x7.07 j%3?i+4:x(j%3)?(i+4):x5.0