Selection Control Structure
6
Introduction to Programming
Objectives 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 Programming
Program 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 Programming
Sequence 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 Programming
Example: Average of Two Numbers Flowchart Code sum = x + y
sum = x + y; ave = sum / 2; printf(“%d”,ave);
ave = sum / 2
Display value of ave
Introduction to Programming
Compound Statement • A compound statement is a group of statements enclosed by { and } that are executed sequentially • For example main() { . . . } Introduction to Programming
Selection 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 Programming
If 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 Programming
If Statement
true number > 0
It is a positive number
false Next statement
Introduction to Programming
Conditions • Usually, the condition in an if statement is either a 1. Relational expression 2. Equality expression 3. Logical expression
Introduction to Programming
Relational Expressions • •
Relational operators: < > <= >= Examples: Given the declaration int a = 5, b = 6; if (a < b) true if (a < 3) false if (b >= 6) true Introduction to Programming
Equality 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) Introduction to Programming
true false true
Logical Expressions • Logical operator: Binary AND && • Truth table of && AND
False
True
False
False
False
True
False
True
Introduction to Programming
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 Programming
Logical Expressions • Logical operator: Unary ! (not) • Truth table of NOT Value of expr
!expr
Zero
True
Non-zero
False
Introduction to Programming
Logical Expressions (NOT) • Examples: Given the declaration int x = 7, y = 7; if ( !x ) if ( !(x – y)) if ( !!x ) if( !(! (x + y))
Introduction to Programming
Logical Expressions (OR) • Logical operator: Binary || (or) • Truth table OR
False
True
False
False
True
True
True
True
Introduction to Programming
Logical Expressions (OR) • Examples: Given the declaration int i =1, j =2, k = 3; if ((i<j) || (i1)&&(k==3))) if (i||j)
Introduction to Programming
If-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 Programming
Example: If-Else Statement
if (number > 0) printf(“A positive number. \n”); else printf(“Not a positive number.\n”);
Introduction to Programming
if-else Statement
false
number > 0
Not a positive number
true
A positive number
Next statement
Introduction to Programming
Nested If Statements if( x > 0) printf(“x is positive”); else{ if(x < 0) printf(“x is negative”); else printf(“x is zero”); } Introduction to Programming