Branching and Looping
Branching and Looping
C if else Statement
if( boolean_expression 1) {
int main () {
default:
code to be executed if all cases are not matched;
}
Rules for switch statement in C
language
• The switch expression must be of an integer or
character type.
• The case value must be an integer or character
constant.
• The case value can be used only inside the switch
statement.
• The break statement in switch case is not must. It
is optional. If there is no break statement found
in the case, all the cases will be executed present
after the matched case. It is known as fall through
the state of C switch statement.
Flowchart of switch statement in C
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("Value is 1\n");
break;
case 2:
printf("Value is 2\n");
break;
case 3:
printf("Value is 3\n");
break;
default:
printf("Value is not 1, 2, or 3\n");
break;
}
return 0;
}
Break and Default keyword in Switch statement
• Let us explain and define the "break" and "default"
keywords in the context of the switch statement,
along with example code and output.
• 1. Break Keyword:
• The "break" keyword is used within the code block
of each case to terminate the switch statement
prematurely. When the program encounters
a "break" statement inside a case block, it
immediately exits the switch statement, preventing
the execution of subsequent case blocks.
The "break" statement is crucial for avoiding switch
statements' "fall-through" behavior.
2. Default Keyword:
If no matching case exists and a "default" case
exists, the code block associated with
the "default" case is run.
Nested switch case statement
#include <stdio.h>
int main () {
int i = 10;
int j = 20;
switch(i) {
case 10:
printf("the value of i evaluated in outer switch: %d\n",i);
case 20:
switch(j) {
case 20:
printf("The value of j evaluated in nested switch: %d\n",j);
}
}
return 0;
}
C Loops
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. There are three types of loops used in the C language. In this part of
the tutorial, we are going to learn all the aspects of C loops.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It enables us to alter the
flow of the program so that instead of writing the same code again and again, we can
repeat the same code for a finite number of times. For example, if we need to print the
first 10 natural numbers then, instead of using the printf statement 10 times, we can print
inside a loop which runs up to 10 iterations.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of C Loops
There are three types of loops in C language that is given below:
do while
while
for
do-while loop in C
The do-while loop continues until a given condition
satisfies. It is also called post tested loop. It is used
when it is necessary to execute the loop at least once
(mostly menu driven programs).
The syntax of do-while loop in c language is given
below:
do{
//code to be executed
}while(condition);
#include <stdio.h>
int main() {
inti = 1;
do {
printf("%d\n", i);
i++;
} while (i<= 5);
return 0;
}
while loop in C
• The while loop in c is to be used in the scenario where
we don't know the number of iterations in advance. The
block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is
also called a pre-tested loop.
while(condition){
//code to be executed
}
#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d \n",i);
i++;
}
return 0;
}
Properties of while loop
• A conditional expression is used to check the condition.
The statements defined inside the while loop will
repeatedly execute until the given condition fails.
• The condition will be true if it returns 0. The condition
will be false if it returns any non-zero number.
• In while loop, the condition expression is compulsory.
• Running a while loop without a body is possible.
• We can have more than one conditional expression in
while loop.
• If the loop body contains only one statement, then the
braces are optional.
Infinitive while loop in C
If the expression passed in while loop results in
any non-zero value then the loop will run the
infinite number of times.
while(1){
//statement
}
for loop in C
The for loop in C language is used to iterate the statements
or a part of the program several times. It is frequently used
to traverse the data structures like the array and linked list.
Let's see the simple program of for loop that prints table
of 1.
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}
Properties of Expression 1
• #include<stdio.h>
• void main ()
• {
• int i;
• for(i=0;i<10;i++)
• {
• int i = 20;
• printf("%d ",i);
• }
• }
Infinitive for loop in C
To make a for loop infinite, we need not give any expression in the syntax.
Instead of that, we need to provide two semicolons to validate the syntax of
the for loop. This will work as an infinite for loop.
#include<stdio.h>
void main ()
{
for(;;)
{
printf("welcome ");
}
}
What is the Continue Statement in C?
//loop statements
continue;
//some lines of the code which is to be skipped
Example of Continue Statement in C
#include<stdio.h>
void main ()
{
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
printf("%d\n", i);
}
include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
return 0;
}
What is the Break Statement in C?
void main ()
{
int n=1,i,choice;
do
{
i=1;
while(i<=10)
{
printf("%d X %d = %d\n",n,i,n*i);
i++;
}
printf("Do you want to further print the table of %d , enter any non-zero value to
continue.",n+1);
scanf("%d",&choice);
if(choice == 0)
{
break;
}
// C program to explain the use
// of continue statement with nested loops
#include <stdio.h>