Top Banner
Decision Making Statements
39

Decision Making Statements In Programming

Feb 22, 2017

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Decision Making Statements In Programming

Decision Making Statements

Page 2: Decision Making Statements In Programming

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

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

Operation of if else statement

Page 5: Decision Making Statements In Programming

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

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

If Statement in C

If (condition)statement ;

Page 8: Decision Making Statements In Programming

If Statement in CIf ( condition ){

statement1 ;statement2 ;

:}

Page 9: Decision Making Statements In Programming

If statement in C

if (age1 > age2)cout<<“Student 1 is older than student 2” ;

Page 10: Decision Making Statements In Programming

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

Relational Operators

a != b;

X = 0;X == 0;

Page 12: Decision Making Statements In Programming

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

Flow Chart SymbolsStart or stop

Process

Continuation mark

Decision

Flow line

Page 14: Decision Making Statements In Programming

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

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

Logical Operators

AND &&OR ||

Page 17: Decision Making Statements In Programming

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

if-elseif (condition){

statement ;--

}else{

statement ;--

}

Page 19: Decision Making Statements In Programming

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

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

ExampleCode

if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;} else { cout<<“Amir is younger than Amara” ;}

Page 22: Decision Making Statements In Programming

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

ExampleIf (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages

are not equal”;

Page 24: Decision Making Statements In Programming

Unary Not operator !

!true = false !false = true

Page 25: Decision Making Statements In Programming

If (!(AmirAge > AmaraAge))

?

Page 26: Decision Making Statements In Programming

Example

if ((interMarks > 45) && (testMarks >= passMarks)){

cout << “ Welcome to Virtual University”;}

Page 27: Decision Making Statements In Programming

Example

If(!((interMarks > 45) && (testMarks >= passMarks)))

?

Page 28: Decision Making Statements In Programming

Nested ifIf (age > 18){

If(height > 5){

:}

}Make a flowchart of this nested if structure…

Page 29: Decision Making Statements In Programming

In Today’s Lecture Decision

If Else

Flowcharts Nested if

Page 30: Decision Making Statements In Programming

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

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

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

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
Page 35: Decision Making Statements In Programming

Defining Label and use of switch, break and goto statements

Page 36: Decision Making Statements In Programming

#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

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

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