c++ lecture 4
TRANSCRIPT
![Page 1: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/1.jpg)
DECISIONS LECTURE # 4
![Page 2: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/2.jpg)
if Statement
Syntax
• Single statement if if(condition)
statement;
• Multi statement if if(condition)
{
statement1;
statement2;
}
![Page 3: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/3.jpg)
if Statement Flow Chart
Body of if
Test Expression
Exit
False True
![Page 4: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/7.jpg)
If…else Statement Flow Chart
Body of if
Test Expression
Exit
False True
Body of else
![Page 8: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/10.jpg)
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;
}
![Page 11: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/16.jpg)
Conditional Operator ?:
Syntax
result=(Condition)? Expression 1: Expression 2;
![Page 17: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/17.jpg)
Conditional Operator Flow Chart
Expression 1
Test Expression
Exit
False True
Expression 2
![Page 18: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/19.jpg)
Logical Operators
Operator Effect
&& Logical And
|| Logical Or
! Logical Not
![Page 20: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/22.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/23.jpg)
Operator Precedence
Operator Type Operators
Unary !,++,--
Arithmetic *, /, %
+, -
Relational <, >, <=, >=
==, !=
Logical &&
||
Conditional ?:
Assignment =, +=, -=, *=, /=, %=
![Page 24: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/24.jpg)
Continue Statement
Condition within loop
continue;
Start of loop Normal loop return
![Page 25: c++ Lecture 4](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/25.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052410/555a5f3ad8b42ae7218b45e7/html5/thumbnails/26.jpg)
goto Statement
Syntax
goto Label;
//other statements
Label:
//control will begin here