decision making statements in programming
TRANSCRIPT
![Page 1: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/1.jpg)
Decision Making Statements
![Page 2: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/2.jpg)
if statement
The if else statement executes one or more commands when a condition is true.
If condition is false then those commands are ignored and are not executed.
![Page 3: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/3.jpg)
if statement#include<iostream.h>#include<conio.h>void main(void){ clrscr();
int a;cout<<"Enter value";cin>>a;if(a>=10){
cout<<“BSCS";}else{
cout<<“MCS";}getch();
}
![Page 4: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/4.jpg)
Operation of if else statement
![Page 5: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/5.jpg)
Nested if statement#include<iostream.h>#include<conio.h>void main(void){ clrscr();
int a, b, c;cout<<"Enter values for A, B and C ";cin>>a>>b>>c;if(a==b){
if(b==c){cout<<"A. B and C are Equal";}else{cout<<"B and C are not Equal";}
}else{cout<<"A and B are not Equal";}getch();
}
OutputEnter values for A, B and C 5 5 8B and C are not Equal
Enter values for A, B and C 5 3 3A and B are not Equal
Enter values for A, B and C 4 4 4A. B and C are Equal
Enter values for A, B and C 8 6 8A and B are not Equal
![Page 6: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/6.jpg)
If StatementIf condition is true
statements
If Ali’s height is greater then 6 feetThen
Ali can become a member of the Basket Ball team
![Page 7: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/7.jpg)
If Statement in C
If (condition)statement ;
![Page 8: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/8.jpg)
If Statement in CIf ( condition ){
statement1 ;statement2 ;
:}
![Page 9: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/9.jpg)
If statement in C
if (age1 > age2)cout<<“Student 1 is older than student 2” ;
![Page 10: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/10.jpg)
Relational Operators< less than<= less than or equal to== equal to>= greater than or equal to> greater than!= not equal to
![Page 11: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/11.jpg)
Relational Operators
a != b;
X = 0;X == 0;
![Page 12: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/12.jpg)
Example#include <iostream.h>main ( ) {
int AmirAge, AmaraAge;AmirAge = 0;AmaraAge = 0;
cout<<“Please enter Amir’s age”;cin >> AmirAge;cout<<“Please enter Amara’s age”;cin >> AmaraAge;
if AmirAge > AmaraAge) {
cout << “\n”<< “Amir’s age is greater then Amara’s age” ;}
}
![Page 13: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/13.jpg)
Flow Chart SymbolsStart or stop
Process
Continuation mark
Decision
Flow line
![Page 14: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/14.jpg)
Flow Chart for if statement
Condition
Process
IF
Then
Entry point for IF block
Exit point for IF block
Note indentation from left to right
![Page 15: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/15.jpg)
ExampleIf the student age is greater than 18
or his height is greater than five feet then put him on the foot balll team
ElsePut him on the chess team
![Page 16: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/16.jpg)
Logical Operators
AND &&OR ||
![Page 17: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/17.jpg)
Logical OperatorsIf a is greater than b
AND c is greater than d
In Cif(a > b && c> d)if(age > 18 || height > 5)
![Page 18: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/18.jpg)
if-elseif (condition){
statement ;--
}else{
statement ;--
}
![Page 19: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/19.jpg)
if-elseCondition
Process 1
IF
Then
Entry point for IF-Else block
Exit point for IF block
Process 2
Else
Note indentation from left to right
![Page 20: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/20.jpg)
Code
if (AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;}
if (AmirAge < AmaraAge) { cout<< “Amir is younger than Amara” ;}
Example
![Page 21: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/21.jpg)
ExampleCode
if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;} else { cout<<“Amir is younger than Amara” ;}
![Page 22: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/22.jpg)
Example Code
if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;} else { cout<<“Amir is younger than or of the same age as
Amara” ;}
![Page 23: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/23.jpg)
ExampleIf (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages
are not equal”;
![Page 24: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/24.jpg)
Unary Not operator !
!true = false !false = true
![Page 25: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/25.jpg)
If (!(AmirAge > AmaraAge))
?
![Page 26: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/26.jpg)
Example
if ((interMarks > 45) && (testMarks >= passMarks)){
cout << “ Welcome to Virtual University”;}
![Page 27: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/27.jpg)
Example
If(!((interMarks > 45) && (testMarks >= passMarks)))
?
![Page 28: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/28.jpg)
Nested ifIf (age > 18){
If(height > 5){
:}
}Make a flowchart of this nested if structure…
![Page 29: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/29.jpg)
In Today’s Lecture Decision
If Else
Flowcharts Nested if
![Page 30: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/30.jpg)
Counting words and characters with different technique#include<iostream.h>#include<conio.h>void main(void){
int chcount=0;int wdcount=1;char ch;clrscr();while((ch=getche()) !='\r'){ if(ch==' ')
wdcount++;else
chcount++;}cout<<"\nWords = "<<wdcount;cout<<"\nCharacters = "<<chcount;getch();
}
Outputhello how are youWords = 4Characters = 14
![Page 31: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/31.jpg)
The switch statement The switch statement is similar to the if
else or else if construct but is more flexible.
If decision tree is large, and all the decisions depend on the value of the same variable, then it is better to use switch statement instead of series of if else or else if statements.
![Page 32: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/32.jpg)
The switch statement#include<iostream.h>#include<conio.h>void main(void){ clrscr();
char ch=‘b’;cout<<“Enter character”;cin>>ch;switch(ch){
case ‘b':cout<<“BSCS”<<endl;break;
case ‘m':cout<<“MCS”<<endl;break;
default:cout<<“BSCS and MCS";
}getch();}
![Page 33: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/33.jpg)
The break statement The break keyword causes the entire switch
statement to exit. Control goes to the first statement following the end of the switch statement.
If break statement is not used then the control passes down to the next case statement and the statements that we do not want to execute, starts executing.
If the value of the switch variable doesn’t match any of the case constants then control passes to the end of the switch without doing anything
![Page 34: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/34.jpg)
![Page 35: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/35.jpg)
Defining Label and use of switch, break and goto statements
![Page 36: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/36.jpg)
#include<iostream.h>#include<conio.h>void main(void) // main start{ clrscr();int a, b;char ch;cout<<"Enter 1st value ";cin>>a;cout<<"Enter 2nd value ";cin>>b;again:cout<<"+, -, x, / ";ch=getche();cout<<endl;
switch(ch){case '+':cout<<a<<" + "<<b<<" = "<<a+b<<endl;break;case '-':cout<<a<<" - "<<b<<" = "<<a-b<<endl;break;case 'x':cout<<a<<" x "<<b<<" = "<<a*b<<endl;break;case '/':cout<<a<<" / "<<b<<" = "<<a/b<<endl;break;default:cout<<"Wrong Choice\n";goto again;
}getch();} // main end
Example
![Page 37: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/37.jpg)
EXAMPLEUsing multiple cases in switch
statement
#include<iostream.h>#include<conio.h>void main(void){ clrscr();int a, b;char ch;cout<<"Enter 1st value ";cin>>a;cout<<"Enter 2nd value ";cin>>b;cout<<"1. Add\n";cout<<"2. Subtract\n";cout<<"3. Multiply\n";cout<<"4. Divide\n";again:cout<<"Enter your choice (1-4) ";ch=getche();cout<<endl;
switch(ch) {
case '1':case 'A':case 'a':
cout<<a<<" + "<<b<<" = "<<a+b<<endl;break;
case '2':case 'S':case 's':
cout<<a<<" - "<<b<<" = "<<a-b<<endl;break;
case '3':case 'M':case 'm':
cout<<a<<" x "<<b<<" = "<<a*b<<endl;break;
case '4':case 'D':case 'd':
cout<<a<<" / "<<b<<" = "<<a/b<<endl;break;
default:cout<<"Wrong Choice\n";goto again;
}getch();}
![Page 38: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/38.jpg)
The Conditional Operator There is a compressed way of expressing the if else statement and
is called the conditional operator.#include<iostream.h>#include<conio.h>void main(void)
{ clrscr();int a, b;cout<<"Enter 1st value ";cin>>a;cout<<"Enter 2nd value ";cin>>b;(a > b) ? cout<<"1st value is greater" : cout<<"2nd value is greater";getch();
}
![Page 39: Decision Making Statements In Programming](https://reader034.vdocument.in/reader034/viewer/2022042906/58acf5341a28abca0c8b4fb3/html5/thumbnails/39.jpg)