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

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Fundamentals of Programming

Lecture 6 Hamed Rasifard

1

slide-2
SLIDE 2

Outline

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

2

slide-3
SLIDE 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

slide-4
SLIDE 4

C’s Operator

  • Arithmetic operators
  • Relational operators
  • Logical Operators

4

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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
  • n value of an expression

10

slide-11
SLIDE 11

Flowcharting the Single- Selection if Statement

11

slide-12
SLIDE 12

if Statement Form

  • Form of if Statement:
  • Parentheses around the expression are

mandatory

  • There is no any then
  • Compound statements:

if (expression) statement if (expression) {statements}

12

slide-13
SLIDE 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

slide-14
SLIDE 14

Flowcharting the Double- Selection if/else statement

14

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Dangling else problem

  • Consider following statements:
  • You should enclose inner if statement in braces

if (y != 0) if (x != 0) result = x/y; else printf(“ERROR, y is equal to 0\n”);

17