Upcoming SlideShare
×

# C Prog. - Decision & Loop Controls

3,121 views

Published on

4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,121
On SlideShare
0
From Embeds
0
Number of Embeds
833
Actions
Shares
0
221
0
Likes
4
Embeds 0
No embeds

No notes for slide

### C Prog. - Decision & Loop Controls

1. 1. C Programming - Decision & Loop Control Organized By: Vinay Arora Assistant Professor, CSED Thapar University, Patiala
2. 2. Decision Control The if statement The if-else statement The conditional operators Vinay Arora CSED
3. 3. Forms of if Vinay Arora CSED
4. 4. Forms of if (contd.) Vinay Arora CSED
5. 5. Relational Operator Vinay Arora CSED
6. 6. Demonstrating - If Vinay Arora CSED
7. 7. Flowchart Vinay Arora CSED
8. 8. Demonstrating - If Vinay Arora CSED
9. 9. Flowchart Vinay Arora CSED
10. 10. Expression in Conditional Part Vinay Arora CSED
11. 11. Multiple statements within if Vinay Arora CSED
12. 12. Demonstrating If - Else Vinay Arora CSED
13. 13. Flowchart If-Else Vinay Arora CSED
14. 14. Logical Operators with If-Else Vinay Arora CSED
15. 15. Logical Operators with If-Else Vinay Arora CSED
16. 16. Else-if ladder Vinay Arora CSED
17. 17. Logical Operators with If-Else Vinay Arora CSED
18. 18. Smallest amongst 3 nos. Vinay Arora CSED
19. 19. Smallest amongst 3 nos. Vinay Arora CSED
20. 20. Program using Logical OR, elseif Vinay Arora CSED
21. 21. Calculate Salary as per following table Vinay Arora CSED
22. 22. && - Logical AND C allows usage of three logical operators, namely, &&, || and ! These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. Don’t use the single symbol | and &. These single symbols also have a meaning. The first two operators, && and ||, allow two or more conditions to be combined in an if statement. Vinay Arora CSED
23. 23. && - Logical AND (C Program) Vinay Arora CSED
24. 24. Vinay Arora CSED
25. 25. Vinay Arora CSED
26. 26. | | - Logical OR (C Program) Vinay Arora CSED
27. 27. Vinay Arora CSED
28. 28. Vinay Arora CSED
29. 29. ! - Logical NOT This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates to zero then on applying ! operator to it makes it 1, a non-zero value. Vinay Arora CSED
30. 30. ! - Logical NOT (C Program) Vinay Arora CSED
31. 31. Vinay Arora CSED
32. 32. Vinay Arora CSED
33. 33. &&, ||, ! Operator Vinay Arora CSED
34. 34. ? : - Conditional Operator The conditional operators ? and : are sometimes called Ternary Operators since they take three arguments. They form a kind of foreshortened if-then-else. Their general form is, expression 1 ? expression 2 : expression 3 If expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3. Vinay Arora CSED
35. 35. Conditional Operator (C Program) Vinay Arora CSED
36. 36. Vinay Arora CSED
37. 37. Vinay Arora CSED
38. 38. goto Statement goto is used to switch the control flow. In a difficult programming situation it seems so easy to use a goto to take the control. In most of the scenarios use of goto is depreciated. goto can be replaced by if-else, switch, for. Vinay Arora CSED
39. 39. goto Statement - Program Vinay Arora CSED
40. 40. Vinay Arora CSED
41. 41. Vinay Arora CSED
42. 42. switch Statement The control statement that allows us to make a decision from the number of choices is called a switch. The integer expression following the keyword switch is any C expression that will yield an integer value. Vinay Arora CSED
43. 43. switch Statement - Flowchart Vinay Arora CSED
44. 44. switch Statement - Program Vinay Arora CSED
45. 45. Vinay Arora CSED
46. 46. Vinay Arora CSED
47. 47. Loops Repetitive instructions is done through a Loop control instruction. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. There are three methods by way of which we can repeat a part of a program. They are: (a) Using a while statement (b) Using a do-while statement (c) Using a for statement Vinay Arora CSED
48. 48. while Loop - Flowchart Vinay Arora CSED
49. 49. while Loop – general form Vinay Arora CSED
50. 50. while Loop – forms of conditions Vinay Arora CSED
51. 51. while Loop – C Program Vinay Arora CSED
52. 52. Vinay Arora CSED
53. 53. While Loop – Program 1 //Program to demonstrate simple while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
54. 54. Output Vinay Arora CSED
55. 55. While Loop – Program 2 //Program to demonstrate simple while loop with decrement operator #include<stdio.h> #include<conio.h> void main() { int i=5; clrscr(); while (i>=1) { printf("%dn",i); i=i-1; } getch(); } Vinay Arora CSED
56. 56. Output Vinay Arora CSED
57. 57. While Loop – Program 3 /* Program to demonstrate simple while loop taking incremental value as float */ #include<stdio.h> #include<conio.h> void main() { float i=10.0; clrscr(); while (i<=10.5) { printf("nCivil Engineering at Thapar"); i=i+.1; } getch(); } Vinay Arora CSED
58. 58. Output Vinay Arora CSED
59. 59. While Loop – Program 4 //Demonstrating simple while loop with integer value out of range #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=32767) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
60. 60. Output – Infinite Loop Vinay Arora CSED
61. 61. While Loop – Program 5 //Program to demonstrate simple while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10); { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
62. 62. Output Vinay Arora CSED
63. 63. While Loop – Program 6 //Program to demonstrate post increment operator in while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { printf("%dn",i); i=i++; } getch(); } Vinay Arora CSED
64. 64. Output Vinay Arora CSED
65. 65. While Loop – Program 7 //Program to demonstrate compound assignment operator within while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=5) { printf("%dn",i); i+=1; } getch(); } Vinay Arora CSED
66. 66. Output Vinay Arora CSED
67. 67. While Loop – Program 8 //Program to demonstrate post increment operator with while loop #include<stdio.h> #include<conio.h> void main() { int i=0; clrscr(); while (i++ < 5) { printf("%dn",i); } getch(); } Vinay Arora CSED
68. 68. Output Vinay Arora CSED
69. 69. While Loop – Program 9 //Program to find out even numbers between 1-10 #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { if (i%2==0) printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
70. 70. Output Vinay Arora CSED
71. 71. Do-while Loop While Do while Vinay Arora CSED
72. 72. Do-while Loop – Program 10 //Program to demonstrate DO-WHILE loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); /* while(i<1) { printf("hello i am at Thapar"); } */ do { printf("hello i am at Thapar"); } while(i<1); getch(); } Vinay Arora CSED
73. 73. Output Vinay Arora CSED
74. 74. Program Transformation Unary Post Increment Operator Vinay Arora CSED
75. 75. Program Transformation Compound Assignment Operator Vinay Arora CSED
76. 76. For Loop The for allows us to specify three things about a loop in a single line: Vinay Arora CSED
77. 77. For Loop (Program-1) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=1; i<=10; i=i+1) printf("%dn",i); getch(); } Vinay Arora CSED
78. 78. For Loop (Program-1 Output) Vinay Arora CSED
79. 79. For Loop (Program-2) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=1; i<=10;) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
80. 80. For Loop (Program-2 Output) Vinay Arora CSED
81. 81. For Loop (Program-3) //Program to demonstrate simple For loop //Print numbers from 1 to 5 #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); for (;i<=5;i=i+1) { printf("%dn",i); } getch(); } Vinay Arora CSED
82. 82. For Loop (Program-3 Output) Vinay Arora CSED
83. 83. For Loop (Program-4) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); for (;i<=5;) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
84. 84. For Loop (Program-4 Output) Vinay Arora CSED
85. 85. For Loop (Program-5) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=0;i++<5;) { printf("%dn",i); } getch(); } Vinay Arora CSED
86. 86. For Loop (Program-5 Output) Vinay Arora CSED
87. 87. For Loop (Program-6) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=0;++i<5;) { printf("%dn",i); } getch(); } Vinay Arora CSED
88. 88. For Loop (Program-6 Output) Vinay Arora CSED
89. 89. For Loop (Program-7) //Program to demonstrate NESTED For loop #include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for (i=1;i<=5;i=i+1) { printf("n"); for (j=1;j<=i;j=j+1) { printf(" *"); } } getch(); } Vinay Arora CSED
90. 90. For Loop (Program-7 Output) Vinay Arora CSED
91. 91. Thnx… Vinay Arora CSED