Decision Making It is used to change the order of the program based on condition. Categories: –...

75
Decision Making • It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration structure – Encapsulation structure

Transcript of Decision Making It is used to change the order of the program based on condition. Categories: –...

Page 1: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Decision Making

• It is used to change the order of the program based on condition.

• Categories:– Sequential structure– Selection structure– Iteration structure– Encapsulation structure

Page 2: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Decision Making (cont)• Sequential structure– In which instructions are executed in sequence.

• Selection structure– In which instruction are executed based on the

result of some condition.• Iteration structure– In which instruction are executed repeatedly.

• Encapsulation structure– In which some compound structure are used.

Page 3: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

SELECTION STRUCTURE

• It allows the program to make a choice from alternative paths.

• C provide the following selection structures– IF statement– IF … ELSE statement– Nested IF … ELSE statement– IF … ELSE ladder

Page 4: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

IF Statement

SyntaxIF (condition is true)

{

Statements;

}

If condition

False

True

Statements

Page 5: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);

if(a>10){

printf(" \n a is greater than 10");}

getch( );}

Page 6: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the number: 12a is greater than 10

Page 7: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

IF…ELSE StatementSyntaxIF (condition) {

True statements;}ELSE{

False statements;}

If Condition

True False

True statements

False statements

Page 8: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);

if(a>10){

printf(" \n a is greater than 10");}

else{

printf(" \n a is less than 10");}

getch( );}

Page 9: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

NESTED IF… ELSE

If Condition

2

True False

True statements

False statements

IfCondition

1False

StatementsTrue

Page 10: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

NESTED IF… ELSESyntaxIF (condition1) {

IF (condition2) {

True statements;}ELSE{

False statements;}

}ELSE{

False statements;}

Page 11: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

IF…ELSE LADDER

Condition1

Statements

Condition2

Statements

Condition3

Statements Statements

TRUE

TRUE

TRUE FALSE

FALSE

FALSE

Page 12: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

IF…ELSE LADDERSyntaxIF (condition1) {statements;}

else if (condition2) {statements;}

else if (condition3){statements;}

else{statements;}

Page 13: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example#include<stdio.h>#include<conio.h>void main(){ int m1,m2,m3; float avg; printf("\nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("\n The average is:%f",avg); printf("\n The Grade is:"); if(avg>=60) {

printf("First class"); }

Page 14: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

else if(avg>=50) {

printf("Second class"); } else if(avg>=35) {

printf("Thrid class"); } else {

printf("Fail"); }

getch();}

Page 15: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the marks:657570The average is:70.000000The Grade is: First class

Page 16: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Looping structure

• It is used to execute some instructions several time based on some condition.– WHILE – Do…WHILE – For

Page 17: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

WHILE Loop

Syntax.

WHILE (condition){

.Body of the loop;

. }

Body of The loop

conditionFalse

True

Page 18: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example#include<stdio.h>#include<conio.h>void main(){ int i=1,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); while(i<=n) {

fact =fact *i; i++; // i=i+1

} printf("\n The value of %d! is:%d",n,fact); getch();}

Page 19: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the Number:3The value of 3! is: 6

Page 20: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

DO…WHILE Loop

Syntaxdo

{

Body of the loop

}while (condition);

Body of The loop

condition

False

True

Page 21: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

for loop

Syntaxfor (initialization; test condition; Increment/Decrement)

{

Body of the loop

}

Page 22: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

for loop

Initialization

condition False

Body of the loop

Inc / Decrement

Page 23: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example#include<stdio.h>#include<conio.h>void main(){ int i,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) {

fact =fact *i; } printf("\n The value of %d! is:%d",n,fact); getch();}

Page 24: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the Number:3The value of 3! is: 6

Page 25: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Nested for loop

Syntaxfor (initi; cond; Inc/Dec)

{

for (initi; cond; Inc/Dec){

Body of the loop

}

}

Page 26: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

CASE structure

Case 1

Case 2

Defaultcase

Switch

Page 27: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

CASE structureSyntaxswitch (expression){case constant 1:

block1;break;

case constant 2:block2;break;..

default :default block;break;

}

Page 28: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example#include<stdio.h>#include<conio.h>void main(){ int i,n; printf("\nEnter the Number:"); scanf("%d",&n);

switch(n) {

case 1:{printf("\n Its in case 1");break;}

Page 29: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

case 2:{printf("\n Its in case 2");break;}

default:{printf("\n Its in default");break;}

} getch();}

Page 30: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the Number:2Its in case 2

Page 31: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

break Statement

• It is used to terminate the loop• When a break statement is encountered

inside a loop, then the loop is terminated.

Page 32: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Loops with break Statement

while(cond){

…………if(cond)break;…………

}

Page 33: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

do{

…………if(cond)break;…………

} while(cond);

Page 34: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

for (initi; condt; Inc/Dec){…………if(cond)break;…………

}

