06 es26 lab - selection control structure

Post on 16-Nov-2014

1.485 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to ProgrammingIntroduction to Programming

Selection Control StructureSelection Control Structure

66

Introduction to ProgrammingIntroduction to Programming

ObjectivesObjectives

After completing this lesson, you should be able to do the following:

• Understand how a selection control structure works

• Learn how to use the if-else selection control structure

Introduction to ProgrammingIntroduction to Programming

Program Control StructureProgram Control Structure

Control Flow – the order in which statements are executed in a program

3 Kinds of Control Structures• Sequence• Selection

• Repetition/Iteration

Introduction to ProgrammingIntroduction to Programming

Sequence Control StructureSequence Control Structure

• Refers mainly to the sequential execution of statements

• A statement specifies the type of operation to be done such as the declaration statement

int num;

Introduction to ProgrammingIntroduction to Programming

sum = x + y;

ave = sum / 2;

printf(“%d”,ave);

Example: Average of Two NumbersExample: Average of Two Numbers

sum = x + y

ave = sum / 2

Display value of ave

Code

Flowchart

Introduction to ProgrammingIntroduction to Programming

Compound StatementCompound Statement

• A compound statement is a group of statements enclosed by { and } that are executed sequentially

• For example

main() {

. . .

}

Introduction to ProgrammingIntroduction to Programming

Selection Control StructureSelection Control Structure

• Chooses among alternatives statements• Uses a condition to select a course of action• A condition is either true or false

3 Types of Selection Control Structure

1. if

2. if-else

3. switch

Introduction to ProgrammingIntroduction to Programming

If StatementIf Statement• Format:

if (expression) stmt;

• Example: A program that determines whether a given number is positive or not

If (number > 0) printf(“It is a positive number”);

Introduction to ProgrammingIntroduction to Programming

If StatementIf Statement

number > 0true

false

It is a positive number

Next statement

Introduction to ProgrammingIntroduction to Programming

ConditionsConditions

• Usually, the condition in an if statement is either a

1. Relational expression

2. Equality expression

3. Logical expression

Introduction to ProgrammingIntroduction to Programming

Relational ExpressionsRelational Expressions

• Relational operators:

< > <= >=• Examples: Given the declaration

int a = 5, b = 6;

if (a < b)

if (a < 3)

if (b >= 6)

true

false

true

Introduction to ProgrammingIntroduction to Programming

Equality expressionsEquality expressions

• Equality Operators

== (equal)

!= (not equal)

• Example: Given the declaration

int i = 1, j = 2;

if (i == 1)

if (i == (j+1))

if ((i+2) != j)

true

false

true

Introduction to ProgrammingIntroduction to Programming

Logical ExpressionsLogical Expressions

• Logical operator: Binary AND && • Truth table of &&

AND False True

False False False

True False True

Introduction to ProgrammingIntroduction to Programming

Logical Expressions (AND)Logical Expressions (AND)

• Examples: Given the declaration

int i = 1, j = 2, k = 3;

if ( (i < j) && ( j < k )) if ( (i && j) && k) if ( i && j)

Introduction to ProgrammingIntroduction to Programming

Logical ExpressionsLogical Expressions

• Logical operator: Unary ! (not)• Truth table of NOT

Value of expr !expr

Zero True

Non-zero False

Introduction to ProgrammingIntroduction to Programming

Logical Expressions (NOT)Logical Expressions (NOT)

• Examples: Given the declaration

int x = 7, y = 7;

if ( !x )

if ( !(x – y))

if ( !!x )

if( !(! (x + y))

Introduction to ProgrammingIntroduction to Programming

• Logical operator: Binary || (or)• Truth table

OR False True

False False True

True True True

Logical Expressions (OR)Logical Expressions (OR)

Introduction to ProgrammingIntroduction to Programming

Logical Expressions (OR)Logical Expressions (OR)

• Examples: Given the declaration

int i =1, j =2, k = 3;

if ((i<j) || (i<k))

if((i==1)||((j>1)&&(k==3)))

if (i||j)

Introduction to ProgrammingIntroduction to Programming

If-Else StatementIf-Else Statement

Syntax:

if (condition)

statement1;

else

statement2;

Semantic: If condition is true, then statement 1 is executed and statement 2 is skipped;

if condition is false, then statement 1 is skipped and statement 2 is executed.

Introduction to ProgrammingIntroduction to Programming

Example: If-Else StatementExample: If-Else Statement

if (number > 0)

printf(“A positive number. \n”);

else

printf(“Not a positive number.\n”);

Introduction to ProgrammingIntroduction to Programming

if-else Statementif-else Statement

number > 0

A positive number

truefalse

Not a positive number

Next statement

Introduction to ProgrammingIntroduction to Programming

Nested If StatementsNested If Statements

if( x > 0)printf(“x is positive”);

else{

}

if(x < 0)printf(“x is negative”);

elseprintf(“x is zero”);

top related