chapter 4_control statement
DESCRIPTION
Control StatementsTRANSCRIPT
Vocational crash course on “C with introduction to embedded C
Course instructor : Prof. Kumar Anand Singh (Asst. Professor, EC, MEFGI)
Course assistances:
Prof. Hetal Dave (Asst. Professor, EC, MEFGI)
Mr. Saumil Vora (Student, PG-VLSI, 1st year)
Chapter 4 : Control Statement & Storage class Specifier
Time table chart
S.No. Topic Date Total Hours
TH (hrs.) PR(hrs.)
1. Introduction 1 0.5
1. Basic Structure 1 0.5
2. C Fundamentals 4 2
3. I/O Functions 2 1
4. Control statements 4 2
5. Arrays 4 2
6. String handling function 2 1
7. Functions 2 1
8. Structure & Unions 4 2
9. Pointers 4 2
10. Dynamic Memory Allocation 2 1
11. File handling 2 1
12. General Interview Questions 4 2
13. Introduction to Embedded C 4 2
TOTAL (12 days,10th Dec 2014 to 23rd Dec 2014) 40 20
Control Statements
Decision Control
Loop Control
Case Control
• If statements • If else statements • Nested if statements
• for • while • do-while
• switch • break • continue • goto
Decision Control
Group of statements are executed when condition is true. If condition is false, then else part statements are executed.
if statement :
Syntax : if (condition) { Statements; }
int main()
{
int m=40,n=40;
if (m == n)
{
printf("m & n are equal");
}
}
output :
m & n are equal
if…else statement : Syntax : if (condition) { Statement1; Statement2; } else { Statement3; Statement4; } int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m & n r not equal"); } }
nested if statement Syntax : if(condition1) { Statement1; } else_ if(condition2) { Statement2; } else Statement 3;
Loop Control • Used to perform looping operations until the given condition is true. • Control comes out of the loop statements once condition becomes false
for loop for (exp1; exp2; expr3) { statements; } Where, exp1 – variable initialization ( Example: i=0, j=2, k=3 ) exp2 – condition checking ( Example: i>5, j<3, k=3 ) exp3 – increment/decrement ( Example: ++i, j–, ++k )
while loop while (condition) { statements; } Where, condition might be a>5, i<10
do while loop do { statements; }while (condition);
• Usually not used as loop is executed irrespective of the condition for first time. • After executing while loop for first time, then condition is checked.
for Loop :
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d ",i);
}
}
output :
0 1 2 3 4 5 6 7 8 9
while Loop : #include <stdio.h> int main() { int i=3; while(i<10) { printf("%d\n",i); i++; } } output : 3 4 5 6 7 8 9
do while Loop : #include <stdio.h> int main() { int i=1; do { printf("Value of i is %d\n",i); i++; }while(i<=4 && i>=2); } output : Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
Case Control int main () { int value = 3; switch(value) { case 1: printf(“Value is 1 \n” ); break; case 2: printf(“Value is 2 \n” ); break; case 3: printf(“Value is 3 \n” ); break; case 4: printf(“Value is 4 \n” ); break; default : printf(“Value is other than 1,2,3,4 \n” ); } return 0; } output: Value is 3
1. switch case statement
Case Control
int main() { int i; for(i=0;i<10;i++) { if(i==5) { printf(“\nComing out of for loop when i = 5″); break; } printf(“%d “,i); } } Output: 0 1 2 3 4 Coming out of for loop when i = 5
2. break statement
Case Control
int main() { int i; for(i=0;i<10;i++) { if(i==5 || i==6) { printf(“\nSkipping %d from display using ” \“continue statement \n”,i); continue; } printf(“%d “,i); } } Output: 0 1 2 3 4 Skipping 5 from display using continue statement Skipping 6 from display using continue statement 7 8 9
3. Continue statement
Case Control
int main() { int i; for(i=0;i<10;i++) { if(i==5) { printf(“\nWe are using goto statement when i = 5″); goto HAI; } printf(“%d “,i); } HAI : printf(“\nNow, we are inside label name \”hai\” \n”); } Output: 0 1 2 3 4 We are using goto statement when i = 5 Now, we are inside label name “hai”
4. goto statement
Different shapes and Pyramids using for loop :
for(i=0;i<10;i++) { for(j=0;j<10;j++) printf(“Hello”); }
for(i=0;i<10;i++) { for(j=0;j<i;j++) printf(“Hello”); }
for(i=10;i>0;i--) { for(j=0;j<10;j++) printf(“Hello”); }
for(i=10;i>0;i--) { for(j=0;j<i;j++) printf(“Hello”); }
for(i=5;i>0;i--) { for(j=0;j<i;j++) printf(“*”); printf(“\n”); }
for(i=0;i<5;i++) { for(j=0;j<i;j++) printf(“*”); printf(“\n”); }
Different shapes and Pyramids using for loop :
i=0; for(;;) { if(i<5) for(j=0;j<10;j++); printf(“i”); i++; printf(“\n”); }
i=0; for(;i<5;) { for(j=0;j<i;j++) printf(“%d”,i); i++; }
for(i=5;i>0;i--) { for(j=0;j<i;j++) printf(“*\n”); }
i=0; for(;i<5;) { for(j=0;j<i;j++) printf(“%d”,j); i++; }
Different shapes and Pyramids using for loop :
for (i=1; i <= 6; i++) { for (j=i; j<6; j++) { printf(" "); } for (k=1; k <= i; k++) { printf("*"); printf(" "); } printf("\n");
* * * * * * * * * * * * * * * * * * * * *
Problems ??????