Iteration and Loops
Iteration and Loops
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:
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 NUMBERS.
Algorithm C Program
1. START #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);
5. IF B>MAX THEN MAX=B max=a;
if(b>max)
6. IF C>MAX THEN MAX=C
max=b;
7. PRINT “LARGEST if(c>max)
NUMBER IS”, MAX max=c;
8. STOP printf(“Largest No is %d”, max);
return 0;
}
TWO-WAY DECISIONS USING IF-ELSE STATEMENT
if(TestExpr) TestExpr
stmtT;
else
stmtF;
stmtT stmtF
WRITE A PROGRAM THAT PRINTS THE LARGEST
AMONG THREE NUMBERS.
Algorithm C Program
1. START #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);
5. IF B>MAX THEN MAX=B max=a;
if(b>max)
6. IF C>MAX THEN MAX=C max=b;
7. PRINT “LARGEST if(c>max)
NUMBER IS”, MAX max=c;
printf(“Largest No is %d”, max);
8. STOP
return 0;
}
MULTI-WAY DECISIONS
if(TestExpr1)
switch(expr)
stmtT1; {
else if(TestExpr2) case constant1: stmtList1;
stmtT2; break;
else if(TestExpr3) case constant2: stmtList2;
break;
stmtT3;
case constant3: stmtList3;
.. . break;
else if(TestExprN) ………………………….
stmtTN; ………………………….
else default: stmtListn;
}
stmtF;
TestExpr
TestExpr
2
TestExpr3
stmtT1
TestExpr
stmtT2 N
stmtT3
stmtTF
stmtTN
THE FOLLOWING PROGRAM CHECKS WHETHER A NUMBER
GIVEN BY THE USER IS ZERO, POSITIVE, OR NEGATIVE
#include <stdio.h>
int main()
{
int x;
printf(“\n ENTER THE NUMBER:”);
scanf(“%d”, &x);
if(x > 0)
printf(“x is positive \n”);
else if(x == 0)
printf(“x is zero \n”);
else
printf(“x is negative \n”);
return 0;
}
NESTED IF
float x;
x = 0.0;
while(x != 1.1)
{
x = x + 0.1;
printf(“1.1 minus %f equals %.20g\n”, x, 1.1 -x);
}
The above loop never terminates on many computers, because 0.1 cannot
be accurately represented using binary numbers.
Never test floating point numbers for exact equality, especially in loops.
The correct way to make the test is to see if the two numbers are
‘approximately equal’.
“FOR” CONSTRUCT
The general form of the for statement is as follows:
#include <stdio.h>
int main()
{
int n, s=0, r;
printf(“\n Enter the Number”);
scanf(“%d”, &n);
for(;n>0;n/=10)
{
r=n%10;
s=s+r;
}
printf(“\n Sum of digits %d”, s);
return 0;
}
8.3 “DO-WHILE” CONSTRUCT
#include <stdio.h>
int main()
{
int x = 1;
int count = 0;
do {
scanf(“%d”, &x);
if(x >= 0) count += 1;
} while(x >= 0);
return 0;
}
WHICH LOOP SHOULD BE USED???
THERE ARE NO HARD-AND-FAST RULE
REGARDING WHICH TYPE OF LOOP SHOULD BE USED
Using Counter
GOTO STATEMENT
“return” statements
“break” statements
“continue” statements
“BREAK” AND “CONTINUE” STATEMENTS
NESTED LOOPS
A nested loop refers to a #include <stdio.h>
loop that is contained int main()
within another loop. {
If the following output has int row, col;
to be obtained on the for(row=1;row<=4;++row)
screen {
1 for(col=1;col<=row;++col)
22 printf(“%d \t”, row);
333 printf(“\n”);
4444 }
then the corresponding return 0;
program will be }
COMMON PROGRAMMING ERRORS
Use of = instead of ==
Dangling else