unary not operator !

26
Unary Not operator ! !true = false !false = true

Upload: angelo

Post on 24-Feb-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Unary Not operator !. !true = false !false = true. Example. if (( interMarks > 45) && ( testMarks >= passMarks )) { cout 18) { If(height > 5) { : } } Make a flowchart of this nested if structure…. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unary Not operator  !

Unary Not operator ! !true = false !false = true

Page 2: Unary Not operator  !

Exampleif ((interMarks > 45) && (testMarks >= passMarks)){

cout << “ Welcome to Lahore University”;}

Page 3: Unary Not operator  !

Nested ifIf (age > 18){

If(height > 5){

:}

}Make a flowchart of this nested if structure…

Page 4: Unary Not operator  !

Programming Fundamentals

Lecture 6

Page 5: Unary Not operator  !

In the last lecture

Conditional Construct if if-else

Page 6: Unary Not operator  !

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.

Page 7: Unary Not operator  !

Example

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

Page 8: Unary Not operator  !

Find the Sum of the first 100 Integer starting from 1

?

Page 9: Unary Not operator  !

WhileWhile means, 'do it until

the condition is true'.

Page 10: Unary Not operator  !

while ( Logical Expression ){

statements; :}

Page 11: Unary Not operator  !

Example

int sum ;sum = 0 ;

Page 12: Unary Not operator  !

Example

int sum = 0; ( Optional )

Page 13: Unary Not operator  !

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 ;

Page 14: Unary Not operator  !

while (number <= UpperLimit)

Page 15: Unary Not operator  !

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 ;

Page 16: Unary Not operator  !

if ( number % 2 == 0 ){

sum = sum + number ;number = number + 1 ;

}

Page 17: Unary Not operator  !

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;

Page 18: Unary Not operator  !

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.

Page 19: Unary Not operator  !

Flow Chart for While Construct

Page 20: Unary Not operator  !
Page 21: Unary Not operator  !

#include<iostream.h> 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++;} }

Page 22: Unary Not operator  !

Innovations• Write the same program but now with fix values

Page 23: Unary Not operator  !

#include<iostream.h>void main(){

int a;a=1;

while(a<=10){

cout<<a<<endl;a++;

}

cout<<endl;}

Page 24: Unary Not operator  !

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

Page 25: Unary Not operator  !

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

Page 26: Unary Not operator  !

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.