# Control Flow

• Decisions - if then else
• More decisions - switch
• Loops - while, do while, for
• Keyword-break
• Keyword-continue

## Decisions - if then

• Parentheses surround the test
• One statement becomes the “then part”
• If more are required, braces must be used

1
2
3
4
5
6
7
8
9
10
11
12
```scanf("%i", &i);

if(i > 0)
{
printf("a positive number was entered\n");
}

if(i < 0)
{
printf("a negative number was entered\n");
i = -i;
}```

## if then else

• An optional else may be added
• One statement by default, if more are required, braces must be used
1
2
3
4
5
6
7
8
```if(i>0)
{
printf("i is positive\n");
}
else
{
printf("i is negative\n");
}```

## Nesting if-s

• else associates with the nearest if

Example :

1
2
3
4
5
6
7
8
9
10
11
12
```int i = 100;
if(i>0)
{
if(i>1000)
{
printf("i is big\n");
}
else
{
printf("i is reasonable\n");
}
}```

Result : i is reasonable

Example :

1
2
3
4
5
6
7
8
9
10
11
12
```int = -20;
if(i>0)
{
if(i>1000)
{
printf("i is big\n");
}
}
else
{
printf("i is negative\n");
}```

Result : i is negative

## switch

C supports a switch for multi-way decision making

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
```switch(c)
{
case ‘a’:  case ‘A’:
printf("area = %.2f\n", r * r * pi);
break;

case ‘c’: case ‘C’:
printf("circuinference — %.2f\n", 2 * r * pi);
break;

case ‘q’:
printf("quit option chosen\n”);
break;

default:
printf ("unknown option chosen\n”);
break;
}```

## while Loop

• The simplest C loop is the while
• Parentheses must surround the condition
• One statement forms the body of the loop
•  Braces must be added if more statements are to be executed

Example:

1
2
3
4
5
```int j = 5;
while(j>0)
{
printf("j = %i\n", j--);
}```

Result :

1
2
3
4
5
```j = 5
j = 4
j = 3
j = 2
j = 1```

## do while

do while guarantees execution at least once
Example :

1
2
3
4
5
6
7
8
```int j = 5;
printf("start\n");
do
{
printf("j = %i\n", j--);
}
while(j>0);
printf("stop\n");```

Result
1
2
3
4
5
6
7
```start
j = 5
j = 4
j = 3
j = 2
j = 1
stop```

Example :
1
2
3
4
5
6
7
8
9
```int j = -10;
printf("start\n");
do
{
printf("j = %i\n", j);
j--;
}
while(j>0);
printf("stop\n");```

Result :
1
2
3
```start
j = -10
stop```

## for Loop

for encapsulates the essential elements of a loop into one statement

1
2
3
4
```for(initial-part; while-condition; update-part)
{
body;
}```

Example :

1
2
3
4
5
```int j;
for(j = 5; j > 0; j--)
{
printf("j = %i\n",j);
}```

Result :

1
2
3
4
5
```j = 5
j = 4
j = 3
j = 2
j = 1```

Example :

1
2
3
4
5
```for(j = 5; j > 0; j--)
{
printf("j  = %i",j);
printf("%s\n", ((j%2==0)?"even","odd"));
}```

Result :

1
2
3
4
5
```j = 5 odd
j = 4 even
j = 3 odd
j = 2 even
j = 1 odd```

## Stepping With for

Unlike some languages, the for loop is not restricted to stepping up or down by 1

1
2
3
4
5
6
7
8
9
10
```#include <math.h>
int main(void)
{
double angle;
for(angle = 0.0; angle < 3.14159; angle  +=  0.2)
{
printf("sine of %.1lf is %.2lf\n”, angle, sin(angle));
return 0;
}
}```

## Extending the for Loop

The initial and update parts may contain multiple comma separated statements

1
2
```int i, j, k;
for(i = 0, j = 5, k = -1; i < 10; i++, j++, k--)```

The initial, condition and update parts may contain no statements at all!

• if you dont want to use initial part than you can use as shown below Example :
1
`for(; i<10; i++; j++; k--)`

• if you dont want to use initial part and update part than you can use as shown below Example :
1
`for(; i<10;)`

• and if you dont want to use initial part,condition and update part than it creates an infinite loop so you can use as shown below Example :
1
`for(;;)`