Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

CHAPTER 3

SELECTION CONTROL
STRUCTURE
Prepared by: Siti Hasrinafasya Che Hassan
Reference to: D.S. Malik
Overview

• Conditional for Selection Structure


• Selection Structure
• If selection
• If … else selection
• Nested if (multiple selection)
• Logical expression
• Strcmp
• switch...case statement
Introduction to Control Structures

• A computer can proceed:


➢ Sequentially
➢ Selectively (branch) - making a choice
➢ Repetitively (iteratively) - looping
• Some statements are executed only if certain conditions are met
• A condition is met if it evaluates to True.
Conditional for Selection Structure

• Condition
➢ To perform a test which compares two values
• Comparisons
➢ Needs a relational operators
Relational Description • Eg:
Operators – 8 < 15 evaluates to True
> Greater than – 6 != 6 evaluates to False
>= Greater than or equal – 2.5 > 5.8 evaluates to False
< Less than – 5.9 <= 7.5 evaluates to True

<= Less than or equal


== Equal
!= No equal
Selection Structure

• Is used to choose among alternative courses of action


• Statement are executed based on the condition selected
• Condition is formed by Boolean expression (TRUE or FALSE)
• Three types of selection structure :-
1. If selection
2. If..else selection
3. Multiway selection
If Selection
if (num >= 0) Example

cout << “Positive number” << endl;


cout << “End of program…”;

• If selection performs an action if a condition is true or skips the


action if false
• Syntax: True
if (condition) Condition

statement block; Instruction1


False

Instruction2
where:
➢ condition: a logical test enclosed in simple parenthesis.
➢ statement block: either a single statement or a group of
statements enclosed between the braces { }
If Selection (Examples)
Example 1

Example 2

Example 3
If… else Selection
if (num >= 0) Example
cout << “Positive number” << endl;
else if(num < 0)
cout << “negative number” << endl;
• Performs an action from two alternatives cout << “End of program…” <<;
• Syntax:
if (condition) True False
Condition
statement block1;
else
Instruction1 Instruction2
statement block2;

where: Instruction3

➢ condition: a logical test enclosed in simple parenthesis.


➢ statement block1: either a single statement or a group of statements
enclosed between the braces { } of condition is TRUE
➢ statement block2: either a single statement or a group of statements
enclosed between the braces {} if condition is FALSE
If… else Selection (Examples)
Example 1

Example 2
Multiple Selection
• Executes multiple if statements if the condition is true
• Syntax:
if (condition1)
statement block1; True False
Condition1
else if (condition2)
statement block2;
Instruction1 True False
: Condition2
else
statement blockN; Instruction2 True
ConditionN
if (num > 0) Example
cout << “Positive number” << endl; InstructionN :
else if (num < 0)
cout << “negative number” << endl;
else
cout << “Zero number” << endl;
cout << “End of program…” <<;
Multiple Selection (Examples)
Example 1

Example 2 To avoid excessive identation, the above code also can be rewritten as follows:
Multiple Selection (Examples)

Example 3
Comparing if… else Statements with a
Series of if Statements(Example)

Example 4
Logical Expression

• Selection structure also enable you to combine two or more


conditions (logical expressions) by using logical operators:
Logical Operators Description
Binary && AND – all conditions must be True
Binary || OR – at least one condition is True
Unary ! NOT – invert condition

Example (NOT expression)


Logical Expression
Example (AND expression)

Example (OR expression)


How to Compare String (strcmp)

• Strcmp used to compare the string S1 to the string S2. This


function will return a value of 0, less than 0 or greater than 0.
• Function strcmp compares first string argument with the second
string argument character by character.
• Syntax :-
strcmp (S1, S2) == 0 → means that string S1 and S2 are the same
strcmp (S1, S2) > 0 → means that string S1 is greater than string S2
strcmp (S1, S2) < 0 → means that string S1 is less than string S2
switch…case Statement
• Syntax :-
switch (expression )
{
case value1 :
statement1;
break;
case value 2:
statement2;
break;
:
case value N:
statementN;
break;
default : // Process for all other cases
statementN;
}

Remember: The value must be in CHARACTER or INTEGER data type


switch…case Statement
Example 1 Example 2

char drinkCode; int num1 = 15, num2 = 3, result,


cout << “Choose your drink code: "; operation;
cin >> drinkCode; cout << “Enter the operation code: ";
switch(drinkCode) { cin >> operation;
case ‘A’: switch(operation) {
case ‘a’: cout << "Apple"; case 1: result = num1 + num2;
break; break;
case ‘B’: case 2: result = num1 - num2;
case ‘b’: cout << “Banana"; break;
break; case 3: result = num1 * num2;
case ‘C’: break;
case ‘c’: cout << “Carrot"; case 4: result = num1 / num2;
break; break;
default : cout << “Invalid"; default: cout << "Invalid input...\n";
} result = 0;
}
cout << "Answer: " << result;
ANY QUESTION…?

You might also like