Control Statement 1
Control Statement 1
1. LEARNING OBJECTIVES
Understanding meaning of a statement
and statement block
• return
PROGRAM CONTROL
STATEMENTS/CONSTRUCTS IN ‘C’
Program Control
Statements/Constructs
Selection/Branching Iteration/Looping
if-else-
if if-else
if
switch break continue goto
OPERATORS
<
! >
!= ==
Operators
<=
||
>=
&& !=
Equality and Logical
RELATIONAL OPERATORS
Operators
Negation !
POINTS TO NOTE
If an expression, involving the relational operator, is true, it is given a value of 1. If
an expression is false, it is given a value of 0.
Similarly, if a numeric expression is used as a test expression, any non-zero value
(including negative) will be considered as true, while a zero value will be
considered as false.
Space can be given between operand and operator (relational or logical) but space
is not allowed between any compound operator like <=, >=, ==, !=. It is also
compiler error to reverse them.
The relational operators have lower precedence than all arithmetic operators.
A FEW EXAMPLES
The following declarations and initializations
are given:
int x=1, y=2, z=3;
Then,
The expression x>=y evaluates to 0 (false).
The expression x+y evaluates to 3 (true).
The expression x=y evaluates to 2 (true).
LOGICAL OPERATORS MAY BE MIXED WITHIN RELATIONAL EXPRESSIONS BUT
ONE MUST ABIDE BY THEIR PRECEDENCE RULES WHICH IS AS FOLLOWS:
(!) NOT
operator && AND
operator || OR
operator
OPERATOR SEMANTICS
Operators Associativity
() ++ (postfix) -- (postfix) left to right
+ (unary) - (unary) right to left
++ (prefix) -- (prefix) * / % left to right
+- left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
?: right to left
=+=-=*=/= right to left
, (comma operator) left to right
CONDITIONAL EXECUTION
AND SELECTION
Selection Statements
Multi-way decisions
stmtT
WRITE A PROGRAM THAT
PRINTS THE LARGEST AMONG
THREE
Algorithm
1. START
NUMBERS.
C Program
#include <stdio.h>
2. PRINT “ENTER THREE int main()
NUMBERS” {
int a, b, c, max;
3. INPUT A, B, C printf(“\nEnter 3 numbers”);
4. MAX=A scanf(“%d %d %d”, &a, &b, &c);
max=a;
5. IF B>MAX THEN MAX=B if(b>max)
6. IF C>MAX THEN MAX=C max=b;
if(c>max)
7. PRINT “LARGEST
max=c;
NUMBER IS”, MAX
printf(“Largest No is %d”, max);
8. STOP return 0;
}