Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
unconiditional stmts.docx
1. Unconditional
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
C supports the following control statements.
C supports the following control statements.
Sr.No. Control Statement & Description
1 break statement
Terminates the loop or switch statement and transfers execution to the
statement immediately following the loop or switch.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
3 goto statement
Transfers control to the labeled statemen
break, continue and goto statements
The break; continue; and goto; statements are used to alter the normal flow
of a program.
Loops perform a set of repetitive task until text expression becomes false
but it is sometimes desirable to skip some statement/s inside loop or
terminate the loop immediately without checking the test expression. In
such cases, break and continue statements are used.
2. break statement
In C programming, break statement is used with conditional if statement.
The break is used in terminating the loop immediately after it is
encountered.
it is also used in switch...casestatement. which is explained in next topic.
Syntax:
break;
The break statement can be used in terminating loops like for, while and do...while
Example:
//Write a C Program Which use of break statement.
#include<stdio.h>
#include<conio.h>
void main(){
int num, sum=0;
int i,n;
printf("Note: Enter Zero for break loop!n");
printf("Enter Number of inputsn");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter num%d: ",i);
3. scanf("%d",&num);
if(num==0) {
break; /*this breaks loop if num == 0 */
printf("Loop Breakedn");
}
sum=sum+num;
}
printf("Total is %d",sum);
getch();
}
Note: Enter Zero for break loop!
Enter Number of inputs
5
Enter num1: 5
Enter num2: 10
Enter num3: 0
Loop Breaked
Total is 15
continue statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used.
Syntax:
continue;
Just like break, continue is also used with conditional if statement.
4. Example:
//Write a C Program Which use of continue statment.
#include<stdio.h>
#include<conio.h>
void main(){
int i, n=20;
clrscr();
for(i=1;i<=n;++i){
if(i % 5 == 0) {
printf("passn");
continue; /*this continue the execution of loop if i % 5 == 0 */
}
printf("%dn",i);
}
getch();
}
Output:
Command Prompt
1
2
3
4
5. pass
6
7
8
9
pass
11
12
13
14
pass
16
17
18
19
pass
goto statement
In C programming, goto statement is used for altering the normal sequence
of program execution by transferring control to some other part of the
program.
Syntax:
goto label;
.............
.............
.............
label:
statement;
In this syntax, label is an identifier.
When, the control of program reaches to goto statement, the control of the
program will jump to the label: and executes the code below it.
goto-statement
Example:
6. //Write a C Program Which Print 1 To 10 Number Using goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
count: //This is Label
printf("%dn",i);
i++;
if(i<=10) {
goto count; //This jumps to label "count:"
}
getch();
}
Output:
Command Prompt
1
2
3
4
5
6
7. 7
8
9
10
Note:
Though goto statement is included in ANSI standard of C, use of goto
statement should be reduced as much as possible in a program.
Reasons to avoid goto statement
Though, using goto statement give power to jump to any part of program,
using goto statement makes the logic of the program complex and tangled.
In modern programming, goto statement is considered a harmful construct
and a bad programming practice.
The goto statement can be replaced in most of C program with the use of
break and continue statements.
In fact, any program in C programming can be perfectly written without the
use of goto statement.
All programmer should try to avoid goto statement as possible as they can.