SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Control Structures: Conditional Constructs and Looping in Programming

angelo avatar
26 diapositives · 410 Views · Jul 19, 2014

This lecture discusses key programming concepts such as conditional constructs (if, if-else) and repetition structures (loops). It explains the unary NOT operator, nested if statements, and the importance of controlling flow in programming. The lecture also covers practical examples including finding the sum of integers and handling even numbers using loops. Additionally, it highlights the potential for infinite loops and provides guidance on avoiding them. Flowcharts are introduced to visualize these structures, emphasizing their relevance in real-life systems like payroll.

Understanding Control Structures: Conditional Constructs and Looping in Programming - slide 1
01 / 26Understanding Control Structures: Conditional Constructs and Looping in Programming
1 / 17

Télécharger la présentation

Understanding Control Structures: Conditional Constructs and Looping in Programming
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. Unary Not operator ! • !true = false • !false = true

  2. Example if ((interMarks > 45) && (testMarks >= passMarks)) { cout << “ Welcome to Lahore University”; }

  3. Nested if If (age > 18) { If(height > 5) { : } } Make a flowchart of this nested if structure…

  4. Programming Fundamentals Lecture 6

  5. In the last lecture Conditional Construct if if-else

  6. Loop - Repetition structure In our day to day life, most of the things are repeated. Days and nights repeat themselves 30 times a month. Four seasons replace each other every year. We can see similar phenomenon in the practical life. For example, in the payroll system, some procedures are same for all the employees. These are repeatedly applied while dealing with the employees. So repetition is very useful structure in the programming.

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

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

  9. While While means, 'do it until the condition is true'.

  10. while ( Logical Expression ) { statements; : }

  11. Example int sum ; sum = 0 ;

  12. Example int sum = 0; ( Optional )

  13. Example int sum , number ; sum = 0 ; number = 1 ; while ( number <= 1000 ) { sum = sum + number ; number = number + 1 ; } cout << “ The sum of the first 1000 integer starting from 1 is ” << sum ;

  14. while (number <= UpperLimit)

  15. Example int sum, number , UpperLimit ; sum = 0 ; number = 1 ; cout << “ Please enter the upper limit for which you want the sum ” ; cin >> UpperLimi t; while (number <= UpperLimit) { sum = sum + number ; number = number +1 ; } cout << “ The sum of the first ” << UpperLimit << “ integer is ” << sum ;

  16. if ( number % 2 == 0 ) { sum = sum + number ; number = number + 1 ; }

  17. Example sum = 0; number = 1; cout << “ Please enter the upper limit for which you want the sum ”; cin >> UpperLimit; while (number <= UpperLimit) { if (number % 2 == 0) { sum = sum + number; } number = number + 1; } cout << “ The sum of all even integer between 1 and ” << UpperLimit << “ is” << sum;

  18. Infinite Loop: • Consider the condition in the whilestructure that is (number <= upperLimit) and in • the whileblock the value of numberis changing (number = number + 1) to ensure that • the condition is tested again next time. If it is true, the whileblock is executed and so • on. So in the whileblock statements, the variable used in condition must change its • value so that we have some definite number of repetitions. What will happen if we do • not write the statement number = number + 1;in our program? The value of number • will not change, so the condition in the whileloop will be true always and the loop • will be executed forever. Such loops in which the condition is always true are known • as infinite loops as there are infinite repetitions in it.

  19. Flow Chart for While Construct

  20. #include<iostream.h> void main() { inta,b; cout<<" Input the first printing value :" ; cin>>a; cout<<endl; cout<<"Input the last printing value :" ; cin>>b; cout<<endl; while(a<=b) { cout<<" The Print out value is :"<<a; cout<<endl; a++; } }

  21. Innovations • Write the same program but now with fix values

  22. #include<iostream.h> void main() { inta; a=1; while(a<=10) { cout<<a<<endl; a++; } cout<<endl; }

  23. Innovations • Write the same program but now take values from user

  24. Factorial Definition n! = n*(n-1)*(n-2)*(n-3)…………*3*2*1

  25. Property of While Statement • In the above example, if the user enters 0, as the value for upper limit. In the while condition we test (number <= upperLimit) i.e. number is less than or equal to upperLimit( 0 ), this test return false. The control of the program will go to the next statement after the while block. The statements in while structure will not be executed even for a single time. So the property of while loop is that it may execute zero or more time.