Module2-decision control and loop
Module2-decision control and loop
goto
break
Unconditional
Branch statements continue
return
if(test Expression)
{ test
Statement 1; Expression False
Statement 2; True
... Statement 1
Statement 2
…
…
… …
…
Statement n; Statement n
}
Statement x; Statement x
The „if‟ structure may include one statement or „n‟ statements enclosed within curly brackets.
Working Principle
First the test expression is evaluated. If the test expression is true, then the statements of „if‟
block (statement 1 to n) are executed. Otherwise these statements will be skipped and the
execution will jump to statement x.
The is-else statement is used when we must choose between two choices (alternatives).
Hence is also called as “Two-way Decision or Selection Statement”.
Working Principle
According to the if-else construct, first the „test expression‟ is evaluated.
If the expression is true then Statement block 1 is executed and Statement block 2 is skipped.
If the expression is false the Statement block 2 is executed and Statement block 1 is ignored.
Now in any case after the Statement block 1 or 2 gets executed the control will pass to
Statement x. It is executed in every case.
3. Write a C program to enter a character and then determine whether it is a vowel or not.
#include<stdio.h>
void main()
{
char ch;
printf(“Enter any character:”);
scanf(“%c”,&ch);
if(ch==‟a‟||ch==‟e‟||ch==‟i‟||ch==‟o‟||ch==‟u‟||ch==‟A‟||ch==‟E‟||ch==‟I‟||ch==‟O‟||ch==‟U‟)
printf(“\n Character is a vowel”);
else
printf(“\n Character is a consonant”);
}
Statement block 1;
}
else True False Statement
Test
{ Expression2 block 3
Statement block 2;
} Statement Statement
} block 1 block 2
else
{
Statement block 3;
Statement x
}
Statement x;
Working Principle
If the test expression 1 is evaluated to true, then the test expression 2 is checked for true or
false. If the test Expression2 is evaluated to true, then the statements in block 1 are executed,
otherwise the statements in block 2 are executed. After executing the inner if-else the control
comes out and the statement x is executed.
If the test expression1 itself is false, then the statements in block 3 are executed. After
executing these statements, the statement x is executed.
Statement block n;
} Statement x
Statement x;
Working Principle
The „Expressions‟ are evaluated in order. If any expression is true then the statement
associated with it is executed and this terminates the whole chain and statement x is
executed.
The last „else‟ part is executed when all the test expression are false.
Statement x
Working Principle
First the expression within switch is evaluated.
The value of an expression is compared with all the case values.
The value of an expression within switch is compared with the case value-1. If it matches
then the statements associated with that case are executed. If not then the case value-2 is
compared, if it matches then the associated statements are executed and so on.
The “default” statements are executed when no match is found.
A default is optional.
The „break‟ statement causes an exit from the switch. „break‟ indicates end of a particular
case and causes the control to come out of the switch. [Significance of break within switch
statement]
2. Write a C program to enter a number from 1 to 7 and display the corresponding day of
the week using switch.
#include<stdio.h>
void main()
{ int day;
printf(“Enter any number:”);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“Sunday”);
break;
case 2: printf(“Monday”);
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednseday”);
break;
case 5: printf(“Thursday”);
break;
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
default : printf(“invalid input”);
}
}
do-while loop
Statement x
Working principle
The test expression is evaluated first, if it is TRUE then the set of statements within the body
of the loop are executed repeatedly as long as specified test expression is TRUE.
If the test expression is false, then the control comes out of the loop by skipping the
execution of the statements within the body of the loop, by transferring the control to the
Statement x.
6. Write a C program to find the factorial of a given number using while statement.
#include<stdio.h>
void main()
{
int fact=1,i=1,n;
printf(“Enter the value of n:\n”);
scanf(“%d”,&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf(“Factorial of a given number is=%d”,fact);
}
Statement x
Working Principle
initialization: In this section loop variable is initialized, like i=0, n=0, i=1,n=1. (i and n are
loop variables).
test expression: The test expression may be a relational expression or logical expression or
both, which is evaluated to TRUE or FALSE. Depending on the value of the test expression,
the body of the loop is executed. If the test expression is TRUE, then the body of the loop is
executed. This process of execution of body of the loop is continued as long as the
expression is TRUE. When the test expression becomes FALSE, execution of the statements
contained in the body of the loop are skipped, thereby transferring the control to the
Statement x, which immediately follows the for loop.
increment/decrement: This section increments or decrements the loop variables after
executing the body of the loop.
Infinite Loop
A for loop without a test expression is an Infinite loop.
Ex: for(i=0; ;i++)
{
………
} is an “infinite” loop.
printf(“%c\t”,ch);
}
}
Output: A B C D E F Z
6. Write a C program to find the factorial of a given number using for statement.
#include<stdio.h>
void main()
{
int fact=1,i,n;
printf(“Enter the value of n:\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial of a given number is=%d”,fact);
Statement x
Working Principle
It is a post-test or bottom-testing loop and hence the statements contained within the body
of the loop are executed first and then the expression is evaluated to TRUE or FALSE. If it is
TRUE, then the statements contained within the body of the loop are executed once again
and the expression is evaluated. This is repeated until the expression is evaluated to FALSE.
If the expression is FALSE, then the control comes out of the loop by skipping the execution
of the statements within the body of the loop, by transferring the control to the Statement x.
True
True
Statements while(test expression);
False
Statement x Statement x
2. Write a C program to calculate the sum and average of first „n‟ numbers.
#include<stdio.h>
void main()
{
int n,i=0,sum=0;
float avg=0.0;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
}while(i<=n);
avg=sum/n;
printf(“The sum of numbers=%d”,sum);
printf(“The average of numbers=%f”,avg);
}
If break is executed in a loop (for/while/do-while) then the control comes out of the loop
and the statement following the loop will be executed.
Syntax
1. while(…)
{
……
if(condition)
break;
……
} Transfers the control out of the while loop
……
2. do
{
……
if(condition)
break;
…… Transfers the control out of the do-while loop
}while(condition);
……
3. for(…)
{
……
if(condition)
break;
……
} Transfers the control out of the for loop
……
Syntax:
1. while(… )
{
if(condition) Transfers the control to the expression of the while loop
continue;
……….
}
……….
2. do
{
……
if(condition)
continue;
…… Transfers the control to the expression of the do-while loop
}while(condition);
……
3. for(…)
{
……
if(condition) Transfers the control to the expression of the for loop
continue;
……
}
……
2.10 Write a C Program to display the following by reading the number of rows as input,
soprotection.com