Professional Documents
Culture Documents
4/14/12
LETS DO PRE-TEST !
4/14/12
At the end of this Learning Outcomes 3.1sub-chapter, students should be able to:
At the end of this sub-chapter, students should be able to: Explain the selection statement:
3.1.1
4/14/12
SELECTION CONTROL ?
It allows instructions to be executed in a nonsequential ways. The user can choose what he want to process. It compares two expressions. Based on the comparison, it takes a certain course of action.
4/14/12
4/14/12
Pairing Activity 1
Comparing & identify a variety type of selection control
Instructions:
1. 2.
You are given a few program sample. Identify the type of selection control for each program code Your answer will be discuss for the next lecture.
3.
4/14/12
Single if
Used to execute a set of statements when the given condition is satisfied. Conditional statements within the block are executed when the condition in the if statement is satisfied.
If else
Nested if
The if statements written Executes the set of statements in if block, within the body of another when the given condition is if statement to test satisfied. multiple conditions is Executes the statements in called nested if. the else block, when the condition is not satisfied.
If else
if (<condition>) { < statements1>; } else { <statements2>; }
Nested if
if (<Condition 1>) { if(<Condition 2>) { <Statement 1>; } else { <Statement 2>; } } else { <Statement 3>; }
Inne r if Oute r if
Program 1
#include <iostream> using namespace std; void main() {
int num; cin >> num; if (num Selection control ? Type of < 50)
4/14/12
Single if
Program 2
#include <iostream> using namespace std; void main() {
int num; cin >> num;
4/14/12
If else
Program 3
#include <iostream> using namespace std; void main() { int num; cout << "Enter 1 number = "; cin >> num; if (num > 0) { if (num < 10) cout << Less than 10" << endl; else
4/14/12
Nested if
#include <iostream> Type of using namespace std; Selection void main() control ? { int num; cout << "Enter 1 number = "; cin >> num; If else if (num < 0) cout << "Negative number" << endl; else if (num > 0) cout << "Positive number" << endl; else cout << "Zero number" << endl; }
Program 4
4/14/12
4/14/12
Pairing Activity 2
Develop flow chart and find the output
Instructions: You are choose 2 program sample (from Activity 1). Develop flow chart Give possible input and output for the program
1.
2. 3.
4/14/12
Is a multi-way selection statement. Contains various case statements. The case statements are executed based on the value of the expression. A break statement passes the control outside switch structure.
4/14/12
Sample Program
#include <iostream.h> void main() {
int Month;
4/14/12
cout << "Enter 1 number between 1 to 3 : "; cin >> Month; switch (Month) {
case 1: cout << "January" << endl;
void main() { int Month; cout<<"Enter 1 number between 1 to3:"; cin >> Month;
void main() { int Month; cout<<"Enter 1 number between 1 to 3:"; cin >> Month;
4/14/12
Break is used within loops and switch statements to jump to the end of the code block.
loop switch( variable ) { { break; case value: //code.. /* code */ . break; case value: It causes the "//code..." /* code */ above to be skipped and } break; terminates the loop. }
4/14/12
Activity
4/14/12
nu 20 m1 num 15 2 min 20
4/14/12
mar k
92
4/14/12
1. Write a program to input at least 2 data from user. Process the data and display the output. Example output Total Buying: RM 80 Total Pay : RM 100
4/14/12
4/14/12