Lecture 4

456 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
456
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 4

  1. 1. DECISIONS LECTURE # 4
  2. 2. if Statement Syntax • Single statement if if(condition) statement; • Multi statement if if(condition) { statement1; statement2; }
  3. 3. if Statement Flow Chart Body of if Test Expression Exit FalseTrue
  4. 4. if Statement Example #include<iostream> using namespace std; int main() { int num; cout<<“Enter a number”; cin>>num; if(num>100) { cout<<“Number is greater than 100”; cout<<endl; } return 0; }
  5. 5. Example • Generate even number using if and for loop. #include<iostream> #include<conio.h> using namespace std; int main() { int i; for(i=0;i<=100;i++) { if(i%2==0) cout<<i<<endl; } getche(); return 0; }
  6. 6. If…else Statement Syntax • Single statement if...else if(condition) statement; else statement; • Multi statement if…else if(condition) { statement1; statement2; } else { statement1; statement2; }
  7. 7. If…else Statement Flow Chart Body of if Test Expression Exit FalseTrue Body of else
  8. 8. If…else Statement Example #include<iostream> using namespace std; int main() { int num; cout<<“Enter a number”; cin>>num; if(num>100) { cout<<“Number is greater than 100”; cout<<endl; } else cout<<“Number is not greater than 100”; return 0; }
  9. 9. If…else Statement Example #include<iostream> #include<conio.h> using namespace std; int main() { int chcount=-1; int wdcount=1; char ch=‘a’; cout<<“Enter a phrasen”; while(ch!=‘r’) { ch=getche(); if(ch==‘ ‘) wdcount++; else chcount++; } cout<<“nWord count=“<<wdcount<<endl; cout<<“Character count=“<<chcount; return 0; }
  10. 10. Nested If…else Statement Syntax • Single statement if...else if(condition) statement; else statement; • Multi statement if…else if(condition) { statement1; statement2; } else { statement1; statement2; } (condition) (condition)
  11. 11. Nested If…else Example #include<iostream> #include<conio.h> using namespace std; int main() { char dir=‘a’; int x=0,y=0; while(dir!=‘r’) { cout<<“Enter your location”; dir=getche(); if(dir==‘n’) y--; else if(dir==‘s’) y++; else if(dir==‘e’) x++; else if(dir==‘w’) x--; } return 0; }
  12. 12. Matching else #include<iostream> #include<conio.h> int main() { int a,b,c; cout<<“Enter three numbers:”; cin>>a>>b>>c; if(a==b) if(b==c) cout<<“b and c are same”; else cout<<“b and c are different”; getche(); retutn 0; }
  13. 13. Switch Statement Syntax switch(n) { case 1: statement statement; break; case 2: statement statement; break; default: statement; statement; } 1st case body 2nd case body Default body Break causes exit from switch
  14. 14. Switch Statement Flow Chart 1st case body Switch variable==1st case constant Exit Switch variable==2nd case constant Switch variable==nth case constant 2nd case body Nth case body Default body true true true false false false
  15. 15. Switch Example #include<iostream> #include<conio.h> using namespace std; int main() { char dir=‘a’; int x=0,y=0; while(dir!=‘r’) { cout<<“Enter your location”; dir=getche(); switch(dir) { case ‘n’: y--; break; case ‘s’: y++; break; case ‘e’: x++; break; case ‘w’: x--; break; default: cout<<“try again”; } //end switch } return 0; }
  16. 16. Conditional Operator ?: Syntax result=(Condition)? Expression 1: Expression 2;
  17. 17. Conditional Operator Flow Chart Expression 1 Test Expression Exit FalseTrue Expression 2
  18. 18. Conditional Operator Example #include<iostream> #include<conio.h> using namespace std; int main() { int x=100, y=0; int result=(x>y)?x:y; getche(); return 0; }
  19. 19. Logical Operators Operator Effect && Logical And || Logical Or ! Logical Not
  20. 20. Logical And #include<iostream> #include<conio.h> using namespace std; int main() { int number; cout<<“Enter your numbers”; cin>>number; if(number>=90) cout<<“A”; else if(number>=70 && number<90) cout<<“B”; else if(number>=60 && number<70) cout<<“C”; else cout<<“F”; getche(); return 0; }
  21. 21. Logical Or #include<iostream> #include<conio.h> using namespace std; int main() { char dir=‘a’; int x=0,y=0; while(dir!=‘r’) { cout<<“Enter your location”; dir=getche(); if(dir==‘n’ || dir==‘N’) y--; else if(dir==‘s’ || dir==‘S’) y++; else if(dir==‘e’ || dir==‘E’) x++; else if(dir==‘w’ || dir==‘W’) x--; } return 0; }
  22. 22. Logical Not #include<iostream> #include<conio.h> int main() { int x; cout<<“enter a number”; cin>>x; if(!(x==0)) cout<<“Number is not zero”; getche(); return 0; }
  23. 23. Operator Precedence Operator Type Operators Unary !,++,-- Arithmetic *, /, % +, - Relational <, >, <=, >= ==, != Logical && || Conditional ?: Assignment =, +=, -=, *=, /=, %=
  24. 24. Continue Statement Condition within loop continue; Start of loop Normal loop return
  25. 25. Example #include<iostream> #include<conio.h> int main() { int dividend, divisor; char ch=‘a’; while(ch!=‘r’) { cout<<“Enter dividend”; cin>>dividend; cout<<“Enter divisor”; cin>>divisor; if(divisor==0) continue; cout<<“Quotient=“<<dividend/divisor; } getche(); return 0; }
  26. 26. goto Statement Syntax goto Label; //other statements Label: //control will begin here

×