SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Control Structures in C++: If and Switch Statements

sailor avatar
19 diapositives · 344 Views · Jul 27, 2014

This chapter explores conditional constructs in C++, focusing on control structures like if-statements and switch statements. It discusses the purpose and usage of these constructs to control the flow of program execution based on conditions. Examples illustrate the basic if structure, sorting integers, determining the largest number, and using if-else statements. Additionally, it covers nested if structures and the switch statement, providing code examples for practical understanding. Exercises challenge readers to apply their knowledge in real programming scenarios.

Understanding Control Structures in C++: If and Switch Statements - slide 1
01 / 19Understanding Control Structures in C++: If and Switch Statements
1 / 17

Télécharger la présentation

Understanding Control Structures in C++: If and Switch Statements
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.