conditional control flow by muhammad ahsan qadar sacs programming fundamentals workshop 1

13
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

Upload: shon-ellis

Post on 31-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

1

Conditional Control Flow

By Muhammad Ahsan Qadar

Page 2: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

2

Introduction

• Here we would discuss various constructs of C language which would be used to control the flow of the program.

• “Control flow” is the order in which statements are executed.

Page 3: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

3

Flow of Control

• There are three flow of control in the programming languages:– The sequential control flow– The conditional control flow– The iteration control flow

• We will be discussing the Conditional Control Flow in this lecture today.

Page 4: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

4

What is a Condition???

• What is the value of a conditional expression???

• How can we facilitate the conditional expression with Boolean operators???

• Use of Flow Charts…

• What is short circuiting???

Page 5: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

5

Condition

• In parentheses is a condition, also called a “logical” or “Boolean” expression.

• Made up of variables, constants, arithmetic expressions, and the relational operators:

• Math symbols: < , , > , , = , • in C: < , <=, > , >= , == , !=

Page 6: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

6

Truth Tables for the Conditional Operators

p q p && q

T T T

T F F

F T F

F F F

p q p || q

T T T

T F T

F T T

F F F

Page 7: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

7

The ‘if’ statement

• Performs an action if a condition is true. The condition, which is a C expression, evaluates to zero (false) or nonzero (true).

• Syntax:if (conditional expression){

statement;}

condition

statement

True False

Page 8: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

8

The ‘if-else’ Statement

• Perform if-action if a condition is true. Otherwise, perform else-action.

• Syntax:if (conditional expression){

statement1

}else{

statement2

}

condition

statement1 statement1

True False

Page 9: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

9

The ‘else-if’ Statement

• You can connect conditional constructs to form longer sequences of conditional tests:

• Syntax: if(conditional expression1){

statement1}else if (expression2){ statement2}else if (expression3){ statement3}else{ statement4}

Page 10: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

10

Points to remember…

• An else is associated with the closest unassociated if.

if (expression1){ if (expression2) statement2 else statement3

}

if (expression1) { if (expression2) statement2 else statement3}

if (expression1) { if (expression2) statement2}else statement3

Correct Interpretation

Just as parentheses modify the order of evaluation of expressions...

braces modify how statements are executed.

Page 11: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

11

The ‘Switch’ Statement

• Some of the confusions in the Switch keywords:– Switch– Case– Break– Default

Page 12: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

12

The ‘switch’ Statement• Performs actions based on a series of tests of

the same variable.• Form: switch (conditional expression)

{ case const-expr: statements case const-expr: statements case const-expr: statements default: statements }

• The break statement causes an immediate exit from the switch.

• Because cases serve only as labels, execution falls through to the next unless there is explicit action to escape.

case 1

...;break;

True False

...;break;

True False

...;break;

True False

case 2

case 3

Page 13: Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

SACS Programming Fundamentals Workshop

13

Any Questions???