Lecture_Notes_07_08_09_10

# Lecture_Notes_07_08_09_10 - De Morgans Law(a

This preview shows pages 1–11. Sign up to view the full content.

De Morgan’s Law !(a<b || c<d) is same as !(a<b) && !(c<d) is same as a>=b && c>=d ____________________________ !(a==b && c!=d) is same as !(a==b) || !(c!=d) is same as a!=b || c==d Note that and and or operators are reversed by moving the not inward.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lazy Evaluation of Logical Operators #include <iostream> #include <cstdlib> using namespace std; int main() { int score=50, labscore=60; if (score<40 && ++labscore<65) { cout<< "Inside if condition" ; } cout<< "score is " << score<<endl; cout<< "labscore is " <<labscore<<endl; system( "PAUSE" ); return 0; } The && and || operators are computed lazily. As soon as truth value is determined, no further conditions are evaluated
1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. char ch; 6. cout<< "Hello World" <<endl; 7. cout<< "Hello World" <<endl; 8. system ( "PAUSE"); 9. return 0 ; 10. } Why Use Loops?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1. #include <iostream> 2. using namespace std; 3. void main() 4. { 5. char ch; 6. for ( int i=0; i<5; i++) 7. { 8. cout<< "Hello World" <<endl; 9. } 10. system ( "PAUSE"); 11. return 0 ; 12. } Why Use Loops?
Types of Loops Loops are of three types: while loop - the loop continuation condition is tested at the beginning of the loop before the body of the loop is performed. do/while loop – the loop continuation condition is tested after the loop body is performed; therefore, the loop body will be executed at lease once. for loop – this loop is primarily used for counting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
while Loop condition statement1; statement2; ……………. while (condition) { statement1; statement2; } system( "PAUSE"); return EXIT_SUCCESS; false true while (condition) statement1; system( "PAUSE"); return EXIT_SUCCESS; Multiple Statements Block of Statements Single Statement
while Loop product<10 product= 2*product 1. int main() 2.{ 3. int product=0; 4. while (product<10) 5. { 6. product +=2; //equivlent to product=product+2 7. cout<<product<<endl; 8. } 9. system( "PAUSE"); 10. return EXIT_SUCCESS; 11.} false true

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document