Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration."— Presentation transcript:

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

2 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.

3 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

4 IF Statement Syntax IF (condition is true) { Statements; } If condition False True Statements

5 Example #include void main ( ) { int a; clrscr( ); printf("\nEnter the number:"); scanf("%d",&a); if(a>10) { printf(" \n a is greater than 10"); } getch( ); }

6 Output Enter the number: 12 a is greater than 10

7 IF…ELSE Statement Syntax IF (condition) { True statements; } ELSE { False statements; } If Condition True False True statements False statements

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

9 NESTED IF… ELSE If Condition 2 True False True statements False statements If Condition 1 False Statements True

10 NESTED IF… ELSE Syntax IF (condition1) { IF (condition2) { True statements; } ELSE { False statements; } ELSE { False statements; }

11 IF…ELSE LADDER Condition 1 Statements Condition 2 Statements Condition 3 Statements TRUE FALSE

12 IF…ELSE LADDER Syntax IF (condition1) { statements; } else if (condition2) { statements; } else if (condition3) { statements; } else { statements; }

13 Example #include 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"); }

14 else if(avg>=50) { printf("Second class"); } else if(avg>=35) { printf("Thrid class"); } else { printf("Fail"); } getch(); }

15 Output Enter the marks:65 75 70 The average is:70.000000 The Grade is: First class

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

17 WHILE Loop Syntax. WHILE (condition) {. Body of the loop;. } Body of The loop condition False True

18 Example #include 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(); }

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

20 DO…WHILE Loop Syntax do { Body of the loop }while (condition); Body of The loop condition False True

21 for loop Syntax for (initialization; test condition; Increment/Decrement) { Body of the loop }

22 for loop Initialization condition False Body of the loop Inc / Decrement

23 Example #include 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(); }

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

25 Nested for loop Syntax for (initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop }

26 CASE structure Case 1 Case 2 Default case Switch

27 CASE structure Syntax switch (expression) { case constant 1: block1; break; case constant 2: block2; break;. default : default block; break; }

28 Example #include void main() { int i,n; printf("\nEnter the Number:"); scanf("%d",&n); switch(n) { case 1: { printf("\n Its in case 1"); break; }

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

30 Output Enter the Number:2 Its in case 2

31 break Statement It is used to terminate the loop When a break statement is encountered inside a loop, then the loop is terminated.

32 Loops with break Statement while(cond) { ………… if(cond) break; ………… }

33 do { ………… if(cond) break; ………… } while(cond);

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

35 Continue Statement When a continue statement is encountered inside a loop, the control is transferred to the beginning.

36 Loops with continue Statement while(cond) { ………… if(cond) continue; ………… }

37 do { ………… if(cond) continue; ………… } while(cond);

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

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

40 Syntax for goto Statement label: ………… goto label; …………

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

42

43 getchar() Example #include void main() { char x; printf("enter the character:"); x=getchar();

44 if(islower(x)) putchar(toupper(x)); else putchar(tolower(x)); getch(); } Output: enter the character:ABC a

45 getche() Example #include void main() { char c ; clrscr(); printf("\nInput a string:"); c = getche();

46 printf("\nstring is:"); putch(c); getch(); } Output: Input a string:k string is:k

47 Getch() Example #include void main() { char c; clrscr(); printf("\nInput a string:"); c = getch();

48 printf("\nstring is:"); putch(c); getch(); } Output: Input a string: string is:h

49 getc Example #include void main() { char x; printf("enter the character:"); x=getc(stdin);

50 if(islower(x)) putc(toupper(x),stdout); else putc(tolower(x),stdout); getch(); } Output: enter the character:abc A

51 gets() Example #include void main() { char c[80]; clrscr(); printf("Input a string:"); gets(c);

52 printf("The string is:"); puts(c); getch(); } Output: Input a string:qwerty The string is:qwerty

53 Example #include 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);

54 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;

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

56 Output Enter the value of a,b:5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 0.EXIT Enter the choice:1 The result of Addition is:11

57 Finding Armstrong No #include 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; }

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

59 Output Enter the number:153 It is an armstrong number

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

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

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

63 Reverse of a number #include void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)

64 { r=n%10; sum=sum*10+r; n=n/10; } printf("Reverse of the number is:%d",sum); getch(); }

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

66 Fibonacci Series #include void main() { int f=0,f1=-1,f2=1,n,i; printf("\nEnter the number:"); scanf("%d",&n);

67 while(f<n) { f=f1+f2; f1=f2; f2=f; printf("\t%d",f); } getch(); }

68 Output Enter the number:5 0 1 1 2 3 5

69 Swapping #include 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;

70 printf(" \nThe value of a is:%d",a); printf(" \nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:4 The value of a is:4 The value of b is:5

71 Swapping without using third variable #include void main ( ) { int a,b; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b);

72 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:5 Enter the value of b:6 The value of a is:6 The value of b is:5

73 Quadratic Equation #include 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;

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

75 Output Enter the value of a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary


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

Similar presentations


Ads by Google