c++ lecture 4

26
DECISIONS LECTURE # 4

Upload: sajidpk92

Post on 19-May-2015

123 views

Category:

Education


0 download

TRANSCRIPT

Page 1: c++ Lecture 4

DECISIONS LECTURE # 4

Page 2: c++ Lecture 4

if Statement

Syntax

• Single statement if if(condition)

statement;

• Multi statement if if(condition)

{

statement1;

statement2;

}

Page 3: c++ Lecture 4

if Statement Flow Chart

Body of if

Test Expression

Exit

False True

Page 4: c++ Lecture 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;

}

Page 5: c++ Lecture 4

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

Page 6: c++ Lecture 4

If…else Statement

Syntax • Single statement if...else

if(condition) statement; else statement;

• Multi statement if…else if(condition) { statement1; statement2; } else { statement1; statement2; }

Page 7: c++ Lecture 4

If…else Statement Flow Chart

Body of if

Test Expression

Exit

False True

Body of else

Page 8: c++ Lecture 4

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;

}

Page 9: c++ Lecture 4

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 phrase\n”;

while(ch!=‘\r’)

{

ch=getche();

if(ch==‘ ‘)

wdcount++;

else

chcount++;

}

cout<<“\nWord count=“<<wdcount<<endl;

cout<<“Character count=“<<chcount;

return 0;

}

Page 10: c++ Lecture 4

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;

}

afshanjamil
Text Box
(condition)
afshanjamil
Text Box
(condition)
Page 11: c++ Lecture 4

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

Page 12: c++ Lecture 4

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;

}

Page 13: c++ Lecture 4

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

Page 14: c++ Lecture 4

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

Page 15: c++ Lecture 4

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

Page 16: c++ Lecture 4

Conditional Operator ?:

Syntax

result=(Condition)? Expression 1: Expression 2;

Page 17: c++ Lecture 4

Conditional Operator Flow Chart

Expression 1

Test Expression

Exit

False True

Expression 2

Page 18: c++ Lecture 4

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;

}

Page 19: c++ Lecture 4

Logical Operators

Operator Effect

&& Logical And

|| Logical Or

! Logical Not

Page 20: c++ Lecture 4

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;

}

Page 21: c++ Lecture 4

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

Page 22: c++ Lecture 4

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;

}

Page 23: c++ Lecture 4

Operator Precedence

Operator Type Operators

Unary !,++,--

Arithmetic *, /, %

+, -

Relational <, >, <=, >=

==, !=

Logical &&

||

Conditional ?:

Assignment =, +=, -=, *=, /=, %=

Page 24: c++ Lecture 4

Continue Statement

Condition within loop

continue;

Start of loop Normal loop return

Page 25: c++ Lecture 4

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;

}

Page 26: c++ Lecture 4

goto Statement

Syntax

goto Label;

//other statements

Label:

//control will begin here