1 / 40

Lecture-7

Instructor Name: Muhammad Safyan. Lecture-7. Lecture Outline. Nested if Statement (More Example) Switch Statement Repititive structure (while Loop). 2.

pcrawford
Télécharger la présentation

Lecture-7

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. Instructor Name: Muhammad Safyan Lecture-7

  2. Lecture Outline Nested if Statement (More Example) Switch Statement Repititive structure (while Loop) 2

  3. Write a Program that prompt the user to enter the vlaue for variable a, b and c. Code the program based on condition written in left column of the table and print corresponding value written in right column of the table (Only use Nested if Condition.

  4. What will be the output of the code If the value of a=7 b=10 c=11 • if(a>b) • { • if(a>c) • { • if(b>c) • { • cout<< "White"; • } • else • { • cout<<"Yellow"; • } • } • else • { • cout<<"Green"; • } • } • else • { if(a>c) { cout<<"Pink"; } else { if(b>c) { cout<<"Red"; } else { cout<<"Black"; } } }

  5. IF Condition (multiple scenario) main() { intmagic;   intguess;      magic=1234;                           // get a random number cout << "Enter your guess: "; cin >> guess; if (guess == magic) { cout << "** Right **\n"; cout << magic << " is the magic number.\n";   } else { cout << "...Sorry, you're wrong."; if(guess > magic) cout <<" Your guess is too high.\n"; else cout << " Your guess is too low.\n";   } }

  6. Example • A company insures its drivers in the following cases • If driver is married • If the driver is unmarried, male & above 30 years of age • If the driver is unmarried, female & above 25 years of age

  7. Nested If example main() { char gender,ms; int age; cout<< "enter age , gender, marital status"; cin>>age>>gender>>ms; if(ms=='M') cout<<"driver is insured"; else { if(gender=='M') { if(age>30) cout<<"driver is insured"; else cout<<"driver is not insured"; } else { if (age>25) cout<<"driver is insured"; else cout<<"Driver is not insured"; } } getch(); }

  8. Logical Operator example #include<iostream.h> #include<conio.h> main() { char gender, ms; int age; cout<<"enter the age gender martial status"; cin>>age>>gender>>ms; if((ms=='M')|| (ms=='U'&& gender=='M' && age>30)|| (ms=='U' && gender=='F' && age>25)) cout<<"Driver is insured"; else cout<<"driver is not insured"; getch(); }

  9. switch Structures • C++’s switch structure gives the computer the power to choose from among many alternatives • A general syntax of the switch statement is:

  10. Switch Statement • switch, case, break, and default are reserved words. • A switch structure, first the expression is evaluated. The value of the expression is then used to perform the actions specified in the statements that follow the reserved word case. • optional part of the definition • Although it need not be, the expression is usually an identifier. Whether it is an identifier or an expression, the value can be only integral • The expression is sometimes called the selector.

  11. Flow Chart of switch statement switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..”

  12. Logical Operator example • Value of the expression is matched against a case value (Called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. • A break statement causes an immediate exit from the switch structure • If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is Skipped

  13. Example switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ :cout << “ Very Good ” ; break ; case ‘C’ : cout << “Good ” ; break ; case ‘D’ :cout << “ Poor ” ; break ; case ‘F’ :cout << “ Fail ” ; break ; }

  14. Example

  15. Example

  16. If alpha input is 5 , what will be alpha value after processing • cin>>alpha; • switch (alpha) • { • case 1: • case 2: • alpha = alpha + 2; • break; • case 4: • alpha++; • case 5: • alpha = 2 * alpha; • case 6: • alpha = alpha + 5; • break; • default: • alpha--; • }

  17. If alpha input is 5 , what will be alpha value after processing • cin>>alpha; • switch (alpha) • { • case 1: • case 2: • alpha = alpha + 2; • break; • case 4: • alpha++; • case 5: • alpha = 2 * alpha; • case 6: • alpha = alpha + 5; • break; • default: • alpha--; • }

  18. If a is 6 cin >> a; if (a > 0) switch (a) { case 1: a = a + 3; case 3: a++; break; case 6: a = a + 6; case 8: a = a * 8; break; default: a--; } else a = a + 2;

  19. Practice Problem • Write a program that prompt the user to enter two numbers and one “Arithmetic Operator” . Program should perform the Arithmetic operation on that numbers and display result.

  20. Loop - Repetition structure

  21. Why is Repetition Needed? To add a list of numbers a variable for each number and one for the total would need to be declared.  For a different number of values, recoding would be required.  C++ has three repetition structures that repeat statements over and over until certain conditions are met.

  22. Example int sum ; sum = 1+2+3+4+5+……..+10 ; cout << sum ;

  23. Find the Sum of the first 100 Integer starting from 1 ?

  24. while

  25. While while ( Logical Expression ) { statement1; statement2; …………. }

  26. ·      In C++, while is a reserved word.  ·      The expression acts as a decision maker.  It is called the loop condition. ·      The statement can be either a simple or compound statement.  It is called the body of the loop. Explanations: (Step 1)  If logical Expression evaluates to true, the statement executes. (Step 2) The logical Expression is reevaluated.  The body of the loop continues to execute until the logical Expression is false.   

  27. While while ( number <= 1000 ) { sum = sum + number; number = number + 1; }

  28. While Loop

  29. Problem Write a program for sum of numbers by prompting upper limit Write a program for sum of even number by prompting upper limit

  30. While loop Cases Case 1: Counter-controlled while loop Case 2:Sentinel Controlled While Loop Case 3:Flag-controlled while loops Case 4: EOF-controlled while loop

  31. Case 1- Counter-controlled while loop • Infinite loop- a loop that continues to execute endlessly is called an.   • An infinite loop just never ends - loops and loops forever, because the stopping condition is never met.   • Case 1: Counter-controlled while loop • The exact number of pieces of data is known – for example it is N • The counter is initialized to 0, then is evaluated to be <= N, then incremented in the body of the loop.

  32. Case 1- Counter-controlled while loop Example:  inti, sum;    sum = 0; i = 1;    while (i <= 100)    {         sum = sum + i; i=i+1;   } cout << "The sum of the first 100 numbers is " << sum << endl;   

  33. Case 2-Sentinel Controlled While Loop • · Case 2: Sentinel-controlled while loop • The exact number of pieces of data is not know, but the last entry is a special value, called a sentinel • The first item is read before the while statementif it does not equal the sentinel, the body of the while statement executes • Each additional item is read within the while statement.

  34. Case 2-Sentinel Controlled While Loop Example: int N, sum; cout << "Enter the numbers to be added (for a sentinel enter 0):" << endl;    sum = 0; cin >> N;    while (N != 0)    {       sum = sum + N; cin >> N;    } cout << "The sum of the entered numbers is " << sum << endl;  

  35. Telephone Digits • Write a program reads the letter codes A to Z and prints the corresponding telephone digit. Program will terminate when user press # button.

  36. int main() { char letter; cout << "Program to convert uppercase " << "letters to their corresponding " << "telephone digits." << endl; cout << "To stop the program enter #." << endl; cout << "Enter a letter: "; cin >> letter; cout << endl; while (letter != '#') { cout << "The letter you entered is: " << letter << endl; cout << "The corresponding telephone " << "digit is: "; if (letter >= 'A' && letter <= 'Z') switch (letter) { case 'A': case 'B': case 'C': cout << "2" <<endl; break; case 'D': case 'E': case 'F': cout << "3" << endl; break; case 'G': case 'H': case 'I': cout << "4" << endl; break; case 'J': case 'K': case 'L': cout << "5" << endl; break; case 'M': case 'N': case 'O': cout << "6" << endl; break;

  37. case 'P': case 'Q': case 'R': case 'S': cout << "7" << endl; break; case 'T': case 'U': case 'V': cout << "8" << endl; break; case 'W': case 'X': case 'Y': case 'Z': cout << "9" << endl; } else cout << "Invalid input." << endl; cout << "\nEnter another uppercase " << "letter to find its " << "corresponding telephone digit." << endl; cout << "To stop the program enter #." << endl; cout << "Enter a letter: "; cin >> letter; cout << endl; } return 0; }

More Related