Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unary Not operator !  !true = false  !false = true.

Similar presentations


Presentation on theme: "Unary Not operator !  !true = false  !false = true."— Presentation transcript:

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

21 #include void main() { int a,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++; }

22 Innovations Write the same program but now with fix values

23 #include void main() { int a; a=1; while(a<=10) { cout<<a<<endl; a++; } cout<<endl; }

24 Innovations Write the same program but now take values from user

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

26 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.


Download ppt "Unary Not operator !  !true = false  !false = true."

Similar presentations


Ads by Google