Control Flow in C
Control Flow
The control-flow of a language specify the order in which
computations are performed.
Statements: An expression such as x = 0 or i++ or printf(...)
becomes a statement when it is followed by a semicolon, as in
x = 0;
i++;
printf(...);
In C, the semicolon is a statement terminator, rather than a separator
Blocks or Statement Blocks: In C, any sequence of statement
can be grouped together by enclosing pair of curly braces.
Inside statement block variable declaration is allowed.
There is no semicolon after the right brace that ends a block.
2
Statement Blocks (cont..)
Statement block introduces a new scope in the program.
A scope is part of the program within which a variable remains
defined.
Every function has a function body consisting of s set of one or more
statements, i.e. a statement block, due to this reason , every function
body is confined within a pair of curly braces.
3
Program-1
#include<stdio.h>
main(){
int a=3;
{
int a=10;
printf(“a=%d",a);
}
printf(“na=%d",a);
}
Program-2
#include<stdio.h>
main(){
int a=3;
{
int b=10;
printf(“a=%d",a);
}
printf(“nb=%d",b);
}
a=10
a=3
What will be the output ?
main()
{
int x=40;
{
int x=20;
printf(“x=%d",x);
}
printf(“nx=%d",x);
}
 
4
x=20
x=40
Flow control Statements
Following are some constructs for controlling the flow of
execution in C:
1. if,
2. if – else,
3. Nested if and
4. if – else if
5. switch
6. conditional operator
7. goto
5 08/23/15
IF Statements
Syntax:
if(expression)
statement
• The expression is evaluated; if it is true (that is, if expression has a
non-zero value), statementis executed.
• Note: here statement may be single statement or Statement
Block.Just like
if(expression)
{
stmt 1;
stmt 2;
………..
}
6 08/23/15
IF Statements Example
7 08/23/15
IF Statements Example
8 08/23/15
Since an if tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing
if (expression) instead of if (expression != 0)
in last example if(a%2!=0) can be written as if(a%2)
Following three statements are functionally equevalent
if(x)
if(x!=0)
if(!(x==0))
9 08/23/15
IF - ElSE Statements
if (expression)
statement1
else
statement2
 The expression is evaluated; if it is true (that is, if expression has
a non-zero value), statement1is executed. If it is false (expression
is zero) statement2is executed.
10 08/23/15
IF - ElSE Statements
if (n > 0)
if (a > b)
z = a;
else
z = b;
the else goes to the inner if, as we have shown by indentation. If that isn't what you
want, braces must be used to force the proper association:
if (n > 0) {
if (a > b)
z = a;
}
else
z = b;
11 08/23/15
if - else Example
12 08/23/15
if - else Example
13 08/23/15
NESTED IF Statements
14 08/23/15
NESTED IF Example
15 08/23/15
IF – ELSE IF Statements
16 08/23/15
IF – ELSE IF Statements
This sequence of if statements is the most general way of
writing a multi-way decision.
The expressions are evaluated in order; if an expression is
true, the statement associated with it is executed, and this
terminates the whole chain.
The last else part handles the “none of the above'' or default
case where none of the other conditions is satisfied.
Sometimes there is no explicit action for the default; in that
case the trailing
else can be omitted, or it may be used for error checking to
catch an ``impossible'' condition.
17 08/23/15
IF – ELSE IF Example
18 08/23/15
IF – ELSE IF Example
19 08/23/15
IF – ELSE IF Example
20 08/23/15
Switch Statements
Switch is used to Select one choice (case) from many cases.
21 08/23/15
Switch-Case Example1
22 08/23/15
Switch-Case Example2
23 08/23/15
Switch-Case Related Rules
24 08/23/15
Switch-Case Related Rules
25 08/23/15
Example3
26 08/23/15
What would be the o/p of the following program?
27
main()
{
int i=4;
switch(i)
{
default:
printf(“n a mouse”);
break;
case 1:
printf(“n a rabbit”);
break;
case 2:
printf(“n a tiger”);
break;
case 3:
printf(“n a lion”);
}
}
a mouse
08/23/15
Switch example
28 08/23/15
switch (choice = getchar())
{
case ‘r’ :
case ‘R’: printf(“Red”);
break;
case ‘b’ :
case ‘B’ : printf(“Blue”);
break;
case ‘g’ :
case ‘G’:
printf(“Green”);
break;
default:
printf(“Black”);
}
What would be the o/p of the following program?
29
main()
{
int x=1;
switch(x)
{
printf(“Hello”);
case 1:
printf(“n JUET”);
break;
case 2;
printf(“n Good”);
break;
}
}
Explanation: Though there is no error ,the first printf statement can never be
executed irrespective of the value of x . In other words all the statements in the
switch have to belong to some case or other.
JUET
08/23/15
What will be the output?
Q1:
main()
{
printf(“%d, %d, %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}
Q2:
main()
{
int a=010;
printf(“a=%d”,a);
}
Q3:main(){
float c=3.14;
printf(“%f”,c%2);
} 08/23/1530
4, 8, 10
a=8 // printf(“a=%o”); would print a=10
Similarly printf(“a=%x”); would print a=8( equivalent hex)
Error:Illigal use of floating poit in function main
What will be the output?
Q1:
main()
{int a=5;
a=printf(“Hello”) +printf(“JUET”);
printf(“%d”,a);
}
Q2:main(){
printf(“Hi”);
if(!-1)
printf(“Bye”);
}
Q3:main(){ int a=40000,b=20000;
if(a<b)
printf(“Wow!”);
else printf(“Really!”);
}
08/23/1531
o/p: HelloJUET9
o/p: Hi
o/p: Wow! //Because of integer overflow a
will have 40000-65536=-25536
The Conditional Operator ?:(Ternary
operator)
32 08/23/15
Conditional Operator Example-1
void main()
{
int a;
printf(“Enter a no.”);
scanf(“%d”,&a);
a%2==0?printf(“No. is even”):printf(“No. is odd”);
}
33 08/23/15
Conditional Operator Example-2
34 08/23/15
What will be the output?
main()
{
int a=10,b;
a>=5?b=100:b=200;
printf(“%d”,b);
}
08/23/1535
o/p: 100
Goto Statements
Goto changes the flow of execution without checking any
condition
36 08/23/15
Use of Goto Statements
Nevertheless, there are a few situations where gotos may find a place.
The most common use is to abandon processing in some deeply nested
structure, such as breaking out of two or more loops at once.
The break statement cannot be used directly since it only exits from
the innermost loop. Thus:
for ( ... )
for ( ... ) {
...
if (disaster)
goto error;
}
...
error: // goto will transfer the control to this statement.
37 08/23/15
Some facts about goto
A label has the same form as a variable name, and is followed
by a colon.
It can be attached to any statement in the same function as
the goto.
The scope of a label is the entire function.
With a few exceptions like previous example, code that uses
goto statements is generally harder to understand and its very
difficult to maintain as compare to the code without gotos.
Due to above reason goto is rarely used in programming.
08/23/1538
Some Tips
08/23/1539
What will be the Output
Q1#include<stdio.h>
main(){
int a=10,b=50;
if(a>b);
printf("a is greater than bn");
printf("End of program");
}
Q2:
main(){
int a=55,b=10;
if(a>b)
printf("a is greater than bn");
printf("Very Good!");
else printf("a is smaller than b");
}
08/23/1540
o/p: a is greater than b
End of program
o/p: Error: Misplaced else in function main
What will be the Output
Q3#include<stdio.h>
main(){
float F;
F=20000*3/3.0;
printf(“F=%f“,F);
}
Q4:
main(){
int a=20,b=10;
b=2a;
printf(“a=%dn“,a);
printf(“b=%d“,b);
}
08/23/1541
o/p: F=-1845.333374
o/p:Compilation Error

7 decision-control

  • 1.
  • 2.
    Control Flow The control-flowof a language specify the order in which computations are performed. Statements: An expression such as x = 0 or i++ or printf(...) becomes a statement when it is followed by a semicolon, as in x = 0; i++; printf(...); In C, the semicolon is a statement terminator, rather than a separator Blocks or Statement Blocks: In C, any sequence of statement can be grouped together by enclosing pair of curly braces. Inside statement block variable declaration is allowed. There is no semicolon after the right brace that ends a block. 2
  • 3.
    Statement Blocks (cont..) Statementblock introduces a new scope in the program. A scope is part of the program within which a variable remains defined. Every function has a function body consisting of s set of one or more statements, i.e. a statement block, due to this reason , every function body is confined within a pair of curly braces. 3 Program-1 #include<stdio.h> main(){ int a=3; { int a=10; printf(“a=%d",a); } printf(“na=%d",a); } Program-2 #include<stdio.h> main(){ int a=3; { int b=10; printf(“a=%d",a); } printf(“nb=%d",b); } a=10 a=3
  • 4.
    What will bethe output ? main() { int x=40; { int x=20; printf(“x=%d",x); } printf(“nx=%d",x); }   4 x=20 x=40
  • 5.
    Flow control Statements Followingare some constructs for controlling the flow of execution in C: 1. if, 2. if – else, 3. Nested if and 4. if – else if 5. switch 6. conditional operator 7. goto 5 08/23/15
  • 6.
    IF Statements Syntax: if(expression) statement • Theexpression is evaluated; if it is true (that is, if expression has a non-zero value), statementis executed. • Note: here statement may be single statement or Statement Block.Just like if(expression) { stmt 1; stmt 2; ……….. } 6 08/23/15
  • 7.
  • 8.
  • 9.
    Since an iftests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing if (expression) instead of if (expression != 0) in last example if(a%2!=0) can be written as if(a%2) Following three statements are functionally equevalent if(x) if(x!=0) if(!(x==0)) 9 08/23/15
  • 10.
    IF - ElSEStatements if (expression) statement1 else statement2  The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement1is executed. If it is false (expression is zero) statement2is executed. 10 08/23/15
  • 11.
    IF - ElSEStatements if (n > 0) if (a > b) z = a; else z = b; the else goes to the inner if, as we have shown by indentation. If that isn't what you want, braces must be used to force the proper association: if (n > 0) { if (a > b) z = a; } else z = b; 11 08/23/15
  • 12.
    if - elseExample 12 08/23/15
  • 13.
    if - elseExample 13 08/23/15
  • 14.
  • 15.
  • 16.
    IF – ELSEIF Statements 16 08/23/15
  • 17.
    IF – ELSEIF Statements This sequence of if statements is the most general way of writing a multi-way decision. The expressions are evaluated in order; if an expression is true, the statement associated with it is executed, and this terminates the whole chain. The last else part handles the “none of the above'' or default case where none of the other conditions is satisfied. Sometimes there is no explicit action for the default; in that case the trailing else can be omitted, or it may be used for error checking to catch an ``impossible'' condition. 17 08/23/15
  • 18.
    IF – ELSEIF Example 18 08/23/15
  • 19.
    IF – ELSEIF Example 19 08/23/15
  • 20.
    IF – ELSEIF Example 20 08/23/15
  • 21.
    Switch Statements Switch isused to Select one choice (case) from many cases. 21 08/23/15
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    What would bethe o/p of the following program? 27 main() { int i=4; switch(i) { default: printf(“n a mouse”); break; case 1: printf(“n a rabbit”); break; case 2: printf(“n a tiger”); break; case 3: printf(“n a lion”); } } a mouse 08/23/15
  • 28.
    Switch example 28 08/23/15 switch(choice = getchar()) { case ‘r’ : case ‘R’: printf(“Red”); break; case ‘b’ : case ‘B’ : printf(“Blue”); break; case ‘g’ : case ‘G’: printf(“Green”); break; default: printf(“Black”); }
  • 29.
    What would bethe o/p of the following program? 29 main() { int x=1; switch(x) { printf(“Hello”); case 1: printf(“n JUET”); break; case 2; printf(“n Good”); break; } } Explanation: Though there is no error ,the first printf statement can never be executed irrespective of the value of x . In other words all the statements in the switch have to belong to some case or other. JUET 08/23/15
  • 30.
    What will bethe output? Q1: main() { printf(“%d, %d, %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l)); } Q2: main() { int a=010; printf(“a=%d”,a); } Q3:main(){ float c=3.14; printf(“%f”,c%2); } 08/23/1530 4, 8, 10 a=8 // printf(“a=%o”); would print a=10 Similarly printf(“a=%x”); would print a=8( equivalent hex) Error:Illigal use of floating poit in function main
  • 31.
    What will bethe output? Q1: main() {int a=5; a=printf(“Hello”) +printf(“JUET”); printf(“%d”,a); } Q2:main(){ printf(“Hi”); if(!-1) printf(“Bye”); } Q3:main(){ int a=40000,b=20000; if(a<b) printf(“Wow!”); else printf(“Really!”); } 08/23/1531 o/p: HelloJUET9 o/p: Hi o/p: Wow! //Because of integer overflow a will have 40000-65536=-25536
  • 32.
    The Conditional Operator?:(Ternary operator) 32 08/23/15
  • 33.
    Conditional Operator Example-1 voidmain() { int a; printf(“Enter a no.”); scanf(“%d”,&a); a%2==0?printf(“No. is even”):printf(“No. is odd”); } 33 08/23/15
  • 34.
  • 35.
    What will bethe output? main() { int a=10,b; a>=5?b=100:b=200; printf(“%d”,b); } 08/23/1535 o/p: 100
  • 36.
    Goto Statements Goto changesthe flow of execution without checking any condition 36 08/23/15
  • 37.
    Use of GotoStatements Nevertheless, there are a few situations where gotos may find a place. The most common use is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus: for ( ... ) for ( ... ) { ... if (disaster) goto error; } ... error: // goto will transfer the control to this statement. 37 08/23/15
  • 38.
    Some facts aboutgoto A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function. With a few exceptions like previous example, code that uses goto statements is generally harder to understand and its very difficult to maintain as compare to the code without gotos. Due to above reason goto is rarely used in programming. 08/23/1538
  • 39.
  • 40.
    What will bethe Output Q1#include<stdio.h> main(){ int a=10,b=50; if(a>b); printf("a is greater than bn"); printf("End of program"); } Q2: main(){ int a=55,b=10; if(a>b) printf("a is greater than bn"); printf("Very Good!"); else printf("a is smaller than b"); } 08/23/1540 o/p: a is greater than b End of program o/p: Error: Misplaced else in function main
  • 41.
    What will bethe Output Q3#include<stdio.h> main(){ float F; F=20000*3/3.0; printf(“F=%f“,F); } Q4: main(){ int a=20,b=10; b=2a; printf(“a=%dn“,a); printf(“b=%d“,b); } 08/23/1541 o/p: F=-1845.333374 o/p:Compilation Error