Programming Fundamental

Slides:



Advertisements
Similar presentations
Chapter 5: Control Structures II (Repetition)
Advertisements

Decisions If statements in C.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 5: Control Structures II (Repetition)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Previously Repetition Structures While, Do-While, For.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Computer Programming
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 2.1 Control Structures (Selection)
Control Structures II (Repetition)
Repetition-Sentinel,Flag Loop/Do_While
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements I
Control Structures - Repetition
TOPIC 4: REPETITION CONTROL STRUCTURE
Alternate Version of STARTING OUT WITH C++ 4th Edition
Additional Control Structures
Conditional Construct
Structured Program
3 Control Statements:.
Chapter 2.2 Control Structures (Iteration)
2.6 The if/else Selection Structure
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Selection Control Structure
Programming Fundamental
Programming Fundamental-1
Programming Fundamental
Programming Fundamental-1
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-7

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

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

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";

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";    }  }

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

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

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

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

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

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

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

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 ” ; }

Example

Example

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

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

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

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.

Loop - Repetition structure

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.

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

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

while

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

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.      

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

While Loop

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

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

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.  

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;   

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.

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;   

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.

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;

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;