190 likes | 330 Views
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:
E N D
Chapter 8Control 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: • If statement (if statement, if-else statement and Nested if-else structure) • Switch statement.
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:
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:
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:
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:
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:
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:
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
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. }
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:
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:
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 }
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:
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.