fp 201 unit 3

44
Unit 3 Understand selection control structures

Upload: rohassanie

Post on 19-May-2015

431 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: FP 201 Unit 3

Unit 3 Understand selection control structures

Page 2: FP 201 Unit 3

At the end of this presentation, students will be able to:• Understand selection control structures• Describe the structure and working of simple if

statements• Describe the structure and working of nested if

statements• Describe the structure and working of switch

statements

Page 3: FP 201 Unit 3

Statements executed one by one. Simplest Example :

int x = 5; [S1]int power = x*x; [S2]cout << “\n” << power; [S3]

S3S2S1Entry

Exit

Page 4: FP 201 Unit 3

C++ supports two types of program control:• selection control structures• looping control structures

Page 5: FP 201 Unit 3

Purpose:• to evaluate expressions/condition • to direct the execution of the program (depending

on the result of the evaluation). The most commonly used selection

statements are: • if statement• if-else statement• nested-if statement• switch statement

Page 6: FP 201 Unit 3

Used to execute a set of statements when the given condition is satisfied.

Syntaxif (<condition>)

{

<Conditional statements>;

} Conditional statements within the block

are executed when the condition in the if statement is satisfied.

Page 7: FP 201 Unit 3

truecondition

Conditional statement

Next statement

false

Page 8: FP 201 Unit 3

Example:if (age > 21)

cout << “\n Anda layak mengundi”;

Page 9: FP 201 Unit 3

trueage > 21

Anda layak mengundi

Next statement

false

Page 10: FP 201 Unit 3

Program InputValue.cpp illustrates the execution of a simple if statement. The program checks whether the given number is greater than 100.

Page 11: FP 201 Unit 3

number<100false

true

start

Declare: number variable

Read number

Print result “Number is less

than 100”

end

Page 12: FP 201 Unit 3

#include <iostream> using namespace std;

int main() { int num; cout << "Enter integer number: "; cin >> num; if(num<100)

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

return 0; }

Page 13: FP 201 Unit 3
Page 14: FP 201 Unit 3

Executes the set of statements in if block, when the given condition is satisfied.

Executes the statements in the else block, when the condition is not satisfied.

Syntaxif (<condition>){

<Conditional statements1>;}else{

<Conditional statements2>;}

Page 15: FP 201 Unit 3

true

condition

Next statement

false

Conditional statement in else body

Conditional statement in if body

Page 16: FP 201 Unit 3

Example:if (E4162 == ‘L’)

cout << “\n Anda lulus”;else

cout << “\n Anda gagal”;

Page 17: FP 201 Unit 3

Next statement

falsetrue

Anda gagalAnda lulus

E4162 == ‘L’

Page 18: FP 201 Unit 3

Program Checks.cpp illustrates the use of the if-else statement. This program accepts a number, checks whether it is less than 0 and displays an appropriate message.

Page 19: FP 201 Unit 3

number<0falsetrue

start

Declare: number variable

Read number

end

Print “Positive”

Print “Negative”

Page 20: FP 201 Unit 3

#include <iostream> using namespace std;

int main() { int num; cout << "Enter integer number: "; cin >> num; if(num<0)

cout<<"Negative"<<endl; else

cout<<"Positive"<<endl;

return 0; }

Page 21: FP 201 Unit 3

1. Accept a number from the keyboard and check whether it is dividable by 5 (if else).Hint: Use the modulus operator, %, to check the divisibility.

Page 22: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int no;

cout<<"Enter integer number: ";cin>>no;

if(no%5==0)cout<<"dividable by 5"<<endl;

elsecout<<"undividable by 5"<<endl;

return 0;}

Page 23: FP 201 Unit 3

2. Accept two integer numbers from the keyboard and find the highest among them.

Page 24: FP 201 Unit 3

#include<iostream>using namespace std;

int main(){

int no1,no2;cout<<"Enter two integer number: ";cin>>no1>>no2;

if(no1>no2)cout<<"Number 1 is highest than number

2"<<endl;else

cout<<"Number 2 is highest than number 1"<<endl;

return 0;}

Page 25: FP 201 Unit 3

The if statements written within the body of another if statement to test multiple conditions is called nested if.

Syntax if (<Condition 1>){ if (<Condition 2>) {

<Conditional statements1>; } else

{ <Conditional statements2>; }

}else{ <Conditional statements3>;}

Inner if condition Outer if

condition

Page 26: FP 201 Unit 3

Conditional statements1

falsetrue

Condition 1

Condition 2

true

Conditional statements2

Next statement

false

Conditional statements3

Page 27: FP 201 Unit 3

The program Highest.cpp illustrates the use of nested if statements. The program accepts three integers from the user and finds the highest among the three.

Page 28: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int x,y,z;

cout<<"Enter x: ";cin>>x;cout<<"Enter y: ";cin>>y;cout<<"Enter z: ";cin>>z;

