1 / 16

Control structures in C++

Control structures in C++. Lecture 7 By Zahoor Ahmad. Lecture outline. Write a C++ program which calculates grades of students Write a C++ program using switch control structure which takes section number and prints its weekly schedule

jon
Download Presentation

Control structures in C++

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. Control structures in C++ Lecture 7 By Zahoor Ahmad

  2. Lecture outline • Write a C++ program which calculates grades of students • Write a C++ program using switch control structure which takes section number and prints its weekly schedule • Write a C++ program which takes a number as input and calculates factorial of the number • Write a C++ program which provides basic calculator functionality of addition, subtraction, multiplication, division • Write a C++ program which prints fabonacci sequence

  3. if(condition){} else if{} else {}: Control Structure Example

  4. Pseudo code • You can write C like code that may not exactly be C code but similar to that

  5. Flowchart If(percent>80) N Y If(percent>70 and percent < 80) N Y If(percent<70)

  6. /* • This program takes percentage of a student as an input and prints its grade. • */ • #include<iostream.h> • int main(void) { • int percentage; • cout << "\n Please enter your percentage marks: "; • cin >> percentage; • if(percentage > 80) { • cout << "\n You have got A grade \n"; • } • else if(percentage > 70 && percentage <= 80) { • cout << "\n You have got B grade \n"; • } • else if(percentage > 60 && percentage <= 70) { • cout << "\n You have got C grade \n"; • } • else { • cout << "\n You have got D grade \n"; • } • return 0; • }

  7. switch(condition){}: Control Structure Example

  8. If(section===A) Y N If(section===B) Y N If(section===C) ) Y Flowchart

  9. /* • This program takes section name and prints its class schedule. • */ • #include<iostream.h> • int main(void) { • char section; • cout << "\n Please enter your section name: "; • cin >> section; • switch(section){ • case 'a': • case 'A': • cout << "There are 60 students in section A\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'b': • case 'B': • cout << "There are 61 students in section B\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'c': • case 'C': • cout << "There are 65 students in section C\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • case 'd': • case 'D': • cout << "There are 60 students in section D\n"; • cout << "They have computer programming classes on tuesday and wednesday\n"; • break; • case 'e': • case 'E': • cout << "There are 56 students in section E\n"; • cout << "They have computer programming classes on wednesday and friday\n"; • break; • default: • cout << "You should register with some section if you wish to take this course"; • } • return 0; • }

  10. while(condition){}: Control Structure Example

  11. Flowchart Condition N Y increment

  12. /* • This program calculates factorial of the number which user enters. • */ • #include<iostream.h> • int main(void) { • // Variable Daclaration • int Num, Factorial; • // Variable initialisation • Factorial = 1; • cout << "\n Please enter a number for which you want to calculate factorial: "; • cin >> Num; • while(Num > 0) { • Factorial = Factorial * Num; • Num = Num - 1; • } • cout << "Factorial of the number is: " << Factorial; • return 0; • }

  13. do{}while(condition): Control Structure Example

  14. /* • This program presents user with a menu to select from. • */ • #include<iostream.h> • int main() { • // nSelection must be declared outside do/while loop • int nSelection; • nSelection = 0; • do • { • if(nSelection == 0){ • cout << " Make a selection from the menu below: \n"; • } • else{ • cout << " \nYou have made a valid selection\n "; • //Perform some actions according to selection like taking two inputs and perform arithmatic operation on them • } • cout << "Please make a selection: " << endl; • cout << "1) Addition" << endl; • cout << "2) Subtraction" << endl; • cout << "3) Multiplication" << endl; • cout << "4) Division" << endl; • cin >> nSelection; • } while (nSelection == 1 || nSelection == 2 || • nSelection == 3 || nSelection == 4); • return 0; • }

  15. for(initialisation;condition;increment){}: Control Structure Example

  16. /* • This program calculates facbonacci sequence. • 1,1,2,3,5,8,13... • */ • #include<iostream.h> • int main(void) { • // Variable Daclaration • int Num, first, second, temp; • // Variable initialisation • first = 1; • second = 1; • cout << first << ' '; • cout << second << ' '; • cout << "\n How many elements of fabonaci sequence you want to calculate: "; • cin >> Num; • for (int i = 2 ; i < Num ; i++){ • temp = first + second; • cout << temp << ' '; • first = second; • second = temp; • } • return 0; • }

More Related