Page 35: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Continue Statement

• When a continue statement is encountered inside a loop, the control is transferred to the beginning.

Page 36: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Loops with continue Statement

while(cond){

…………if(cond)continue;…………

}

Page 37: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

do{

…………if(cond)continue;…………

} while(cond);

Page 38: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

for (initi; condt; Inc/Dec){…………if(cond)continue;…………

}

Page 39: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

goto Statement

• When a goto statement is encountered inside a loop, the control is transferred to the beginning.

Page 40: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Syntax for goto Statement

label:………………………………goto label;…………

Page 41: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

goto label; ……………………………… label:…………

Page 42: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Page 43: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

getchar() Example

#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getchar();

Page 44: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

if(islower(x))putchar(toupper(x));

elseputchar(tolower(x));

getch();}

Output:enter the character:ABCa

Page 45: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

getche() Example

#include <stdio.h>#include <conio.h>void main(){

char c ;clrscr();printf("\nInput a string:");c = getche();

Page 46: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

printf("\nstring is:");putch(c);getch();

}

Output:Input a string:kstring is:k

Page 47: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Getch() Example

#include <stdio.h>#include <conio.h>void main(){

char c;clrscr();printf("\nInput a string:");c = getch();

Page 48: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

printf("\nstring is:");putch(c);getch();

}Output:Input a string:string is:h

Page 49: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

getc Example

#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getc(stdin);

Page 50: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

if(islower(x))putc(toupper(x),stdout);

elseputc(tolower(x),stdout);

getch();}

Output:enter the character:abcA

Page 51: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

gets() Example

#include <stdio.h>#include<conio.h>void main(){

char c[80];clrscr();printf("Input a string:");gets(c);

Page 52: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

printf("The string is:");puts(c);getch();

}

Output:Input a string:qwertyThe string is:qwerty

Page 53: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Example #include<stdio.h> #include<conio.h> void main() {

int a,b,c,n;clrscr();printf("\nEnter the value of a,b:");scanf("%d%d",&a,&b);printf("\nMENU");printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");printf("\nEnter the choice:");scanf("%d",&n);

Page 54: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

switch(n){ case 1:

c=a+b;printf("\nThe result of Addition is:%d",c);break;

case 2:c=a-b;printf("\nThe result of Subtraction is:%d",c);break;

Page 55: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

case 3:c=a*b;printf("\nThe result of Multiplication is:%d",c);break;

case 0:exit(0);break;

}getch();

}

Page 56: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the value of a,b:56MENU1.ADD2.SUB3.MULTIPLY0.EXITEnter the choice:1The result of Addition is:11

Page 57: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Finding Armstrong No#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n,a; printf("\nEnter the number:"); scanf("%d",&n); a=n;

while(n>0){

r=n%10;sum=sum+r*r*r;n=n/10;

}

Page 58: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

if(a==sum){ printf("\nIt is an armstrong number");}else{ printf("\nIt is not an armstrong number");}

getch();}

Page 59: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the number:153It is an armstrong number

Page 60: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Sum of the Digits#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10;

Page 61: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum);}

Page 62: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the no:156sum of the digits is:12

Page 63: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Reverse of a number

#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)

Page 64: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

{ r=n%10; sum=sum*10+r; n=n/10;

} printf("Reverse of the number is:%d",sum); getch();}

Page 65: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the no:567Reverse of the number is:765

Page 66: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Fibonacci Series

#include<stdio.h>#include<conio.h>void main(){int f=0,f1=-1,f2=1,n,i;printf("\nEnter the number:");scanf("%d",&n);

Page 67: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

while(f<n){ f=f1+f2;

f1=f2; f2=f;

printf("\t%d",f);}

getch();}

Page 68: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the number:5 0 1 1 2 3 5

Page 69: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Swapping #include<stdio.h>#include <conio.h>void main ( ){int a,b,c;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);c=a;a=b;b=c;

Page 70: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}

Output:Enter the value of a:5Enter the value of b:4

The value of a is:4The value of b is:5

Page 71: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Swapping without using third variable

#include<stdio.h>#include <conio.h>void main ( ){int a,b;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);

Page 72: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

a=a+b;b=a-b;a=a-b;printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}

Output:Enter the value of a:5Enter the value of b:6

The value of a is:6The value of b is:5

Page 73: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Quadratic Equation#include<stdio.h>#include <conio.h>#include<math.h>void main ( ){int a,b,c,d,r1,r2;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);printf(" \nEnter the value of c:");scanf("%d",&c);d=b*b-4*a*c;

Page 74: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

if(d>=0){

r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);

printf(" \nThe roots are %d,%d",r1,r2);}else{ printf(" \nThe roots are imaginary");}getch( );}

Page 75: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.

Output

Enter the value of a:4

Enter the value of b:5

Enter the value of c:6

The roots are imaginary