0% found this document useful (0 votes)
13 views8 pages

UNIT 3 Selection Structure C

The document discusses selection structures in programming, specifically focusing on IF-ELSE statements, relational operators, logical operators, and their syntax. It explains the use of one-way selection, nested IF-ELSE statements, and IF-ELSE chains, along with examples and flowcharts. Additionally, it includes activities for writing pseudocode and flowcharts based on specific programming tasks.

Uploaded by

lumiereleonore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

UNIT 3 Selection Structure C

The document discusses selection structures in programming, specifically focusing on IF-ELSE statements, relational operators, logical operators, and their syntax. It explains the use of one-way selection, nested IF-ELSE statements, and IF-ELSE chains, along with examples and flowcharts. Additionally, it includes activities for writing pseudocode and flowcharts based on specific programming tasks.

Uploaded by

lumiereleonore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT 3

SELECTION STRUCTURE
Also known as a conditional structure, a selection structure is a programming feature that performs
different processes based on whether a boolean condition is true or false. Selection structures use
relational operators to test conditions. There are different types of selection structures that can be
used to achieve different outcomes.

IF-ELSE STATEMENT
Syntax:
if (condition){
statement1;
}
else{
statement2;
}
statement;

 Implements a decision structure for two alternatives.

The condition is evaluated to its numerical value:

o a non-zero value is considered to be true


o a zero value is considered
 The else option is optional, it will execute only if the condition is false.

Relational operators

The relational operators are used to test the relation between two values. All relational operators
are binary operators and therefore require two operands. A relational expression returns zero when
the relation is false and a non-zero when it is true.

Relational Meaning Example


Operators
< Less than Age < 30

<= Less than or equal to Height > 6.2

== Equal to Taxable <= 20000

> Greater than Temp >= 98.6


>= Greater than or equal to Grade == 100

!= Not equal to Number != 250

 Relational expressions are evaluated to a numerical value to 1 or 0 only:


o If the value is 1, the expression is true.
o If the value is 0, the expression is false.
 char values are automatically coerced to int values for comparison purposes
 Strings are compared on a character-by-character basis
o The string with the first lower character is considered smaller
Examples of string comparisons

Expression Value Interpretation Comment

“Hello” > “Good-bye” 1 True The first H in Hello is greater than


the first G in Good-bye

“SMITH” > “JONES” 1 True The first S in Hello is greater than


the first J in Good-bye

“123” > “1227” 1 True The third character in 123, the 3, is


greater than the third character in
1227, the 2.

The third character in Behop, the h,


“Behop” > “Beehive” 1 True is greater than the third character in
Beehive, the second e.
Logical operators

The logical operators are used to combine one or more relational expression. The logical operators
are:
 OR : Condition is true if either one or both of the expression is true
 AND : Condition is true only if both expressions are true
 NOT : Changes an expression to its opposite state; true becomes false, false
becomes true

The IF-ELSE FLOWCHART

Common problem with if-else statements:


- Misunderstanding what an expression is
- Using the assignment operator (=) instead of the relational operator (==)
Example:
Design a flowchart that accepts one input which is a radius of a circle, the flowchart should
output the area of the circle if the input radius is greater than 0, else it will display “Invalid radius”.

Flowchart:

Pseudocode:
Start
Input radius
if radius>0:
area = radius*radius*3.1416
Output area
else:
Output “Invalid radius”
endif
Stop
ONE-WAY SELECTION
- An if statement without the else portion

General form of one way selection:


if (condition{

statement;;

next_statement
NESTED IF-ELSE STATEMENTS
- if-else statement can contain any valid python statement, including another if-else
- Nested if-else statement: an if-else statement completely contained within another if-
else
Nested within the if part:

General form of nested if else:


Syntax:
if (condition){

if (condition2){

statement1

else{

statement2

else{
default_statement

IF-ELSE CHAIN
- if-else chain: A nested if statement occurring in the else statement clause of the outer
if-else
- If any condition is true, the corresponding statement is executed and the chain
terminates
- Final else is only executed if no conditions were true
o serves as a catch-all case
- if-else chain provides one selection from any possible alternatives
Nested within the else part:
General form of an if-else chain:

Syntax:
if (condition1){

statement1

else if (condition2){

statement2

else if (conditionn){

statementn

else
default_statement

Activity 3. SELECTION STRUCTURE. WRITE YOUR ANSWER IN A4 SIZE BONDPAPER.

Instruction: Draw the flowchart and write the equivalent pseudocode.

1. Write a program that prompts the user to enter two numbers, the program then will
check and display whether the numbers are equal or not.
2. Write a program that prompts the user to enter a year, the program then will check
whether a given year is a leap year or not.
3. Write a program that prompts the user to enter one number, the program then will
check whether a given number is even or odd;
4. Write a program that accepts 3 numbers as input, the program then will display the
largest value of the 3.
5. Write a program that accepts the value of the 3 sides of a given valid triangle. The
program then will check and display if the triangle is an isosceles, scalene or
equilateral.

You might also like