3 Control Statements
3 Control Statements
‘C’
Control Statements
• Selection Statements
–Using if and if...else
–Nested if Statements
–Using switch Statements
–Conditional Operator
• Repetition Statements
–Looping: while, do-while, and for
–Nested loops
–Using break and continue
Selection Statements
• if Statements
• switch Statements
• Conditional Operators
if Statements
if (Condition) true
Condition ?
{
statement(s); false
statement1
}
statement2
Example:
if (i > 0) statement3
{
printf("i = %d “, i );
}
Caution
Adding a semicolon at the end of an if clause is a
common mistake.
if (radius >= 0); Wrong
{
area = radius*radius*PI;
printf ("The area for the circle
of radius %d is =%d“, radius,area);
}
This mistake is hard to find, because it is neither
a compilation error nor a runtime error, it is a logic error.
This error often occurs when you use the next-line block
style.
The if...else Statement
if (condition)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}
if...else Example
if (radius >= 0)
{
area = radius*radius*PI;
printf("The area for the circle of radius
%d is =%d" , radius, area);
}
else
{
printf(“Radius can not be Negative ");
}
Multiple Alternative if Statements
Other combination is :
int i = 1; int j = 2; int k = 3;
if (i < j)
{
if (i > k)
printf("A");
}
else
printf("B");
switch Statements
switch (variable-name)
{
case value1:
Execute this;
break;
case value2:
Execute this;
break;
case valueN:
Execute this;
break;
default:
Execute this;
}
switch Statement Flow Chart
default
value1
Variable
value2
valueN
Next
Statement
switch Statement Rules
The switch-expression must yield a value of char or
int type and must always be enclosed in parentheses.
The value1, ..., and valueN must have the same data type
as the value of the switch-expression. The resulting
statements in the case statement are executed when the
value in the case statement matches the value of the
switch-expression. (The case statements are executed in
sequential order.)
switch (num) {
case 7: rate = 7.25;
case 15: rate = 8.50;
case 30: rate = 9.0;
default: printlf("Wrong number of years");
}
Example: switch-case
int w = 20;
switch(w)
{
case 10: printf(“First”);
break;
case 20: printf(“Second”);
break;
case 30: printf(“Third”);
break;
default: printf(“Wrong value…”);
}
Conditional Operator
(condition) ? exp1 : exp2
if (x > 0) y = 1
else y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
Ternary operator
Conditional Operator
if (num % 2 == 0)
printf(“%d is even”,num);
else
printf(“%d is odd”,num);
(num%2==0)?printf(“even”):printf(“odd”);
Repetitions
9 while Loops
9 do-while Loops
9 for Loops
9 break and continue
while Loop Flow Chart
while (continuation-condition)
{
false
// loop-body; Continuation
condition?
}
true
Statement(s)
Next
Statement
while Loop Flow Chart, cont.
i = 0;
false
(i < 100)
int i = 0;
while (i < 100)
true
{
printf("Welcome to C!"); printf("Welcome to C!");
i++;
i++;
}
Next
Statement
do-while Loop
Statement(s)
true
Continue
condition?
do
{ false
Next
// Loop body; Statement
} while (continue-condition);
for Loop
for (initialization; condition; increment/decrement)
{
//loop body;
}
Initialization
false
Increment Continuation
Decrement condition?
true
Statement(s)
(loop-body)
Next
Statement
for Loop Example
i=0
false
i<100?
i++
true
Wrong
for (int i=0; i<10; i++);
{
printf("i is %d“,i);
}
Caution, cont.
Similarly, the following loop is also wrong:
int i=0;
while (i<10); Wrong
{
printf("i is %d“,i);
i++;
}
In the case of the do loop, the following
semicolon is needed to end the loop.
int i=0;
do
{
printf("i is %d“,i);
i++; Correct
} while (i<10);
Which Loop to Use?
The three forms of loop statements, while, do, and
for, are expressively equivalent; that is, you can write a
loop in any of these three forms.
It is recommend that you use the one that is most
intuitive and comfortable for you. In general, a for loop
may be used if the number of repetitions is known, as, for
example, when you need to print a message 100 times. A
while loop may be used if the number of repetitions is not
known, as in the case of reading the numbers until the
input is 0. A do-while loop can be used to replace a while
loop if the loop body has to be executed before testing the
continuation condition.
The break Keyword
Continuation false
condition?
true
Statement(s)
break
Statement(s)
Next
Statement
Example: break statement
int a = 10;
while( a >= 0 )
{
printf(“\nValue of a = %d”,a);
a--;
if(a==5)
break;
}
Output:
Value of a = 10
Value of a = 9
Value of a = 8
Value of a = 7
Value of a = 6
The continue Keyword
false
Continue
condition?
true
Statement(s)
continue
Statement(s)
Next
Statement
Example: continue statement
int a = 6;
while( a >= 0 )
{
a--;
if(a==3)
continue;
printf(“\nValue of a = %d”,a);
}
Output:
Value of a = 5
Value of a = 4
Value of a = 2
Value of a = 1
Value of a = 0
Test your skills…
If – else construct