Sei sulla pagina 1di 15

Vocational crash course on

C with introduction to embedded C


Chapter 4 : Control Statement &
Storage class Specifier

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)

Time table chart


S.No.

Topic

Date

Total Hours
TH (hrs.)

PR(hrs.)

1.

Introduction

0.5

1.

Basic Structure

0.5

2.

C Fundamentals

3.

I/O Functions

4.

Control statements

5.

Arrays

6.

String handling function

7.

Functions

8.

Structure & Unions

9.

Pointers

10.

Dynamic Memory Allocation

11.

File handling

12.

General Interview Questions

13.

Introduction to Embedded C

40

20

TOTAL (12 days,10th Dec 2014 to 23rd Dec 2014)

Control Statements

Decision Control
If statements
If else statements
Nested if statements

Case Control

Loop Control
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

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

while loop
while (condition)
{
statements;
}

Where,
exp1 variable initialization
( Example: i=0, j=2, k=3 )

Where,
condition might be a>5,
i<10

do while loop
do
{
statements;
}while (condition);

exp2 condition checking


( Example: i>5, j<3, k=3 )
exp3
increment/decrement
( Example: ++i, j, ++k )

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 :

while Loop :

#include <stdio.h> #include <stdio.h>


int main()
int main()
{
{
int i=3;
int i;
while(i<10)
for(i=0;i<10;i++)
{
{
printf("%d\n",i);
printf("%d ",i);
i++;
}
}
}
}
output :
0123456789

output :
3456789

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
Value of
Value of
Value of

i
i
i
i

is
is
is
is

1
2
3
4

Case Control

1. switch case statement

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

Case Control
2. break statement
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:
01234
Coming out of for loop when i = 5

Case Control
3. Continue statement
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:
01234
Skipping 5 from display using continue statement
Skipping 6 from display using continue statement
789

Case Control
4. goto statement
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:
01234
We are using goto statement when i = 5
Now, we are inside label name hai

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 ??????

Potrebbero piacerti anche