1 / 19

Chapter 8 Control Structures

Chapter 8 Control Structures. Prepared by: Lec . Ghader R. Kurdi. 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:

sailor
Download Presentation

Chapter 8 Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8Control 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. Example #include <iostream.h> void main() { intvalue, 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 <iostream.h> 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 <iostream.h> void main() { cout<< "Enter two integers: "; intnum1, num2, largest; cin>>num1>>num2; if (num1>num2) largest = num1; if (num2>num1) largest = num2; cout<<"\nThelarger is: "<<largest; } Outputs:

  8. Example #include <iostream.h> void main() { intnum1, num2; cin>>num1>>num2; if (num1>num2) cout<<"\nThe largest number is: "<<num1; if (num2>num1) cout<<"\nThelargeest number is: "<<num2; } Outputs:

  9. Example #include <iostream.h> void main() { intnum; 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. Example #include <iostream.h> 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 <iostream.h> void main() { intnum ; 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 <iostream.h> void main() { intgrade; 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 <iostream.h> 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.

More Related