• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture 4
 

Lecture 4

on

  • 173 views

 

Statistics

Views

Total Views
173
Views on SlideShare
173
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecture 4 Lecture 4 Presentation Transcript

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