if(x>y){

if(x>z) cout<<x<<" is the highest"; else

cout<<z<<" is the highest"; }

else {

if (y>z)cout<<y<<" is the highest";elsecout<<z<<" is the highest";

}

return 0;}

Page 29: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int x,y,z;

cout<<"Enter x: ";cin>>x;cout<<"Enter y: ";cin>>y;cout<<"Enter z: ";cin>>z;

if(x>y&&x>z) cout<<x<<" is the highest";

else if (y>x&&y>z) cout<<y<<" is the highest";

else

cout<<z<<" is the highest";

return 0;}

Page 30: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int a,b,c;

cout<<"Enter a: ";cin>>a;cout<<"Enter b: ";cin>>b;cout<<"Enter c: ";cin>>c;

if(a>c&&b>c){if(a>b)

cout<<a<<" is the highest";else

cout<<b<<" is the highest";}

else{if(c>a&&c>b)

cout<<c<<" is the highest";

else cout<<b<<" is the highest";

}return 0;

}

Page 31: FP 201 Unit 3

#include<iostream>using namespace std;void main(){

int x=2;if(x<=3)

if(x!=0)cout << "Hello";

else cout<< "hello";

if(x>3)if(x!=0)

cout << "Hi";else

cout << "hi";}

Page 32: FP 201 Unit 3

Note that the first line does not end with a semicolon.

The curly brackets are necessary only if there are several statements.

Page 33: FP 201 Unit 3

Switch statement is C++'s multi-way branch

Allows to specify a number of different cases, rather than simply true or false

Switch statement requires an expression after the word switch and then it jumps to the statement whose case matches the expression

A break statement passes the control outside switch structure.

Page 34: FP 201 Unit 3

Syntaxswitch (expression) { case expression_1 :

statement sequence; break;

case expression_2 : statement sequence;

break; ………….. case expression_n :

statement sequence; break;

default : statement sequence;

}

Page 35: FP 201 Unit 3

expression_1

expression_2

statement sequence

statement sequence

break

break

default

break

Page 36: FP 201 Unit 3

Example:

int main(){char pilih;cout << “\n Menu Utama\n”;cout << “ M = Masukkan duit \n”;cout << “ K = Keluarkan duit\n”;cout << “ E = Exit\n”;cout << “ Pilihan anda: “;cin >> pilih;switch (pilih)

{case ‘M’ : cout << “Sila tambah duit anda”;break;case ‘K’ : cout << “Hanya boleh keluar

duit”;break;case ‘E’ : cout << “Keluar dari Menu

Utama”;break;default : cout << “Pilihan yang salah”;}

}

Page 37: FP 201 Unit 3

Program SwitchDemo.cpp illustrates switch case execution. In the program, the switch takes an integer value as input and displays the month based on the integer entered.

Page 38: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int month;

cout<<"Enter number: ";cin>>month;

switch (month) { case 1: cout<<"January";

break; case 2: cout<<"February"; break; case 3: cout<<"March"; break; case 4: cout<<"April"; break;

default: cout<<"wrong choice";}return 0;

}

Page 39: FP 201 Unit 3

1. Write a program to accept number of a day for the week and print the day

1 – Sunday 5 – Thursday2 – Monday 6 – Friday3 – Tuesday 7 – Saturday4 - Wednesday

Page 40: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

int day;

cout<<"Enter number: ";cin>>day;

switch (day) { case 1: cout<<"Sunday";

break; case 2: cout<<"Monday"; break; case 3: cout<<"Tuesday"; break; case 4:

cout<<"Wednesday"; break;

case 5: cout<<"Thursday";

break; case 6:

cout<<"Friday"; break; case 7:

cout<<"Saturday"; break;

default: cout<<"wrong choice";

}return 0;

}

Page 41: FP 201 Unit 3

Write a program that able to check either a character is a vowel or not by using switch statements and if else statement

Page 42: FP 201 Unit 3

#include <iostream>using namespace std;

int main(){

char ch;

cout<<"Enter character: ";cin>>ch;

switch (ch) { case 'a': case 'A': cout<<"Vowel";

break; case 'e': case 'E': cout<<"Vowel"; break; case 'i': case 'I': cout<<"Vowel"; break;

case 'o': case 'O': cout<<"Vowel";

break;case 'u': case 'U': cout<<"Vowel";

break;default: cout<<"Not vowel";

} return 0;}

Page 43: FP 201 Unit 3

#include<iostream>using namespace std;

void main(){

char ch;

cout<<"Enter character: ";cin>>ch;

if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')

cout<< ch << " is a vowel\n";else

cout<< ch << " is not a vowel\n";

}

Page 44: FP 201 Unit 3

In this presentation, you learnt the following: Program controls are used to change

the sequential flow of a program. The two types of program controls are

selection control structures and looping control structures

In C++, the selection control structures include if and switch statements.