fundamentals of programming
play

Fundamentals of Programming Lecture 6 Hamed Rasifard 1 Outline - PowerPoint PPT Presentation

Fundamentals of Programming Lecture 6 Hamed Rasifard 1 Outline Expressions Assignment Operators Increment and Decrement Operators if Selection Structure if/else Selection Structure 2 Expressions Formulas that show how


  1. Fundamentals of Programming Lecture 6 Hamed Rasifard 1

  2. Outline • Expressions • Assignment Operators • Increment and Decrement Operators • if Selection Structure • if/else Selection Structure 2

  3. Expressions • Formulas that show how to compute a value • The simplest expressions are variables and constants • More complicated expressions apply operators and operands 3

  4. C’s Operator • Arithmetic operators • Relational operators • Logical Operators 4

  5. Assignment Operators • C’s Assignments operator: Simple Assignment Compound Assignment • lvalue: represent an object stored in memory • Assignment operator requires a lvalue as its left operator 5

  6. Simple assignments • Simple assignment operator (=) is used to store the value of a expression in a variable int i,j,k; float f; i = 7; j = i * 10; k = 2 * ( j = i); k = j = i = 9; f = i = 77.7f; 6

  7. Compound Assignment • It is common to use old value of a variable to compute new value of that variable • Compound assignments operator let us to shorten this kind of statements • Compound operators: +=, -=, *=, /=, %= simple assignment: i = i + 2; compound assignment: i += 2; 7

  8. Increment and Decrement Operators • Two most common operation on a variable are incrementing and decrementing • simple and compound assignments could be used for incrementing and decrementing a variable • C’s increment (++) and decrement (--) assignments allow us to shorten these expression even further i = i + 1; i++; or ++i; i = i - 1; i--; or --i; 8

  9. True or False • The value of a expression is true or false • Several of C’s statements work based on the value of a expression • In c, integer number 0 is interpreted as false, and integer number 1 is interpreted as true 9

  10. if Selection Statement • Dynamic programs need to have various output to various situations • if statement allows a program to choose a particular execution path from two alternatives • if statement decides the execution path based on value of an expression 10

  11. Flowcharting the Single- Selection if Statement 11

  12. if Statement Form • Form of if Statement: if (expression) statement • Parentheses around the expression are mandatory • There is no any then • Compound statements: if (expression) {statements} 12

  13. if/else Statement • if/else statement let us have different actions when expression value is true or false • General form of if/else statement: if (expression) {statement(s)} else {statement(s)} 13

  14. Flowcharting the Double- Selection if/else statement 14

  15. Nested if/else statements • Test for multiple cases by placing if/else statement inside if/else statements if (expression) {statement(s)} else if (expression) {statement(s)} else if (expression) {statement(s)} else . . . 15

  16. Other Form of Nested if/else Statement if (expression) {statement(s)} else if {statement(s)} else if {statement(s)} else if {statement(s)} else if {statement(s)} . . . 16

  17. Dangling else problem • Consider following statements: if (y != 0) if (x != 0) result = x/y; else printf(“ERROR, y is equal to 0\n”); • You should enclose inner if statement in braces 17

Recommend


More recommend