control structure c++
description
Transcript of control structure c++
BFC 20802CONTROL STRUCTURE
OBJECTIVESAt the end of the chapter, student will know:• To explain control structures concept• To write a C++ program using control structures
AIMTo acknowledge student with the concept of control structures in programming: selection control structure and repetition control structure
CONTROL STRUCTURE
Sequence Control
Structure
Selection Control
Structure
Repetition Control
Structure
Selection Control Structure
4
Used in programming to:
offer choice of interests to user of a system.restrict/limit to only one operation/process
in a system to be executed at a timeallow user to choose only one selection of
process/operation at a time from a system.execute a process/operation based on
selection.
5
Selection Control Structure
Best example/case:
In general, the ATM processes are controlled by some restrictions or rules which is written using selection control structure (using particular programming language).
The concept is "one selection, one operation".
6
Selection Control Structure
7
Selection Control Structure
How decision is made when user make their selection from the ATM menu?The system will check the input and compares the selection with the conditions set in the system. If the input matches the setting conditions, the corresponding menu will be displayed.
8
Selection Control Structure
Choose only one instruction to be executed.Selection will be done if only the conditional
statement is TRUE.Types of selection control structure:
Single selection
(if)Double
selection (if-else)
Multi level selection (if-else-if)
Single selection (if)
9
Use if statement.Used in a situation which
if conditional statement is TRUE, statement will be selected.
grade ==‘A’ Print “Excellent”Yes
No
i) Single Selection
Convert to flowchart:
if (conditional expression)C++ statement;
Reservedword
Consist of:Output statement/ Input statement/
expression
Single selection (if)FORMAT
If grade is A, print a message ‘Excellent”
Convert to valid selection statement:
if ( grade = = ‘A’) cout << “Excellent” ;
Single selection (if)
Conditionalexpression
EXAMPLE
13
Double selection (if-else)Use if- else statement.Use in situation which provide 2
condition, but only 1 selection can be made.
If the condition is TRUE, then the first statement will be executed.
If not, the next selection will be executed.
Convert to flow chart:
grade = ‘A’ Print “Passed”Yes
No
Print “Failed”
ii) Double Selection
Double selection (if-else)
if (conditional statement)C++ statement;
elseC++ statement;
FORMAT
16
If grade == ‘A’, print a message “You passed ”Otherwise, print a message “ You failed”
Convert to a valid C++ selection statement:
if ( grade ==‘A’) cout << “Passed”;
else cout << “Failed”;
Double selection (if-else)EXAMPL
E
if (conditional expression is TRUE) C++ statement;else if (conditional expression is TRUE)
C++ statement;else if (conditional expression is TRUE)
C++ statement;else
C++ statement;
iii) Multi Level Selection Use if..else if statement Use in a situation which requires/provides two choices. If the conditional is TRUE (1), then selection
will be made. Format:
if (conditional expression is TRUE) C++ statement;else if (conditional expression is TRUE)
C++ statement;else if (conditional expression is TRUE)
C++ statement;else
C++ statement;
iii) Multi Level Selection Use if..else if statement Use in a situation which requires/provides two choices. If the conditional is TRUE (1), then selection
will be made. Format:
Write the selection control structure statements in C++ to produce the following output based on the input:
Input Output1 One2 Two3 Three4 Four5 Five
iii) Multi Level SelectionExample 1:
if (input == 1) cout << "=One\n";
else if (input == 2) cout << "\n=Two\n";
else if (input == 3) cout << "\n=Three\n";
else if (input == 4) cout << "\n=Four\n";
else if (input == 5) cout << "\n=Five\n";
else cout << "\nInvalid\n";cout << endl << endl;system("pause");return 0;
iii) Multi Level SelectionAnswer:
Write the C++ statements to determine student’s grade based on their marks as shown in the following table:
Mark Grade80-100 A60-79 B40-59 C0-39 D
iii) Multi Level SelectionExample 2:
if (mark >=80 && mark <=100) grade = ‘A’;else if (mark >=60 && mark <=79) grade = ‘B’;else if (mark >= 40 && mark <= 59) grade = ‘C’;else if (mark >= 0 && mark <= 39) grade = ‘D’;else
iii) Multi Level SelectionAnswer:
switch..case statement
switch( conditional expression/variable){ case label1: C++ statement; break; case label2: C++ statement;
break; … default: C++ statement;}
Only Integer/ character
Functioning likeelse
value Exit fromoption
• Use for multi level selection• Similar to if..else..if but use different
format• Format:
iii) Multi Level Selection
void main(){
int number;cout<<“Please insert a number = \n”;
cin>>number;
switch(number) { case 1 : cout << “One\n”; break; case 2 : cout << “Two\n”;
break; case 3 : cout << “Three\n”; break; default: cout << “Others”; } }
variable
Action
iii) Multi Level Selectionswitch..case statement
Example:
The value of
variable number
void main(){ int number; cin>>number;
switch(number) { case 1 : cout<<“One\n”; break; case 2 : cout<< “Two\n”;
break; case 3 : cout<< “Three\n”; break; default: cout<< “Others”; } }
void main(){ int number; cin>>number;
if (number == 1) cout<< “One\n”; else if (number == 2) cout<< “Two\n”; else if (number == 3) cout<< “Three\n”; else cout<< “Others”;}
iii) Multi Level Selectionswitch..case statement
if (colour == ‘r’) cout<<endl<<“red symbolise bravery”;else if(colour == ‘b’)
cout<< endl<< “blue symbolises unity”;else if(colour == ‘k’) cout<< endl<< “yellow is a royal colour”;else
cout<< endl<< “Error”;
Exercise
Convert the following if..else statement to switch..case statement
{ int r, b, k;char color;cout<<"Enter one color = ";
cin>>color;
switch(color) {
case 'r' : cout << "red symbolise bravery\n";
break; case 'b' : cout << "blue symbolise unity\n";
break; case 'k' : cout << "yellow is a royal colour\n"; break; default: cout << "Others";
}
Write a C++ program which will accept five integer
numbers
How will you solve this?
Exercise
{ int nom1, nom2, nom3, nom4, nom5;
cout<<"Enter one number = "; cin>>nom1; cout<<"Enter one number = "; cin>>nom2; cout<<"Enter one number = "; cin>>nom3;
cout<<"Enter one number = "; cin>>nom4; cout<<"Enter one number = "; cin>>nom5;cout << endl << endl;system("pause");return 0;}
Repitition Control StructureEnable statements to be repeated.While the value of conditional expression is TRUE, statements will be repeated.
Repetition will be terminated if the conditional expression is (FALSE).
Type of repetition/loop control structures:• While loop• Do...while loop
While loop• Use while statement for pre test loop. When the
condition becomes false, the while loop is exited.• Entry controlled loop because condition is
checked before the statements in the loop are executed.
• Statement will be repeated if the condition is TRUE.
Format :
counter = 0; while (conditional expression) { C++ statement; counter++; }
IMPORTANT!1. Counter2. Counter initialization 3. Conditional expression testing4. Increment/decrement of counter
Repeated statements
While loopExample:
nom = 10;While i < 20{ cout << ‘\t’<<bil; nom++;}
10111213141516171819
Example:
Do…while loop
While loop
Example:
Infinite While loopExample:
Do…while loopUse do..while statement for post test loop. The condition checked at the end of loop.
Repetition will occurred if condition is TRUE otherwise the loop is exited if the condition is FALSE.
Similar to while, must include 3 important things
Format :
counter = 0; do { C++ statement; counter++; } while (x < 5);Repeated statements
Example:
Do…while loop
nom = 10;do{ cout << ‘\t’<<bil; nom--;}while (bil > 5);
10 9 8 7 6
Example:
Do…while loop
Infinite Do… While loopExample:
while do..while
Example:
FOR LOOP
• The statements in the for loop repeat continuously for aspecific number of times. The while and do-while loops repeat until a certain condition is met.
• The for loop repeats until a specific count is met. • Use a for loop when the number of repetition is know, or
can be supplied by the user. • The coding format is: for(startExpression;
testExpression; countExpression){ block of code;}
• The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in the startExpression (such as int x = 1;). This startExpression is evaluated only once at the beginning of the loop.
• The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of the loop repeats. When the testExpression becomes FALSE, the looping stops and the program continues with the statement immediately following the for loop body in the program code.
• The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value.
• Braces are not required if the body of the for loop consists of only ONE statement. Please indent the body of the loop for readability
• CAREFUL: When a for loop terminates, the value stored in the computer's memory under the looping variable will be "beyond" the testExpression in the loop. It must be sufficiently large (or small) to cause a false condition. Consider:
for (x = 0; x <= 13; x++) cout<<"Melody";
When this loop is finished, the value 14 is
stored in x.
Conclusion
• Selection structurei. Single(if)ii. Double(else)iii. Multiple(else if)• Repeatation (loop)i. Whileii. While..dooiii. For..loop
Kari udang kari ketamEnak dimakan di tepi pantaiKalau korang masih tak fahamHarap kawan sebelah jangan dibantai..
Sekian..