Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.

Similar presentations


Presentation on theme: "CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi."— Presentation transcript:

1 CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi

2 Conditional Constructs They provide the ability to control whether a list of statements is executed. Besides, it is a mechanism for deciding whether an action should be taken. It comes in one of the two types: If statement (if statement, if-else statement and Nested if-else structure) Switch statement.

3 The Basic If Structure

4 Example

5 #include void main() { int value, temp; cout<<"Enter an integer number: "; cin>>value; temp=value; If (value<0) value = - value ; cout<<"the absolute value of "<<temp<<“ is "<<value; } Outputs:

6 Example: Sorting Two Numbers #include void main() { cout << "Enter two integers: "; int value1, value2; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: “; cout << value1 << " " << value2 << endl; } Outputs:

7 Example #include void main() { cout << "Enter two integers: "; int num1, num2, largest; cin>>num1>>num2; if (num1>num2) largest = num1; if (num2>num1) largest = num2; cout<<"\nThe larger is: "<<largest; } Outputs:

8 Example #include void main() { int num1, num2; cin>>num1>>num2; if (num1>num2) cout<<"\nThe largest number is: "<<num1; if (num2>num1) cout<<"\nThe largeest number is: "<<num2; } Outputs:

9 Example #include void main() { int num; cout<<"Enter an integer number: "; cin>>num; if(num>0) cout<<num<<" is positive”<< endl; if(num<0) cout<<num<<" is negative"<< endl; if(num==0) cout<<num<<" is zero"<< endl; } Outputs:

10 The If-Else Structure

11 Example

12 #include void main() { cout << "Enter two integers: "; int value1, value2, max; cin >> value1 >> value2; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl; } Outputs:

13 Selection It is often the case that depending upon the value of an expression we want to perform a particular action. There are two major ways of accomplishing this choice: –Nested if structure if-else statements "glued" together –Switch statement An advanced construct

14 Nested if syntax if(boolean_expression1) { // Executes when the boolean expression 1 is true } else if( boolean_expression2) { // Executes when the boolean expression 2 is true } else { // executes when the none of the above condition is true. }

15 Example #include void main() { int num ; cout<<"Enter an integer number: "; cin>> num; if (num < 0 ) cout << num << " is negative" << endl; else if (num > 0 ) cout << num << " is positive" << endl; else cout << num << " is zero" << endl; } Outputs:

16 Example #include void main() { int grade; cin>>grade; if (grade>=90) cout<<"A"; elseif (grade>=80) cout<<"B"; elseif (grade>=70) cout<<"C"; elseif (grade>=60) cout<<"D"; else cout<<"F“; } Outputs:

17 Switch statement syntax switch ( variable ) { case value1 : statement;// Process value1 break; case value2 : statement;// Process for value2 break; default : statement;// Process for all other cases }

18 Example #include void main() { char grade; cin>>grade; switch (grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : cout << "Well done" << endl; break; case 'C' : cout << “Not bad" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } Outputs :

19 Exercises Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". Write a C++ program that receives the current temperature as input. If the temperature is 35 degrees or more, output a message telling the user to “go swimming”, otherwise, if the temperature is 25 degrees or more, output a message to “go running”, otherwise “stay inside”. Read an integer value. Assume it is the number of a month of the year; print out the name of that month.


Download ppt "CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi."

Similar presentations


Ads by Google