You are on page 1of 28

FP201: Programming Fundamentals/ Chapter 3

Chapter 3 program control


Click to edit Master subtitle style 3.1 Solve problems using selection control 4/14/12 structures 3.2 Loops control structures

4/14/12

LETS DO PRE-TEST !

4/14/12

3.1 SOLVE PROBLEMS USING SELECTION CONTROL STRUCTURES

At the end of this Learning Outcomes 3.1sub-chapter, students should be able to:

3.1.1 Explain the selection statement:

3.1 SOLVE PROBLEMS USING 4/14/12 SELECTION CONTROL STRUCTURES

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.

4/14/12 Syntax : If selection control

Single if if(<conditio n>) { <statements> ; }

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

/* Program to check whether the given number is less than 50 */


How many condition? Which are the condition?

Single if

cout << "Number is less than 50" << endl;

Program 2
#include <iostream> using namespace std; void main() {
int num; cin >> num;

4/14/12

/* Program to check whether the given number is less than 50 */

How many condition? Which are the condition?

Type of Selection control ? if (num < 50)

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

How many condition? Which are the condition?

4/14/12

Type of Selection control ?

Nested if

cout << 10 or bigger than 10" << endl; }

#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

How many condition? Which are the condition?

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

iii. Switch statement

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.

Syntax of Switch Statement


switch (Expression) { case exp_1: statement 1; break; case exp_2: statement 2; break;

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;

3.1 SOLVE PROBLEMS USING 4/14/12 SELECTION CONTROL STRUCTURES

At the end of this sub-chapter, students should be able to:

Convert the nested if statement to switch case


3.1.2

Convert the switch to ifelse 4/14/12 statement


IF #include <iostream.h>

SWITCH #include <iostream.h>

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;

3.1 SOLVE PROBLEMS USING 4/14/12 SELECTION CONTROL STRUCTURES

At the end of this sub-chapter, students should be able to:

3.1.2 Describe the function of break statement

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. }

In switch case statements, break causes

3.1 SOLVE PROBLEMS USING 4/14/12 SELECTION CONTROL STRUCTURES

At the end of this sub-chapter, students should be able to:


3.1.4 Solve a given problem by writing algorithm and program, run, test and debug using selection

4/14/12

Activity

Program Sample 1:if Statement


int num1, num2, min; cout<<Key-in 2 numbers: ; cin>>num1 >> num2; min = num1;

4/14/12

nu 20 m1 num 15 2 min 20

if (num1 > num2)


min = num2;

Key-in 2 numbers: min << endl; cout<<Smallest: << 20 15 Smallest: 15

Program Sample 2:if Statement


int mark; cout<<Mark: ; cin >> mark); mark if (mark > 80) { cout<<Category: Excellent\n; cout<<Congratulations!);

4/14/12

mar k

92

Mark: Cattegory: } Excellent Congratulations!

4/14/12

Test your skill

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

3.2 LOOPS AND CONTROL STRUCTURES

At the end of this Learning Outcomes 3.2sub-chapter,

students should be able to:

3.2.1 Identify the

4/14/12

You might also like