Your SlideShare is downloading. ×
Lecture 4
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lecture 4

87
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
87
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. DECISIONS LECTURE # 4
  • 2. if Statement Syntax • Single statement if if(condition) statement; • Multi statement if if(condition) { statement1; statement2; }
  • 3. if Statement Flow Chart Body of if Test Expression Exit FalseTrue
  • 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. 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. 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. If…else Statement Flow Chart Body of if Test Expression Exit FalseTrue Body of else
  • 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. 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. 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. 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. 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. 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. 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. 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. Conditional Operator ?: Syntax result=(Condition)? Expression 1: Expression 2;
  • 17. Conditional Operator Flow Chart Expression 1 Test Expression Exit FalseTrue Expression 2
  • 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. Logical Operators Operator Effect && Logical And || Logical Or ! Logical Not
  • 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. 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. 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. Operator Precedence Operator Type Operators Unary !,++,-- Arithmetic *, /, % +, - Relational <, >, <=, >= ==, != Logical && || Conditional ?: Assignment =, +=, -=, *=, /=, %=
  • 24. Continue Statement Condition within loop continue; Start of loop Normal loop return
  • 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. goto Statement Syntax goto Label; //other statements Label: //control will begin here

×