Conditional Statements
Pseudocode and Flowcharting
BRANCHING / SELECTION
Branching / Selection Flow
§ Branching is the process of following one of two of
more alternate paths of computations.
Branching / Selection Flow – Single Alternative
§ Pseudocode § Flowchart
IF expression then
statements
ENDIF
no
x = 10?
§ 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
§ Given two (2) numbers, x and y. Make a pseudo-code and
draw a flowchart to determine the difference between x and
y. If x-y is negative, compute R=x+y; if x-y is zero, compute
R=2x+2y; and if x-y is positive, compute R=x*y. Print out the
values, x, y and R.
§ Pseudo code
START 1) START
INITIALIZE: z = 0 2) INITIALIZE z =0.
READ: x and y
COMPUTE: z = x-y 3) INPUT x and y from user.
CHECK: z 4) COMPUTE z = x-y.
IF z < 0 then 5) CHECK for z value:
COMPUTE: R = x + y a. IF z <0, COMPUTE R = x+y
ELSE IF z = 0 then b. IF z =0, COMPUTE R = 2x + 2y
COMPUTE: R = 2x + 2y
c. IF z >0, COMPUTE R= x * y
ELSE
COMPUTE: R = x * y 6) DISPLAY x, y and R.
ENDIF 7) END
PRINT: x, y and R
END
Modular – Branching
§ Pseudo code - Module 1-
- Main - START
START .
. .
. .
if expression then END
Module 1
else - Module 2-
Module2 START
end if .
. .
. .
. END
END
Modular – Branching
MODULE 1 MODULE 2
§ Flowchart
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.
§ Create algorithm using Modular flow without return value
§ Create algorithm using Modular flow with return value
Programming
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.
Operator Symbol Precedence
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
§ Two-Way Selection Statement
§ Computer decision as a conditional statement:
§ True: one or more action statements are done
§ False: different set of actions to be executed
§ Regardless of which set of action, program continues to the next
statement
Selection
§ Two-Way Selection Statement
if(expression)
True False
{
Conditional expression
statement/s;
}
else
{
True Action False Action
statement/s;
}
Selection
§ Two-Way Selection Statement
§ Syntax Rules:
§ Expression is enclosed with parentheses.
§ No semicolon after if…else.
§ Statements may have semicolon as required by their types.
§ Expression can have side effects.
§ Statement/s can have any statement (even another if… else of a
null statement)
Selection
§ Two-Way Selection Statement
§ Syntax Rules:
§ If only one statement, the { } can be omitted.
§ We can swap the position of true or false statement if we use the
complement of the original expression.
§ If the false action is null, the else part can be omitted.
Selection
§ Two-Way Selection Statement
§ Sample Problem: Make a flowchart of a program that will determine if
a number is negative or not.
Start
Yes
Read (num) num<0 Display(“The number is negative”)
No
Display(“The number is not negative”)
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;
Multiway Expression case value2: statement/s;
break;
case value3: statement/s;
value1 value2 value3 value4 break;
case value4: statement/s;
break;
Action 1 Action 2 Action 3 Action 4
default: statement/s;
break;
}
Note: The value of the expression must be integral (int or char)
Used to compare equality only
Selection
§ 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
§ The else-if Statement
§ else-if is used to make multiway decision for non-integral
values ( it can still be used for integral)
§ There is no such C construct as else-if in C.
§ else-if is a style of coding to simplify the multiple nested if-
else’s.
Selection
§ The else-if Statement
if(expression1) if(expression1)
statement1; statement1;
else
else if(expression2)
if(expression2)
statement2;
statement2;
else else if(statement3)
if(expression3) statement3;
statement3;
Selection
§ The else-if Statement
§ Example
char grade;
… Good Programming
if(score>=90) Practice:
grade = ‘A’; When checking a range using
else if(score >=80) greater than, start with the
grade = ‘B’; largest value.
else if(score >=70) When checking a range using
grade = ‘C’; less than, start with the lowest
value.
else
grade = ‘D’
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
§ Code positive statements whenever possible.
§ Code the normal/expected condition first.
§ Code the most probable conditions first.