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

Post on 18-Jan-2018

246 views 0 download

description

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

transcript

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

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

cout << “ Welcome to Lahore University”;}

Nested ifIf (age > 18){

If(height > 5){

:}

}Make a flowchart of this nested if structure…

Programming Fundamentals

Lecture 6

In the last lecture

Conditional Construct if if-else

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.

Example

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

Find the Sum of the first 100 Integer starting from 1

?

WhileWhile means, 'do it until

the condition is true'.

while ( Logical Expression ){

statements; :}

Example

int sum ;sum = 0 ;

Example

int sum = 0; ( Optional )

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 ;

while (number <= UpperLimit)

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 ;

if ( number % 2 == 0 ){

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

}

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;

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.

Flow Chart for While Construct

#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++;} }

Innovations• Write the same program but now with fix values

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

int a;a=1;

while(a<=10){

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

}

cout<<endl;}

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

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

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.