+ All Categories
Home > Documents > 06 ES26 Lab - Selection Control Structure

06 ES26 Lab - Selection Control Structure

Date post: 16-Nov-2014
Category:
Upload: wilmarc
View: 1,482 times
Download: 0 times
Share this document with a friend
22
Introduction to Programming Introduction to Programming Selection Control Structure Selection Control Structure 6 6
Transcript
Page 1: 06 ES26 Lab - Selection Control Structure

Introduction to ProgrammingIntroduction to Programming

Selection Control StructureSelection Control Structure

66

Page 2: 06 ES26 Lab - Selection Control Structure

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

Page 3: 06 ES26 Lab - 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

Page 4: 06 ES26 Lab - Selection Control Structure

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;

Page 5: 06 ES26 Lab - Selection Control Structure

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

Page 6: 06 ES26 Lab - Selection Control Structure

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() {

. . .

}

Page 7: 06 ES26 Lab - Selection Control Structure

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

Page 8: 06 ES26 Lab - Selection Control Structure

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”);

Page 9: 06 ES26 Lab - Selection Control Structure

Introduction to ProgrammingIntroduction to Programming

If StatementIf Statement

number > 0true

false

It is a positive number

Next statement

Page 10: 06 ES26 Lab - Selection Control Structure

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

Page 11: 06 ES26 Lab - Selection Control Structure

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

Page 12: 06 ES26 Lab - Selection Control Structure

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

Page 13: 06 ES26 Lab - Selection Control Structure

Introduction to ProgrammingIntroduction to Programming

Logical ExpressionsLogical Expressions

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

AND False True

False False False

True False True

Page 14: 06 ES26 Lab - Selection Control Structure

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)

Page 15: 06 ES26 Lab - Selection Control Structure

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

Page 16: 06 ES26 Lab - Selection Control Structure

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))

Page 17: 06 ES26 Lab - Selection Control Structure

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)

Page 18: 06 ES26 Lab - Selection Control Structure

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)

Page 19: 06 ES26 Lab - Selection Control Structure

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.

Page 20: 06 ES26 Lab - Selection Control Structure

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”);

Page 21: 06 ES26 Lab - Selection Control Structure

Introduction to ProgrammingIntroduction to Programming

if-else Statementif-else Statement

number > 0

A positive number

truefalse

Not a positive number

Next statement

Page 22: 06 ES26 Lab - Selection Control Structure

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”);


Recommended