Download presentation
Presentation is loading. Please wait.
1
Programming Fundamental
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. Condition Print a>b>c White a>c>b Yellow b>a>c Pink b>c>a Red c>a>b Green c>b>a Black
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"; cout<<"Green"; if(a>c) { cout<<"Pink"; } else if(b>c) cout<<"Red"; cout<<"Black";
5
IF Condition (multiple scenario)
main() { int magic; int guess; 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 include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
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 not insured"; } if (age>25) cout<<"Driver is not insured"; getch(); include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
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(); } include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
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: include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
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. include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
12
Flow Chart of switch statement
switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..”
13
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 include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
14
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 ” ; }
15
Example
16
Example
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; default: alpha--; } 15
18
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; default: alpha--; } 7
19
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; default: a--; } else a = a + 2; 7
20
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.
21
Loop - Repetition structure
22
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.
23
Example int sum ; sum = ……..+10 ; cout << sum ;
24
Find the Sum of the first 100 Integer starting from 1
?
25
while
26
While while ( Logical Expression ) { statement1; statement2; …………. }
27
Explanations: · 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.
29
While while ( number <= 1000 ) { sum = sum + number;
number = number + 1; }
30
While Loop
31
Problem Write a program for sum of numbers by prompting upper limit
Write a program for sum of even number by prompting upper limit
33
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
34
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.
35
Case 1- Counter-controlled while loop
Example: int i, 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;
36
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 statement if it does not equal the sentinel, the body of the while statement executes Each additional item is read within the while statement.
37
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;
38
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.
39
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; case 'G': case 'H': case 'I': cout << "4" << endl; case 'J': case 'K': case 'L': cout << "5" << endl; case 'M': case 'N': case 'O': cout << "6" << endl;
40
case 'P': case 'Q': case 'R': case 'S': cout << "7" << endl; break; case 'T': case 'U': case 'V': cout << "8" << endl; 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 #." cout << "Enter a letter: "; cin >> letter; cout << endl; } return 0;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.