Presentation Decision Making and Branching and Loop 1518762275 277498
Presentation Decision Making and Branching and Loop 1518762275 277498
&
BRANCHING
INTRODUCTION
statement x ;
Ex: if ( category == sports) false Statement block
{
marks=marks+bonus; Statement x
}
printf ( “%f”,marks);
Program of simple if statement
main()
{
int a,b,c,d;
float ratio;
printf(“\n enter four integer value”);
scanf(“%d %d %d”,&a,&b,&c);
if(c-d !=0)
{
ratio= (float) (a+b)/(float)(c-d);
printf(“Ratio= %f”, ratio);
}
} Output- enter four integer values
12 23 34 45
Ratio= -3.181818
The if…else statement entry
Syntax- if( Condition)
{ true Condition
true block statement; false
}
else True block statement False block statement
{
false block statement;
}
Statement x
statement x;
main() case 6:
{ grade=“First Division”;
int grade,mark,index; break;
printf(“Enter ur marks \n”); case 5:
scanf(“%d”,&mark); grade=“Second Division”;
index=mark/10; break;
switch(index)
{ case 4:
case 10: grade=“First Division”;
case 9: break;
case 8: default:
grade=“Honours”; grade=“Fail”;
break; break;
case 7: }
printf(“%s”,grade);
}
THE GOTO STATEMENT
• The goto statement is used for branching unconditionally.
• The goto statement breaks normal sequential execution of the program.
• The goto requires label to where it will transfer a control.
• Label is a valid variable name followed by a colon( : ).
-------------- statement;
------------- ----------------------
-------------- -----------------------
label: ----------------------
• Loop Consists of
• Body of the loop
• Control Statement
Body of Loop
False
Test
Condition
False
True Test
Condition
Body of Loop
True
false
true
Body
FOR
Syntax:
loop continuation
condition
EXAMPLES
#include <stdio.h>
void main()
{
/* a program to produce a Celsius to Fahrenheit conversion chart for the
numbers 1 to 100 */
int celsius;
for (celsius = 0; celsius <= 100; celsius++)
printf(“Celsius: %d Fahrenheit: %d\n”, celsius, (celsius * 9) / 5 + 32);
}
NESTED FOR LOOPS
• In a nested loop, for each value of the outermost counter variable, the complete
inner loop will be executed once
General form
for (loop1_exprs) {
loop_body_1a
for (loop2_exprs) {
loop_body_2
}
loop_body_1b
}
WHILE
expression 1; initialization
while (expression 2) condition checking
{
statements;
expression 3; updation of condition
}
The statement is executed repeatedly as long as the expression is true (non
zero)
The cycle continues until expression becomes zero, at which point execution
resumes after statement
If the test expression in a while loop is false initially, the
while loop will never be executed
int i = 1, sum = 0;
while (i <= 10)
{
sum = sum + i;
i= i + 1;
}
printf(“Sum = %d\n”, sum);
FOR AND WHILE
expr1;
while(expr2)
{
statement;
expr3;
}
BREAK AND CONTINUE
• continue causes the current iteration of a loop to stop and the next iteration
to begin immediately
while (expression)
{
statements;
if(condition)
break;
more_statements
}
while (expression)
{
statements
continue;
more_statements
}
DO-WHILE
int i = 1, sum = 0;
do
{
sum = sum + i;
i= i + 1;
}while(i<=10);
printf(“Sum = %d\n”, sum);
WHILE VS. DO-WHILE
while -- the expression is tested first, if the result is false, the loop body is
never executed
do-while -- the loop body is always executed once. After that, the expression
is tested, if the result is false, the loop body is not executed again