06-Conditional Statements
06-Conditional Statements
BRANCHING / SELECTION
Branching / Selection Flow
§ Example yes
IF x = 10 then
PRINT:
PRINT: “ten” “ten”
ENDIF
Branching / Selection Flow – Double Alternative
§ Pseudocode § Flowchart
IF expression then
statements A
ELSE
statements B x = 10?
no
ENDIF
yes
§ Example
PRINT: PRINT:
IF x =10 then “ten” “x is not ten”
PRINT: “ten”
ELSE
PRINT: “x is not ten”
ENDIF
Branching / Selection Flow – Multiple Alternative
§ Pseudocode § Example
IF expression-1 then IF x =1 then
statements-1 PRINT: “one”
ELSE if expression-2 then ELSE IF x =2 then
statements-2 PRINT: “two”
. ELSE IF x=3 then
. PRINT: “three”
ELSE IF expression-n then ELSE
statements-n PRINT: “x is not 1, 2 & 3”
ENDIF ENDIF
Branching / Selection Flow – Multiple Alternative
§ Flowchart x = 1?
yes PRINT:
“one”
no
yes PRINT:
x = 2?
“two”
no
yes PRINT:
x = 3?
“three”
no
PRINT:
“x is not 1, 2 & 3”
Example for Branching
no
if expression?
yes
Module 1 Module 2
return return
Example for Modular
§ Create an algorithm that will have the option to do addition
or subtraction of two numbers.
BRANCHING / SELECTION
Selection
§ Selection Statements
§ The world is filled with choices.
§ Computer programs must reflect the world.
§ Selection statements allow the computer to make
decisions.
§ Computer decision is based on either true or false.
Selection
§ Logical Data
§ Logical data conveys true or false.
§ Real Life : Yes or No
§ Computer: True or False
§ C has no logical data type
§ False – zero
§ True – nonzero
Selection
§ Logical Operators
§ C has 3 logical operators.
NOT ! 15
AND && 5
OR || 4
Selection
§ Logical Operators
§ NOT Operator
x !x
zero 1
nonzero 0
x=5; y=0;
printf(“%d”, !x); printf(“%d”, !y);
Selection
§ Logical Operators
§ AND Operator
x y x && y
zero zero 0
zero nonzero 0
nonzero zero 0
nonzero nonzero 1
Selection
§ Logical Operators
§ OR Operator
x y x || y
zero zero 0
zero nonzero 1
nonzero zero 1
nonzero nonzero 1
Selection
§ Logical Operators
§ AND short-circuit method
§ false && (any expression)
§ OR short-circuit method
§ true || (any expression)
Selection
§ Relational Operators
§ Relational operators are binary that accept 2 operands
<
<= Precedence
> 10
>=
== Precedence
!= 9
Selection
if(expression)
True False
{
Conditional expression
statement/s;
}
else
{
True Action False Action
statement/s;
}
Selection
Yes
No
End
Selection
§ Two-Way Selection Statement
1 /* Filename: Selection.c
Program Description: Example of if-else */
2 #include<stdio.h> 13
3 14 if( num<0)
4 int main(void) 15 printf(“%d is negative”,num);
5 { 16 else
6 int num; 17 printf(“%d is non-negative”, num);
7 18
8 printf(“Enter an integer:”); 19 getch();
9 20
10 scanf(“%d”,&num); 21 }
11 22
12 23
Selection
§ Two-Way Selection Statement
1 /* Filename: Selection.c
Program Description: Example of if-else with function */
2 #include<stdio.h> 17
3 int CheckNegative(int num); 18 int CheckNegative(int number)
4 19 {
5 int main(void) 20 if(number<0)
6 { int num, result; 21 return 1;
7 22
8 printf(“Enter an integer:”); 23 else
9 scanf(“%d”,&num); 24 return 0;
10 result = CheckNegative(num); 25 }
11 if( result == 1) 26
12 printf(“%d is negative”,num); 27
13 else 28
14 printf(“%d is nonnegative”, num); 29
15 getch(); 30
16 } 31
Selection
§ Nested if-else Statement
True False
expression1 if(expression1)
if(expression2)
False True
statement2;
expression2 else
statement1;
Statement3 else
Statement1 Statement2
statement3;
end
Selection
§ Nested if-else Statement
1 /* Filename: NestedIf.c
Program Description: Determine if the number is +, - or zero */
2 #include<stdio.h> 13
3 14 if( num<0)
4 int main(void) 15 printf(“%d is negative”, num);
5 { 16 else
6 int num; 17 { if( num> 0 )
7 18 printf(“%d is positive”, num);
8 printf(“Enter an integer:”); 19 else
9 20 printf(“%d is zero”, num);
10 scanf(“%d”,&num); 21 }
11 22 getch();
12 23 }
Selection
§ Nested if-else Statement
§ Dangling else is encountered if there is no matching else for every if.
§ Rule: Always pair an else to the most recent unpaired if in the current
block.
Example
To correct:
if (expression1) if (expression1)
if (expression2) {
statement 1 if (expression 2)
statement 1
else }
statement 2 else
statement 2
Selection
§ Simplifying if-else Statement
§ Example:
Simplified:
if(a!=0)
if(a)
printf(“Hello”);
printf(“Hello”);
Simplified:
if(a==0)
if(!a)
printf(“goodbye”);
printf(“goodbye”);
Selection
§ Ternary Conditional Expression
§ Alternative to 2-way selection
§ Contains 3 operands with each operand as expression
§ Syntax: expression1 ? statement1 : statement2
§ If expression 1 is true, statement1 is executed otherwise statement2 is executed.
§ Example:
a=1;
b=2;
c= 3;
a==b?c++:c--;
Selection
§ Multiway Selection
§ Multiway selection is used to choose among several alternatives.
§ 2 Implementations of Multiway
§ switch statement - integral comparison only
§ else-if (nested if’s) - both integral and non-integral comparison
Selection
switch(expression)
§ Switch Statement
{
case value1: statement/s;
break;
§ Switch Statement
§ The expression that follows after the keyword
switch must be of integral type.
§ The expression followed by each case label
must be a constant expression.
§ No two case labels can have the same value.
§ Two case labels can be associated with the
same statement/s.
§ The default label is optional.
§ Used to test for equality only.
Selection
§ Switch Statement
§ Example:
switch(day)
{
case 0: printf(“Sunday”); case 4: printf(“Thursday”);
break; break;
case 1: printf(“Monday”); case 5: printf(“Friday”);
break; break;
case 2: printf(“Tuesday”); case 6: printf(“Saturday”);
break; break;
case 3: printf(“Wednesday”); default: printf(“Not a valid day”);
break; break;
}
Selection
§ De Morgan’s Theorem
§ Logical expression can be described in positive or negative way.
§ Negative expression is sometimes difficult to understand.
§ De Morgan’s Theorem can be used to convert negative expression to
positive expression without affecting the result.
Selection
§ De Morgan’s Theorem
§ Steps
Example:
1. Complement the !(!x && !y)
whole expression.
1. !!(!x && !y)= !x && !y
2. Complement each
component of the 2. !!x && !!y = x && y
expression. 3. x || y
3. Change && to || or
|| to &&.
Selection
§ De Morgan’s Theorem
§ De Morgan’s also applies to relational operators
Original Expression Simplified Expression
!( x< y) x >= y
!(x>y) x<=y
!(x!=y) x==y
!(x<=y) x>y
!(x>=y) x<y
!(x==y) x!=y
Rules for Selection Statements