SlideServe
Menu
Préférences
© SlideServe · v2026

Control structures in C++

jon avatar
16 diapositives · 1.7K Views · Oct 23, 2014

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

Control structures in C++ - slide 1
01 / 16Control structures in C++
1 / 17

Télécharger la présentation

Control structures in C++
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. 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; • }