Lesson 5 of C Programming
Lesson 5 of C Programming
Conditional/Unconditional
Statements
Loops
Repetition Structure
Learning Objectives:
At
FLOW OF CONTROL
Statements in a program are normally executed one after another.
This is called sequential flow of control. Often it is desirable
to alter the sequential flow of control to provide for a choice
of action, or repetition of action. By means of if, if-else, and
switch statement, a selection among alternative actions are made.
By means of while, for and do statements, interactive actions can
be taken.
The relational, equality, and logical operators are heavily
used in flow of control construct.
1. Operators
RELATIONAL, EQUALITY, AND LOGICAL OPERATORS
The following table contains the operators that are most often
used to affect flow of control:
1.1 Relational Operators
<
less than
>
greater than
<=
>=
equal
!=
not equal
(unary) negation
&&
logical and
||
logical or
Relational,
precedence
Equality,
and
and
Logical
associativity
that
operators
determine
have
rules
precisely
of
how
Associativity
+ (unary) (unary) ++ -- !
right to left
left to right
<
<=
= =
!=
left to right
>
>=
left to right
left to right
&&
left to right
||
left to right
+=
-=
*=
/=
etc
right to left
Example
a < b is either true or false. In C, the expression will yield
the int value 1 if it is true and the int value 0 if it is false.
1.1.1 Relational Operators and Expressions
>
<=
>=
are all binary. Each take two expressions as operands and yield
either the int value 0 or the int value 1.
expr == expr
expr != expr
Examples
C == A;
K != .2;
x + y == 3* z- 7;
The logical operator ! is unary, and the logical operators && and
||
are
binary.
All
of
these
operators,
when
applied
to
logical_negation_expression;
logical_or_expression;
logical_and _expression;
logical_or_expression
expr || expr;
logical_and_expression
Examples
a && b;
a || b;
!(a>b) &&x
2. The COMPOUND STATEMENT
Syntax:
compound_statement { { declaration } { statement } }
the
expression
in
an
if
statement
is
relational,
code
can
be
written
to
be
more
efficient
and
more
if
expr
is
zero,
then
statement1
is
skipped
and
Example
if (a>b)
{ b++;
printf( Value is smaller %d\n, b);
}
else
printf(You got a bigger value %d\n, a);
An if-else statement can be used as the statement part of another
if statement, this is what you call nested if.
Example
if(x == 50)
if(y >= 120)
{ sum = x + y;
printf(the sum of x and y is %d, sum);
}
else
{ diff = x y;
printf(the difference between x and y is %d, diff);
}
else
printf(Next time);
3.3 THE switch STATEMENT
The switch is a multiway conditional statement generalizing the
if-else statement.
Syntax:
switch(expression)
}
Action:
The switch expression maybe any expression which evaluates
to an int, conditional, char or double value. The case list lust
consists of constants whose type matches that of the
switch
jump
to
the
end
of
the
switch
statement.
The
switch
an
action
to
be
taken
if
the
value
of
the
Example
Assign a letter grade based on the number value 0-10
switch (QUIZ) {
case
10:
case
9:
printf(A);
break;
case
8:
printf(B);
break;
case
7:
printf(C):
break;
switch
case
case
case
case
case
case
case
default:
switch
6:
printf(D);
break;
5:
4:
3:
2:
1:
0:
printf(F); break;
printf(Input out of Range);
end of
Sample Program:
This program will prompt the user to input an
integer number which will determine the corresponding month in
words.
Sample Run:
Please enter a number:
November
11
#include <stdio.h>
#include <conio.h>
void main()
{ clrscr();
int number;
printf(Please enter a number: );
scanf(%d,&number);
switch(number) {
case 1: printf(January\n);
break;
case 2: printf(February\n);
break;
case 3: printf(March\n);
break;
case 4: printf(April\n);
break;
case 5: printf(May\n);
break;
case 6: printf(June\n);
break;
case 7: printf(July\n);
break;
case 8: printf(August\n);
break;
case 9: printf(September\n);
break;
Example
while(1) {
scanf(%lf,&x);
if(x < 0.0)
break;
printf(%f\n,sqrt(x));}
Example
do{
scanf(%d,&num);
if (x < 0 )
continue;
printf(%d,x);
} while (x != 100);
5. LOOPS
Repetition of action is one reason we rely on computers.
When there are large amount of data, it is very convenient to
have
control
mechanism
that
repeatedly
execute
specific
Example
While(number != 0)
{ scanf (%d,&number);
sum += number;
}
printf( the sum of all numbers is %d,sum);
The for statement allows many variants, but there are three main
parts;
1. initialization is usually an assignment statement that is
used to set the loop control variable.
2. condition is a relational expression that determines when
the loop will exit.
3. increment defines how the loop control variable will change
each time the loop is repeated.
These three major sections must be separated by semicolon. The
for loop continues to execute as long as the condition is TRUE.
Example
for(x=100;x!=65;x+=5)
{
z=sqrt(x);
printf(The square root of %d is %f,x,z);
}
An important point above for loop is that conditional test is
always performed at the top of the loop. This means that the code
inside the loop may not be executed at all if the condition is
FALSE to begin with.
Example:
for(x=0,y=0; x+y < 10; x++)
{
scanf(%d, &y);
sum = x + y;
}
printf(The sum of x and y is %d, sum);
statement;
}
}
5.3 THE do STATEMENT
Syntax:
do {
Statement;
} while(expr);
next statement;
Action:
First statement is executed and expr is evaluated. If the value
of
expr
is
beginning
of
nonzero(TRUE),
the
do
then
statement
control
and
then
passes
the
back
process
to
the
repeats
do {
printf(enter the number: );
scanf(%d, &a);
sum=sum + a;
} while (a!=0);
printf(\nThe sum is %d,sum);
getch();
}
Sample run:
Enter series of integers, until 0 is entered:
Enter the number: 7
Enter the number: 3
Enter the number: 9
Enter the number: 0
The sum is 19
EXERCISE NO. 3
NAME ______________________________
YEAR AND SECTION __________________
DATE _______________
SCORE ______________
_________________________________________________________________
_________________________________________________________________
_______________________________________________
2.
A. Give the syntax and explain for loop:
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________
B. Give the syntax and explain the while loop:
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________
C. Give the syntax and explain the do loop:
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________
SAMPLE EXERCISES NO. 3
Answer the following questions:
1. What is a compound statement?
2. True or false. The else statement
statement
is required.
3. True or false. All conditional expressions should be enclosed
in parenthesis.
4. Correct the syntax error:
if x>25.0{
y=x
else
y=z;; }
x=7; y=8;
if(x<=y)
if(x==y)
x++;
else
y++;
printf(%d%d\,x,y);
(b)
k=3; j=5;
if (k<4)
if (j>2)
if (k-j=3)
k=k+J
else
k=k-j;
printf(%d\n,k);
points =0;
switch (mark) {
case A:
points =4;
exit();
case B:
points =3;
break;
case C:
points =2;
continue;
case E:
case I:
case W:
points =0;
}
if (points>0)
printf (Passed, points earned =%d\n, points);
else
printf (Failed, no points earned\n);
(b)
x=7;y=8
if (x<=y)
{if(x=y)
x++;}
8. Explain the difference between the statements on the left and
the statements on the right. For each group of statements, give
the final value of x if the initial value of x is 1
if (x>0)
x=x+1;
else if (x>=1)
x=x+2;
if (x>0)
x=x+1;
if (x>=)
x=x+2;
Write
program
segment
for
the
following
using
(a)
if
variables
THEY
and
THEM
and
assign
the
results
to
_________________________________________________________________
___________________________________
___________________________________________________________
13. The following program is partially complete:
a=5;
x:
c++;
if(c==1)
{ a=a*a;
goto x;}
else
if (c==2)
{ a = a + a;
goto x;}
else
for(i=1; i<=5;
printf(%2d, i);
i++)
(b)
ctr = 0;
do
{ ctr = ctr + 1;
if ((ctr % 2) != 0)
continue
else
printf(%2d, ctr);
}while ( ctr !=10 );
(c)
x = 10; y = 10;
while (x != y) {
printf(%5d %5d\n,x,y);
x--; y ++; }
of
the
Planet
Endor.
However,
if
the
applicant
is
Fine
10.00
15.00
20.00
Equal to the amount of rent
(CD = 50.00 / VHS = 35.00 )
CASE STUDY 1
Write a program that accepts a positive integer and gives its
prime factorization, that expresses the integer as a product of
primes.
CASE STUDY 2
N factorial can be defined as the product of all integers from 1
to N and it is denoted by the symbol N!. 0! (zero factorial) is
defined as 1. Write a program that will input N and would display
N factorial. (Determine first if N is a nonnegative integer).
CASE STUDY 3
Write a program that will input non-negative integer and call a
function DWARF to determine if the integer is DWARF or NOT. An
integer is said to be DWARF if the sum of its factors is greater
than the half of the number.
Example:
Input number: 6
Factors are: 1 2 3
Sum of its factor: 1 +2+3 =6
Half of the number : 6 /2 = 3
6 is DWARF
Input number: 9
Factors are 1,3
Sum of its factors : 1 + 3 = 4
Half of number : 9 / 2 = 4.5
9 is NOT DWARF