control structure c++
DESCRIPTION
slide control structureTRANSCRIPT
![Page 1: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/1.jpg)
BFC 20802CONTROL STRUCTURE
![Page 2: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/2.jpg)
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
![Page 3: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/3.jpg)
CONTROL STRUCTURE
Sequence Control
Structure
Selection Control
Structure
Repetition Control
Structure
![Page 4: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/4.jpg)
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.
![Page 5: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/5.jpg)
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".
![Page 6: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/6.jpg)
6
Selection Control Structure
![Page 7: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/7.jpg)
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.
![Page 8: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/8.jpg)
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)
![Page 9: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/9.jpg)
Single selection (if)
9
Use if statement.Used in a situation which
if conditional statement is TRUE, statement will be selected.
![Page 10: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/10.jpg)
grade ==‘A’ Print “Excellent”Yes
No
i) Single Selection
Convert to flowchart:
![Page 11: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/11.jpg)
if (conditional expression)C++ statement;
Reservedword
Consist of:Output statement/ Input statement/
expression
Single selection (if)FORMAT
![Page 12: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/12.jpg)
If grade is A, print a message ‘Excellent”
Convert to valid selection statement:
if ( grade = = ‘A’) cout << “Excellent” ;
Single selection (if)
Conditionalexpression
EXAMPLE
![Page 13: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/13.jpg)
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.
![Page 14: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/14.jpg)
Convert to flow chart:
grade = ‘A’ Print “Passed”Yes
No
Print “Failed”
ii) Double Selection
![Page 15: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/15.jpg)
Double selection (if-else)
if (conditional statement)C++ statement;
elseC++ statement;
FORMAT
![Page 16: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/16.jpg)
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
![Page 17: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/17.jpg)
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:
![Page 18: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/18.jpg)
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:
![Page 19: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/19.jpg)
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:
![Page 20: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/20.jpg)
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:
![Page 21: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/21.jpg)
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:
![Page 22: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/22.jpg)
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:
![Page 23: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/23.jpg)
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
![Page 24: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/24.jpg)
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
![Page 25: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/25.jpg)
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
![Page 26: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/26.jpg)
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
![Page 27: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/27.jpg)
{ 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";
}
![Page 28: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/28.jpg)
Write a C++ program which will accept five integer
numbers
How will you solve this?
Exercise
![Page 29: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/29.jpg)
{ 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;}
![Page 30: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/30.jpg)
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
![Page 31: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/31.jpg)
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
![Page 32: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/32.jpg)
While loopExample:
![Page 33: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/33.jpg)
nom = 10;While i < 20{ cout << ‘\t’<<bil; nom++;}
10111213141516171819
Example:
Do…while loop
![Page 34: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/34.jpg)
While loop
Example:
![Page 35: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/35.jpg)
Infinite While loopExample:
![Page 36: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/36.jpg)
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
![Page 37: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/37.jpg)
Example:
Do…while loop
![Page 38: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/38.jpg)
nom = 10;do{ cout << ‘\t’<<bil; nom--;}while (bil > 5);
10 9 8 7 6
Example:
Do…while loop
![Page 39: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/39.jpg)
Infinite Do… While loopExample:
![Page 40: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/40.jpg)
while do..while
Example:
![Page 41: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/41.jpg)
FOR LOOP
![Page 42: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/42.jpg)
• 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;}
![Page 43: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/43.jpg)
• 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.
![Page 44: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/44.jpg)
• 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.
![Page 45: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/45.jpg)
Conclusion
• Selection structurei. Single(if)ii. Double(else)iii. Multiple(else if)• Repeatation (loop)i. Whileii. While..dooiii. For..loop
![Page 46: control structure c++](https://reader030.vdocument.in/reader030/viewer/2022033016/577cb55c1a28aba7118d149c/html5/thumbnails/46.jpg)
Kari udang kari ketamEnak dimakan di tepi pantaiKalau korang masih tak fahamHarap kawan sebelah jangan dibantai..
Sekian..