In C programming, the continue statement works like a break statement, instead of terminating the loop the continue statement skips the code in between and pass it to the next iteration in the loop.
continue statement Flowchart:
continue statement in C
Syntax of continue statement:
continue;
In the for loop, continue statement skips the test condition and increment value of the variable to execute again and In the while and do…while loops, continue skips all the statements and program control goes to at the end of the loop for tests condition.
Example of continue statement in C Program.
#include <stdio.h>
int main()
{
int i = 0;
while (i < 10)
{
i++;
//the 8th iteration is skipped
if (i == 8)
continue;
printf("Value of i: %d\n", i);
}
}
The output of continue statement.
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of i: 6
Value of i: 7
Value of i: 9
Value of i: 10
break statement in C is mostly used in a switch statement to terminate the cases present in switch statement. It terminates the loop and transfers the execution process immediately to statement following the loop.
The use of break statement in nested loops terminates the inner loop and the control is transferred to the outer loop. If the break statement is used in nested loops(i.e., loop within another loop), the break statement will end the execution of the inner loop and Program control goes back to the outer loop.
break statement Flowchart:
break statement in C
Syntax of break statement:
break;
Example of break statement in C Program
#include <stdio.h>
void main()
{
int i = 1;
while (i < 10)
{
printf("Value of i before break: %d\n", i);
if (i == 8)
{
printf("Loop terminated.\n", i);
break;
}
i++;
}
}
The output of break statement in C Programming.
Value of i before break: 1
Value of i before break: 2
Value of i before break: 3
Value of i before break: 4
Value of i before break: 5
Value of i before break: 6
Value of i before break: 7
Value of i before break: 8
Loop terminated.
Loop control statements are also known as Jump statement. The use of jump statement changes the state of execution. This statement help in exiting the loops statement.
C supports three control statements.
break statement
continue statement
goto
break statement in C
break statement is mostly used in a switch statement to terminate the cases present in a switch statement. It terminates the loop and transfers the execution process immediately to a statement following the loop.
The use of break statements in nested loops terminates the inner loop and the control is transferred to the outer loop. If the break statement is used in nested loops(i.e., loop within another loop), the break statement will end the execution of the inner loop and Program control goes back to the outer loop.
In C programming, the continue statement works like a break statement, instead of terminating the loop the continue statement skips the code in between and passes the execution to the next iteration in the loop.
continue statement Flowchart:
continue statement in C
Syntax of continue statement in C:
continue;
In the case of for loop, the continue statement skips the test condition and increment the value of the variable to execute again while in the case of while and do…while loops, continue skips all the statements and program control goes to at the end of the loop for tests condition.
goto statement allows the user in the program to jump the execution to the labeled statement inside the function. The label (tag) is used to spot the jump statement.
NOTE: Remember the use of goto is avoided in programming language because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify.
goto statement Flowchart:
goto statement in C
Syntax of goto statement in C
goto label;
....
....
label: statement; //label to jump
do-while loop in C is just as same as while loop, the only difference is that in do-while the condition is always executed after the loop as shown in the syntax below.
Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.
do-while Flowchart:
do-while loop in C
Syntax of do-while Loop.
do
{
statements..
}while (condition);
Example of a do-while loop in C program.
#include <stdio.h>
int main()
{
int num = 1;
//execution of do-while loop
do {
printf("Value of num: %d\n", num);
num++;
} while (num <= 10); //condition
return 0;
}
The output of do-while loop.
Value of num: 1 Value of num: 2 Value of num: 3 Value of num: 4 Value of num: 5 Value of num: 6 Value of num: 7 Value of num: 8 Value of num: 9 Value of num: 10
A while loop is a straightforward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed. The loop continues until the condition stated is found to be false.
while loop Flowchart:
While Loopin C
Syntax of While Loop.
while(condition)
{
//block of code to be executed
}
Example of while loop in C Program
#include <stdio.h>
int main()
{
//varibale
int num = 1;
//execution of while loop
while (num < 10)
{
printf("The value of num: %d\n", num);
num++;
}
return 0;
}
The output of while loop in C programming.
The value of num: 1 The value of num: 2 The value of num: 3 The value of num: 4 The value of num: 5 The value of num: 6 The value of num: 7 The value of num: 8 The value of num: 9
In C programming, for loop is a more efficient loop structure and is an entry-control loop. The iteration continues until the stated condition becomes false.
It has three computing steps as shown in the syntax below.
initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
Increment or Decrement: The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).
for loop Flowchart:
for loop in C
Syntax of for loop.
for(initialization; condition; Increment or Decrement)
{
// Statements
}
Example of for loop in C Program.
#include <stdio.h>
int main()
{
int num;
//execution of for loop
for (num = 5; num <= 10; num++)
{
printf("The values of num: %d\n", num);
}
return 0;
}
The output of for loop in C programming.
The values of num: 5 The values of num: 6 The values of num: 7 The values of num: 8 The values of num: 9 The values of num: 10
There may be times in a program where a block of code needs to be executed several times sequentially. In such cases, Loops are used to execute a sequence of statements many times until the stated condition becomes false.
Loops in C Programming
Two Types of Loops in C Programming.
1. Entry controlled loop:
In this type loop, the stated conditional is checked first and then the block of code is executed. It is also called a pre-checking loop.
A while loop is a straight forward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed.
The loop continues until the condition stated found to be false.
While Loopin C
Syntax of While Loop in C.
while(condition)
{
//block of code to be executed
}
do-while loop in C is just as same as while loop, only difference is that in do-while the condition is always executed after the loop as shown in syntax below.
Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.
In C programming, for loop is a more efficient loop structure and is an entry-control loop. The iteration continues until the stated condition becomes false.
It has three computing steps as shown in the syntax below.
initialization: The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
condition: Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
Increment or Decrement: The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).
for loop in C
Syntax of for loop in C
for(initialization; condition; Increment or Decrement)
{
// Statements
}
The Infinite Loop
As the name suggests, the infinite loop is a forever executing loop. The infinite loop repeats indefinitely and never terminates. It means the condition never turns to false. It is also known as an indefinite loop or an endless loop.
We can use one of the Loops in C to turn it into infinite loop.
infinite ‘for’ loop
for(; ;)
{
//body of the loop
}
Example:
#include <stdio.h>
int main ()
{
//condition for infinite
for( ; ; )
{
printf("INFINITE LOOP.\n");
}
return 0;
}
If the above program is executed then we will get infinite printf statement. The compiler assume the condition to be true if the condition is absent in the loop.
NOTE: You can terminate an infinite loop by pressing Ctrl + C keys
We can also create infinite while loop by defining the condition as shown below.
A switch statement allows a variable to be tested for equality against multiple values and each of those values is called a case. It can be used instead of nestedif...else.
Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values. In the end, it can have a default case which is optional that is executed if no cases are matched.
Syntax
Syntax of switch statement in C:
switch (expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
.
.
.
.
case valueN:
//code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
Switch statement Flowchart:
Example of switch statement in C.
#include <stdio.h>
int main()
{
//local variable definition
char grade = 'D';
switch (grade)
{
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Very Good!\n");
break;
case 'C':
printf("Well done\n");
break;
case 'D':
printf("GRADE D\n");
break;
case 'F':
printf("Better try again\n");
break;
default:
printf("Invalid grade\n");
}
printf("You Pass with Grade: %c\n", grade);
return 0;
}
The use of switch statement inside another switch statement is called nested switch statement.
Syntax
Syntax ofnested switch statement in C:
switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
//use of another switch statement
switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}
Example of nested switch statements in C:
#include <stdio.h>
int main()
{
//local variable
int a = 10;
int b = 20;
switch (a)
{
case 10:
printf("The OUTER Switch.\n", a);
//INNER Switch
switch (b)
{
case 20:
printf("The INNER Switch.\n", a);
}
}
return 0;
}
An if statement consists of a Boolean expression followed by one or more statements. If the boolean expression is true, the block of code inside the if statement will be executed else not.
Syntax
Syntax of if statement in C:
if (boolean expression)
{
/* if expression is true */
statements... ; /* Execute statements */
}
if statementFlowchart:
if statement
Example of if statement in C Program:
#include <stdio.h>
void main()
{
int num1, num2;
num1 = 90;
num2 = 50;
if (num1 > num2)
{
printf("num1 is greater than num2");
}
}