Control Statements
Control Statements
Control
Statements
statement 3;
• In above syntax, if condition is true only then the
statements within the block are executed otherwise
How do we express the condition in C?
• By using C’s ‘relational’ operators
- The relational operators allow us to compare two values to
see whether they are equal to each other, unequal, or whether one is
greater than the other.
Precedence and Order of evaluation
this expression is true if associativity
x<y x is less than y Left to right
x>y x is greater than y
x <= y x is less than or equal to y
x >= y x is greater than or equal to
y
x == y x is equal to y Left to right
x != y x is not equal to y
• Flowchart /* Program to check whether a no. is even */
# include<stdio.h>
# include<conio.h>
void main()
False {
Condition int num;
clrscr();
True
printf(“enter the number”);
Block of
scanf(“%d”,&num)
if
if(num%2==0)
Next {
statement
printf(“\n Number is even”);
STOP }
printf(“ End of program”);
getch();
}
if – else statement
• In case of if statement, the block of statements is executed only when the
condition is true otherwise the control is transferred to the next statement
following if block.
• But if specific statements are to be executed in both cases (either condition is true
or false) then if – else statement is used.
• In if – else statement a block of statements are executed if the condition is true
but a different block of statements is executed when the condition is false.
• Syntax:
if (condition) False
Test
{ Condition
statement 1;
statement 2; True
if if else if (condition1)
(condition1) (condition1) {
statement 1;
if (condition 3)
{ {
statement 3; else
if if else {
(condition2) (condition2) statement 4;
} if (condition2)
statement
statement 1; 1; statement 2;
Next statement
Switch statement
• Switch is a multi-way decision making statement which selects one
of the several alternatives based on the value of single variable or
expression.
• It is mainly used to replace multiple if-else-if statement.
• The if-else-if statement causes performance degradation as several
conditions need to be evaluated before a particular condition is
satisfied.
• Syntax: switch (expression)
{
case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]
…………………………………….
default: statement (s)
}
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Operators Type
! Logical NOT
*/% Arithmetic and modulus
+- Arithmetic
< > <= >= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment
The Conditional Operators
The conditional operators ? and : are sometimes called ternary operators since they
take three arguments.
Syntax: expression 1 ? expression 2 : expression 3
– It means: if expression 1 is true (that is, if its value is non-zero), then the value
returned will be expression 2, otherwise, the value returned will be expression 3.
– Example:
or
The limitation of the conditional operators: after the ? or after the : only one C statement can occur.
Solve
1. 5. main()
{
int x=10,y=100%90;
if(x!=y);
2. printf(“x=%dy=%d”,x,y);
}
3.
6. main()
This will give you an error ‘Lvalue {
Required’- Why? int x=3,y=4,z=4;
printf(“ans=%d”,z>=y&&y>=z?1:0);
4. }
If(!a>=400) then?
Solve:
1.
2.
3.
count = 1
is No
count <= 3
Yes
STOP
INPUT
p, n, r
si = p * n * r / 100
PRINT
si
count = count + 1
}while (condition);
false
statement;
Next statement
for loop
• Most versatile and popular of three loop structures.
• Is used in those situations when a programmer knows in
advance the number of times a statement or block will
be executed.
• It contains loop control elements all at one place while
in other loops they are scattered over the program and
are difficult to understand.
• Syntax:-
for (initialization; condition; increment/decrement)
{
Statement( s);
}
The for is a sort of while
is equivalent to:
expr1;
while (expr2) {
statement;
expr3;
}
Various other ways of writing same for loops
Also: c += 1;
Also: c++;
Also: ++c;
/* Preincrementing and postincrementing */
#include <stdio.h>
int main()
{
int c;
c = 5;
printf("%d\n", c);
printf("%d\n",c++); /*post increment*/
printf("%d\n\n", c);
:
continued
c = 5;
printf("%d\n", c);
printf("%d\n",++c); /*pre-
increment*/
printf("%d\n", c);
return 0;
}
Output:
5
5
6
5
6
6
Decrementing
Also: c -= 1;
Also: c--;
Also: --c;
Continue statement
• Like break ,continue statement also skips the remaining
statements of the body of the loop where it is defined but
instead of terminating the loop, the control is transferred
to the beginning of the loop for next iteration.
• The loop continues until the test condition of the loop
become false.
• Syntax: continue;
• E.g. for (m=1;m<=3;m++)
{ Output:
for (n=1;n<=2;n++) 1 2
{ 2 1
if (m==n) 3 1
continue; 3 2
printf(“ m=%d n=%d”);
}
}
goto Statement
• An unconditional control statement that causes the
control to jump to a different location in the program
without checking any condition.
• It is normally used to alter the normal sequence of
program execution by transferring control to some
other part of the program.
• So it is also called jump statement.
• Syntax: goto label;
• Label represents an identifier which is used to label the
destination statement to which the control should be
transferred.
label : statement;
• The goto statement causes the control to be shifted
either in forward direction or in a backward direction .
exit() function