3 Control Flow
3 Control Flow
3 Control Flow
Let us C----Chap=1----Page=35*/
#include<conio.h>
#include<stdio.h>
main()
{
int num,one,two,three,four,five,total;
int d1,d2,d3,d4,d5;
clrscr();
printf("Enter a five digit number=");
scanf("%d",&num);
d1=num/10000;
one=num%10000;
d2=one/1000;
two=one%1000;
d3=two/100;
three=two%100;
d4=three/10;
four=three%10;
d5=four/1;
total=d1+d2+d3+d4+d5;
printf("Total=%d",total);
getch();
}
Control Flow
Decisions if then else
More decisions switch
Loops while, do while, for
Keyword break
Keyword continue
Decisions if then else
Parentheses surround the test: -surround the condition with parentheses. These are mandatory.
One statement becomes the then part
If more are required, braces must be used:- if many statements are to be executed ,group the statements into a block.
if(if>0)
printf(a is positive);
if(if>0){
printf(a is positive);
i=-i;
}
A semicolon after the condition forms a do nothing statement.
If then else
An optional else may be added
One statement by default, if more are required, braces must be used
if(if>0)
printf(a is positive);
else
printf(a is negative);
Optionally an else statement, which is executed if the condition is false
Nesting ifs
Else associates with the nearest if:-Potential ambiguity with nested if then else statements. This arises in trying to
determine where an else clause belongs
int i=100;
if(i>0)
if(i>1000)
printf(i is big);
else
printf(i is reasonable);
int i=-20;
if(i>0){
if(i>1000)
printf(i is big);
}else
printf(i is reasonable);
*Assume that the USA uses the following income tax code formula for their
annual income: //////////
5000 of income 0% tax // AJAY //
10000 of income 10% tax //////////
20000 of income 15% tax
above 35000 of income 20% tax
for example earning of 38,000 annually would owe
5000*0.0+10000*0.10+20000*0.15+3000*0.20=4600*/
#include<conio.h> next=income-20000;
#include<stdio.h> tax=(5000*0.00)+(10000*0.10)+(next*0.15);
main() printf("\nTax=%f",tax);
{ }
float income,next,tax; else if(income>=10000)
float limit=35000; {
clrscr(); next=income-10000;
printf("Enter your Annual Salary="); tax=(5000*0.00)+(next*0.10);
scanf("%f",&income); printf("\nTax=%f",tax);
//next=income-limit; }
if(income>limit) else if(income>=5000)
{ {
next=income-limit; next=income-5000;
tax=(5000*0.00)+(10000*0.10)+(20000*0.15)+(next*0.20); tax=5000*0.00;
printf("\nTax=%f",tax); printf("\nTax=%f",tax);
} }
else if(income>=20000) getch();
{ }
Switch
C supports a switch for multi way decision making.
Placing default first does not alter the behavior in any way
Only integral constants may be tested.
If no condition matches the default is executed.
If no default nothing is done
The break is important
switch(c){
case a:case a:
printf(area=%d,r*r*pi);
break;
case c:casec:
printf(circumference=%d,2*r*pi);
break;
caseq;
printf(quit option chosen);
break;
default:
printf(unknown option chosen);
break;
}
while loop
Parentheses must surround the condition.
One statement forms the body of the loop.
Braces must be added if more statements are to be executed.
A semicolon placed after the condition forms a body that does nothing.
Int j=5;
While (j>0){
Printf(j=%I\n,j);
}
The while statement is a top-driven loop: first the loop condition (i.e., the controlling expression) is evaluated. If it yields true, the
loop body is executed, and then the controlling expression is evaluated again. If the condition is false, program execution continues
with the statement following the loop body.
is true as long as the user enters a decimal number. As soon as the function scanf( ) is unable to convert the string input into a
floating-point numberwhen the user types the letter q,
Do while
Do while guarantees execution at least once. Whereas while has the condition followed by the body, do while has the body
followed by the condition.
int j=-10;
printf(start\n);
do{
printf(j=%i\n,j);
j--;
}while(j>0);
printf(stop\n);
char letter[80];
int tag,count=-1;
do ++count;
while((letter[count]=getchar())!=EOL);
tag=count;
count=0;
do{
putchar(toupper(letter[count]));
++count;
}while(count<tag);
}
for(initial-par;while-condition;upate-part)
body;
expression1 : Initialization:- Evaluated only once, before the first evaluation of the controlling expression, to perform any necessary
initialization.
expression2 : Controlling expression:-Tested before each iteration. Loop execution ends when this expression evaluates to false.
expression3 : Adjustment:-An adjustment, such as the incrementation of a counter, performed after each loop iteration, and before
expression2 is tested again.
int j
for(j=5;j>0;j--)
printf(j=%I\n,j);
for(j=5;j>0;j--){
printf(j=%i,j);
printf(%s\n,((j%2)==0)?even:odd);
}
unconditional Jumps
Jump statements interrupt the sequential execution of statements, so that execution continues at a different point in the program. A
jump destroys automatic variables if the jump destination is outside their scope. There are four statements that cause unconditional
jumps in C: break , continue, goto, and return.
Break
The break keyword forces immediate exit from the nearest enclosing loop
for(;;){
printf(type an int=);
if(scanf(%i,&j)==1)
break;
while((c=getchar())!=\n)
;
}
printf(j=%i\n,j);
Continue
The continue keyword forces the next iteration of the nearest enclosing loop.
for(j=1;j<=10;j++){
if(j%3==0)
continue;
printf(j=%i\n,j);
}
if j is exactly divisible by 3 skip.
The goto statement causes an unconditional jump to another statement in the same function. The destination of the jump is specified
by the name of a label:
goto label_name;
label_name: statement
Practical exercise
1. Write a program in quant.c which quantifies numbers, Read an integer x and test it, producing the following output;
X greater then or equal to 1000 print hugely positive
X form 999 to 100 very positive
X between 100 and 0 positive
X exactly 0 zero
Between 0 and 100 negative
-100 to 999 very negative
less then or equal to 1000 hugely negative
2. Write a program which accepts four options. The option a calculates the area of a circle (prompting for the radius), the
option C calculates the circumference of a circle the option v calculates the volume of a cylinder (prompting for the radius
and height) while the option q quits the program. The program should loop until the quit option is chosen.
3. write a program in pow.c which reads two numbers, the first a real , the second an integer. The program then outputs the first
number raised to the power of the second.
4. Write a program in drawx,c which draws an x of user specified height. If the user typed 7, the following seris of *
characters would be drawn
5. Write a program in bases.c which offeres the user a choice of converting integers between octal decimal and hexadecimal,
Prompt the user for either o,d orh and read the number in the chosen format. Then prompt the user for the output format
o,d orh and print the number out accordingly