The document discusses control flow in C programming. It covers various control flow statements like if-else, switch case, loops etc. Key points include:
- Control flow specifies the order of computations. Statements are terminated with semicolons. Blocks of statements are grouped with curly braces.
- if-else and nested if statements execute code based on expression evaluations. if-else if provides multi-way decisions.
- switch-case provides a way to select one choice from many cases based on a variable's value.
- Loops like for, while, do-while repeat a block of code.
- The goto statement changes execution flow without checking conditions.
-
2. 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
3. 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
4. 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
5. 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
6. 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
9. 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
10. 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
11. 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
17. 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
27. 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
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 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
30. 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
31. 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
37. 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
38. 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
40. 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
41. 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