CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba
Control structure Normally, statements in a program are executed one after the other in the order in which they are written. This is called sequential execution. Various C++ statements will enable you to specify the next statement to be executed which might be other than the next one in sequence. This is called transfer of control. 2
Types of Control structure could be written in terms of only three control structures: Sequence structure Selection structure Repetition structure. 3
Types of Control Structures 4
Types of Control structure could be written in terms of only three control structures: Sequence structure Selection structure Repetition structure. 5
Selection structure C++ provides three types of selection structures in the form of statements: The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false. The if … else selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch selection statement performs one of many different actions depending on the value of an expression. 6
selection structure: if The if statement is called a single-selection statement because it selects or ignores a single action. Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. 7
selection structure: if -Example Write a program that reads a student’s grade and prints “passed” if the grade is greater than or equal 60. Flowchart: Code:... if ( grade >= 60 ) cout<<"Passed”;... 8 Note that, these statements Represents the selection part Of the code (i.e. not the whole Code)
selection structure: if … else The if…else statement is called a double- selection statement because it selects between two different actions. Syntax: if (expression) statement1 else statement2 9
selection structure: if … else -Example For example, pseudocode statement If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” Code: … if ( grade >= 60 ) cout<<"Passed"; else cout<<"Failed"; … 10 Flowchart
conditional Operator (? :) C++ provides the conditional operator (?:) which is closely related to the if…else statement. The conditional operator is C++’s only ternary operator i.e. it takes three operands. Syntax: expression1 ? expression2 : expression3 If expression1 = true, then the result of the condition is expression2. Otherwise, the result of the condition is expression3. 11
Conditional (? :) Operator- example1 12 Example: int x = 5, y =3, min ; if (x <= y) min = x ; else min = y ; The above statement can be written using the conditional operator as following: min = ( x <= y ? x : y);
Conditional (? :) Operator- example2 13 … if ( grade >= 60 ) cout<<"Passed"; else cout<<"Failed"; … The above statement can be written using the conditional operator as following: Cout = 60 ? “Passed” : “Failed”);
Compound (Block of) Statements 14 Syntax: { statement1 statement2. statementn }
Compound (Block of) Statements: Example 15 if ( grade >= 60 ) cout<<"Passed\n"; else { cout<<"Failed\n" ; cout<<“you have to take this course again\n"; } Java Programming: From Problem Analysis to Program Design, D.S. Malik
Multiple Selection: Nested if if (expression1) statement1 else if(expression2) statement2 else statement3 Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else syntax 16
Multiple Selection: Nested if (Example-1) Many C++ programmers prefer to write the preceding if statement as if ( grade >= 90 ) cout "A\n" ; else if ( grade >= 80 ) cout = 70 ) cout = 60 ) cout<<"D\n" ; else cout<<"F\n" ; 17
Multiple Selection: Nested if (Example-2) if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”; else cout<<“Good tennis day”; 18 Remember: else is associated with the most recent if that does not have an else
Multiple Selection: Nested if (Example-3) 19 if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”; Remember: else is associated with the most recent if that does not have an else
Multiple Selection: Nested if (Example-4) 20 //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) cout<<“Dean Honor list\n”; else cout<<“GPA below graduation requirement\n”; //This is the correct way of writing the above code if ( GPA >= 2.0 ) { if (GPA >= 3.9) cout<<“Dean Honor list”; } else cout<<“GPA below graduation requirement\n”; To force the nested if..else statement to execute as it was originally intended we must use the braces {} as following:
selection structure: Switch The switch statement is called a multiple- selection statement because it selects among many different actions. 21
selection structure: Switch 22 Expression: is also known as selector. Expression can be an identifier. Value can only be integral. Syntax: switch (expression) { case value1: statements1 break; case value2: statements2 break;... case valuen: statementsn break; default: statements }
Type of values in Switch Cases 23 In Switch statement each action is associated with a value of constant integral expression (i.e. any combination of character constants and integer constant that evaluates to a constant integer value) Thus, switch selector must be either : An variable of integer or char data type, OR An expression that evaluates to a constant integer value.
Switch With break Statements 24 switch (N) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;
Switch With no break Statements 25 switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true
Switch With break Statements 26 Example switch (grade) { case 'A': cout<<"The grade is A."; break; case 'B': cout<<"The grade is B."; break; case 'C': cout<<"The grade is C."; break; case 'D': cout<<"The grade is D."; break; case 'F': cout<<"The grade is F."; break; default: cout<<"The grade is invalid."; }
Previous Example - With Nested If 27 if (grade == 'A') printf("The grade is A."); else if (grade == 'B') printf("The grade is B."); else if (grade == 'C') printf("The grade is C."); else if (grade == 'D') printf("The grade is D."); else if (grade == 'F') printf("The grade is F."); else printf("The grade is invalid.");
Switch With break Statements 28 Example 2 switch (number1+number2) { case 0: cout<<"The sum is 0"; break; case 1: cout<<"The sum is 1"; break; case 2: cout<<"The sum is 2"; break; case 3: cout<<"The sum is 3"; break; case 4: cout<<"The sum is 4"; break; default: cout<<“ illegal expression"; }
Switch With break Statements 29 Example 3 switch (number%2) { case 0: cout<<number<<“is an even number”; break; case 1: cout<< number<<“is an odd number”; break; default: cout<<number<<"is invalid."; }
Exercise Write a program that reads the price and the production date ( month and year of an item ) then the program prints the price after applying the discount rate which equals to : 50% if it is produced before 30% if it is produced in 2011 but before the 4 th month. 10% if it is produced after 4,2011 and before 8,