C Programming 2
C Programming 2
Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another
part of the program. The main differences between break and continue are…..
Basically, break keyword terminates the rest of remaining iterations of the loop. On the contrary, the
continue keyword terminates only the current iteration of the loop.
Once the break keyword executes, the control of the program exit out of the loop and resumes to the
next statement after the loop. In case of continue keyword, the control of the program resumes to the
next iteration of the loop.
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;++i)
{
if(i==3)
break;
printf(“%d “,i);
}
Output:
012
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;++i)
{
if(i==3)
continue;
printf(“%d “,i);
}
Output:
0124
Difference between break and exit() in C
break exit
It can be used only within the loop and switch case statement. It can be used
anywhere in the program.
In a C program, more than one break statement can be executed. In a C program, only one exit
function can be executed.
Switch statement in c
Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
why break and default is used in switch statement in c
When a break statement is reached, the switch terminates, and the flow of control jumps to the
next line following the switch statement.Not every case needs to contain a break. If
no break appears, the flow of control will fall through to subsequent cases until a break is
reached.
A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true.
No break is needed in the default case.
nested-if in C
A nested if in C is an if statement that is the target of another if statement. Nested if
statements means an if statement inside another if statement. Yes, both C and C++ allows us
to nested if statements within if statements, i.e, we can place an if statement inside another if
statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
if-else-if ladder in C
Here, a user can decide among multiple options. The C if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the
final else statement will be executed.
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
C goto:
The goto statement in C/C++ also referred to as unconditional jump statement can be used to jump
from one point to another within a function.
Syntax:
Syntax1 | Syntax2
Condition is checked first then statement(s) is Statement(s) is executed atleast once, thereafter
executed. condition is checked.
Variable in condition is initialized before the variable may be initialized before or within the
execution of loop. loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
void main()
{
for(;;)
{
printf("Hello");
}
}
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop.Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops. The nesting level can be defined at n times. You can define
any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.