0% found this document useful (0 votes)
26 views

C Programming 2

The document discusses the differences between the break and continue statements in C. Break terminates the entire loop, while continue skips the current iteration and continues with the next. Break exits the loop entirely, while continue jumps to the next iteration. The document provides examples to demonstrate the behavior of each statement.

Uploaded by

SANJIB BEJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C Programming 2

The document discusses the differences between the break and continue statements in C. Break terminates the entire loop, while continue skips the current iteration and continues with the next. Break exits the loop entirely, while continue jumps to the next iteration. The document provides examples to demonstrate the behavior of each statement.

Uploaded by

SANJIB BEJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Key Differences Between Break and Continue

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 is a keyword It is a pre-defined function.

It doesn’t require any header file. It requires header file stdlib.h

It terminates the loop. It terminates the program.

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

The switch statement is a multiway branch statement. It provides an easy


way to dispatch execution to different parts of code based on the value of the
expression.

Switch is a control statement that allows a value to change control of


execution.

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

goto label; | label:


. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a label.
Here label is a user-defined identifier which indicates the target statement. The statement immediately
followed after ‘label:’ is the destination statement.

Difference between while and do-while loop in C

Condition is checked first then statement(s) is Statement(s) is executed atleast once, thereafter
executed. condition is checked.

It might occur statement(s) is executed zero


times, If condition is false. At least once the statement(s) is executed.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

If there is a single statement, brackets are not


required. Brackets are always required.

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);

What is infinite loop?


An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It
is also called an indefinite loop or an endless loop. It either produces a continuous output or no output.
An infinite loop is useful for those applications that accept the user input and generate the output
continuously until the user exits from the application manually. In the following situations, this type of
loop can be used.

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.

Syntax of Nested loop


Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}

You might also like