decision making statements in programming

Post on 22-Feb-2017

16 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Decision Making Statements

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.

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();

}

Operation of if else statement

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

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

If Statement in C

If (condition)statement ;

If Statement in CIf ( condition ){

statement1 ;statement2 ;

:}

If statement in C

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

Relational Operators< less than<= less than or equal to== equal to>= greater than or equal to> greater than!= not equal to

Relational Operators

a != b;

X = 0;X == 0;

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

}

Flow Chart SymbolsStart or stop

Process

Continuation mark

Decision

Flow line

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

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

Logical Operators

AND &&OR ||

Logical OperatorsIf a is greater than b

AND c is greater than d

In Cif(a > b && c> d)if(age > 18 || height > 5)

if-elseif (condition){

statement ;--

}else{

statement ;--

}

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

Code

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

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

Example

ExampleCode

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

Example Code

if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;} else { cout<<“Amir is younger than or of the same age as

Amara” ;}

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

are not equal”;

Unary Not operator !

!true = false !false = true

If (!(AmirAge > AmaraAge))

?

Example

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

cout << “ Welcome to Virtual University”;}

Example

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

?

Nested ifIf (age > 18){

If(height > 5){

:}

}Make a flowchart of this nested if structure…

In Today’s Lecture Decision

If Else

Flowcharts Nested if

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

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.

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

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

Defining Label and use of switch, break and goto statements

#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

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

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();

}

top related