The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.
break;
We just put the break where ever we want to terminate the execution of the loop.
The break statement in C is used for breaking out of the loop. We can use it with any type of loop to bring the program control out of the loop. In C, we can use the break statement in the following ways:
Break statements in C can be used with simple loops i.e, for loops, while loops, and do-while loops.
break in for loop 1 2 break in while loop 1 2
Break statements can also be used when working with nested loops. The control will come out of only that loop in which the break statement is used.
1 1 2 1 2 3 1 2 3 4
Note: Break statement only breaks out of one loop at a time. So if in nested loop, we have used break in inner loop, the control will come to outer loop instead of breaking out of all the loops at once. We will have to use multiple break statements if we want to break out of all the loops.
An Infinite loop can be terminated with a break statement as a part of the condition.
0 1 2 3 4
In the above program, the loop condition is always true, which will cause the loop to execute indefinitely. This is corrected by using the break statement. Iteration of the loop is restricted to 8 iterations using break.
Working of break in a for loop
The working of the break statement in C is described below:
In general, the Switch case statement evaluates an expression, and depending on the value of the expression, it executes the statement associated with the value. Not only that, all the cases after the matching case after the matching case will also be executed. To prevent that, we can use the break statement in the switch case as shown:
Output:
Enter an operator (+, -), if want to exit press x: + Enter Two Values: 10 20 10.0 + 20.0 = 30.0
In this article, we have discussed the break statement and how it can be used to alter the flow of the normal execution of the C program. The break statement is particularly useful when we want to terminate the loop earlier than the normal logic.
The break statement in C is used for early termination and exit from the loop.
The difference between the break and continue in C is listed in the below table:
break;
continue;
Like Article -->The break in C++ is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there and control returns from the loop immediately to the first statement after the loop. Syntax: break; Basically, break statements are used in situations when we are not sure
5 min read Print "Even" or "Odd" without using conditional statementWrite a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, <,>. etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read Return Statement vs Exit() in main() in C++The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of <stdlib.h> that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read goto Statement in CThe C goto statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label; In the above syntax, the first line te
3 min read C if. else StatementThe if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block. C if Statement The if statement in C is used to execute a block of code based on a specified condit
6 min read Return Statement in CPre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void returns nothing, etc. In C, we can only return a single
4 min read Continue Statement in CThe continue statement in C is a jump statement that is used to bring the program control to the start of the loop. We can use the continue statement in the while loop, for loop, or do..while loop to alter the normal flow of the program execution. Unlike break, it cannot be used with a C switch case. What is continue in C? The C continue statement
5 min read C - if StatementThe if in C is the most simple decision-making statement. It consists of the test condition and if block or body. If the given condition is true only then the if block will be executed. What is if in C? The if in C is a decision-making statement that is used to execute a block of code based on the value of the given expression. It is one of the cor
5 min read Data type of case labels of switch statement in C++?In C++ switch statement, the expression of each case label must be an integer constant expression. For example, the following program fails in compilation. C/C++ Code /* Using non-const in case label */ #include<stdio.h> int main() < int i = 10; int c = 10; switch(c) < case i: // not a "const int" expression printf("Value of c
2 min read Interesting facts about switch statement in CPrerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution. C/C++ Code // Following is a simple program to demonstrate syntax of switch. #include <stdio.h> int main() < int x = 2; switch (x) < case 1: printf("Choice is 1"); break; case 2: printf("Cho
3 min read Implementing ternary operator without any conditional statementHow to implement ternary operator in C++ without using conditional statements.In the following condition: a ? b: c If a is true, b will be executed. Otherwise, c will be executed.We can assume a, b and c as values. 1. Using Binary Operator We can code the equation as : Result = (!!a)*b + (!a)*c In above equation, if a is true, the result will be b.
4 min read Switch Statement in CSwitch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). Switch case statements follow a selection-control mechanism and allow a value to change control of
8 min read C Programming Language TutorialIn this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro
8 min read Top 50 C Coding Interview Questions and Answers (2024)C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is the most used language in top companies such as LinkedIn, Microsoft, Opera, Meta, and N
15+ min read printf in CIn C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output in numerous ways. Syntax of printfprintf ( "formatted_string", arguments_list);Parametersformatted_st
5 min read Pattern Programs in CPrinting patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this article, we will discuss the following example progra
15+ min read C ProgramsTo learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
8 min read C Programming Interview Questions (2024)At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the old and most popular programming languages. There are many applications in which C programming language is used, including language compilers, operating systems,
15+ min read scanf in CIn C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.scanf also uses format specifiers like printf.sc
2 min read C Hello World ProgramThe “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn. To print the "Hello World", we can use the printf function from the stdio.h library that prints the given string on the screen. C Program to Print "Hello World"The following C program displays "Hello World" in
2 min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is
9 min read Substring in C++The substring function is used for handling string operations like strcat(), append(), etc. It generates a new string with its value initialized to a copy of a sub-string of this object. In C++, the header file which is required for std::substr(), string functions is <string>. The substring function takes two values pos and len as an argument
8 min read Strings in CA String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'. C String Declaration SyntaxDeclaring a string in C is as simple as declaring a one-
8 min read Decision Making in C (if , if..else, Nested if, if-else-if )The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs. They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making sta
11 min read Operators in CIn C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the symbol that helps us to perform some specific math
14 min read C PointersPointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. In this article, we will discuss C pointers in detail, their
14 min read Data Types in CEach variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning
7 min readArray in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more
15+ min read Bitwise Operators in CIn C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C takes two n
7 min read C Language IntroductionC is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableLow-level Memory AccessFast SpeedClean SyntaxThese