0% found this document useful (0 votes)
9 views56 pages

Branching and Looping

The document provides an overview of control flow statements in C, including if-else statements, switch statements, and loops (for, while, do-while). It explains the syntax and usage of each statement, along with examples to illustrate their functionality. Additionally, it covers the break and continue statements, which are used to control loop execution.

Uploaded by

Shri ABCD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views56 pages

Branching and Looping

The document provides an overview of control flow statements in C, including if-else statements, switch statements, and loops (for, while, do-while). It explains the syntax and usage of each statement, along with examples to illustrate their functionality. Additionally, it covers the break and continue statements, which are used to control loop execution.

Uploaded by

Shri ABCD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Branching and Looping

C if else Statement

The if-else statement in C is used to perform the


operations based on some specific condition. The
operations specified in if block are executed if and
only if the given condition is true.
There are the following variants of if statement in C
language.
• If statement
• If-else statement
• If else-if ladder
• Nested if
If Statement

• The if statement is used to check some given


condition and perform some operations
depending upon the correctness of that
condition. It is mostly used in the scenario where
we need to perform the different operations for
the different conditions. The syntax of the if
statement is given below.
if(expression){
//code to be executed
}
Flowchart of if statement in C
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
printf(“Finished”);
return 0;
}
If-else Statement

• The if-else statement is used to perform two operations for a single


condition. The if-else statement is an extension to the if statement
using which, we can perform two different operations, i.e., one is
for the correctness of that condition, and the other is for the
incorrectness of the condition. Here, we must notice that if and else
block cannot be executed simiulteneously. Using if-else statement is
always preferable since it always invokes an otherwise case with
every if condition. The syntax of the if-else statement is given
below.
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Flowchart of the if-else statement in C
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
printf(“Finished”);
return 0;
}
If else-if ladder Statement

The if-else-if ladder statement is an extension to the if-


else statement. It is used in the scenario where there are
multiple cases to be performed for different conditions.
In if-else-if ladder statement, if a condition is true then
the statements defined in the if block will be executed,
otherwise if some other condition is true then the
statements defined in the else-if block will be executed,
at the last if none of the condition is true then the
statements defined in the else block will be executed.
There are multiple else-if blocks possible. It is similar to
the switch case statement where the default is executed
instead of else block if none of the cases is matched.
Syntax
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
nested if statements
It is always legal in C programming to nest if-else statements, which
means you can use one if or else if statement inside another if or else
if statement(s).
Syntax
The syntax for a nested if statement is as follows −

if( boolean_expression 1) {

/* Executes when the boolean expression 1 is true */


if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
#include <stdio.h>

int main () {

/* local variable definition */


int a = 100;
int b = 200;

/* check the boolean condition */


if( a == 100 ) {

/* if condition is true then check the following */


if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
C Switch Statement

• The switch statement in C is an alternate to if-


else-if ladder statement which allows us to
execute multiple operations for the different
possible values of a single variable called switch
variable. Here, We can define various statements
in the multiple cases for the different values of a
single variable.
The syntax of switch statement in c
language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

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

printf("Exact value of i is : %d\n", i );


printf("Exact value of j is : %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.

• The syntax of while loop in c language is given below:

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.

Syntax of for loop in C


The syntax of for loop in c language is given below:

for(Expression 1; Expression 2; Expression 3){


//code to be executed
}
C for loop Examples

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

• The expression represents the initialization of


the loop variable.
• We can initialize more than one variable in
Expression 1.
• Expression 1 is optional.
• #include <stdio.h>
• int main()
• {
• int a,b,c;
• for(a=0,b=12,c=23;a<2;a++)
• {
• printf("%d ",a+b+c);
• }
• }
• #include <stdio.h>
• int main()
• {
• int i=1;
• for(;i<5;i++)
• {
• printf("%d ",i);
• }
• }
Properties of Expression 2
• Expression 2 is a conditional expression. It checks for a
specific condition to be satisfied. If it is not, the loop is
terminated.
• Expression 2 can have more than one condition. However,
the loop will iterate until the last condition becomes false.
Other conditions will be treated as statements.
• Expression 2 is optional.
• Expression 2 can perform the task of expression 1 and
expression 3. That is, we can initialize the variable as well as
update the loop variable in expression 2 itself.
• We can pass zero or non-zero value in expression 2.
However, in C, any non-zero value is true, and zero is false
by default.
• #include <stdio.h>
• int main()
• {
• int i;
• for(i=0;i<=4;i++)
• {
• printf("%d ",i);
• }
• }
• #include <stdio.h>
• int main()
• {
• int i,j,k;
• for(i=0,j=0,k=0;i<4,k<8,j<10;i++)
• {
• printf("%d %d %d\n",i,j,k);
• j+=2;
• k+=3;
• }
• }
Properties of Expression 3

• Expression 3 is used to update the loop


variable.
• We can update more than one variable at the
same time.
• Expression 3 is optional.
#include<stdio.h>
void main ()
{
int i=0,j=2;
for(i = 0;i<5;i++,j=j+2)
{
printf("%d %d\n",i,j);
}
}
• Loop body
• The braces {} are used to define the scope of the loop. However, if the loop
contains only one statement, then we don't need to use braces. A loop without a
body is possible. The braces work as a block separator, i.e., the value variable
declared inside for loop is valid only for that block and not outside. Consider the
following example.

• #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?

• The continue statement is used to skip the


current iteration of any loop and bring the
control to the beginning of the iteration. The
statements following the continue statement
are skipped and the iteration starts again.
Syntax

//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?

• If you remember we already came across the break


statement while learning the switch statement. We
used to check each case in switch and after finding the
correct match used to get out using break.
• It is one of the most used jump statements.
• In C, break is used to prematurely exit from a loop or
switch statement, before the block has been fully
executed.
• As soon as a break statement is encountered inside a
loop, the loop terminates immediately, and control is
transferred to the next statement outside the loop.
Syntax

//loop or switch case


break;
Example of Break Statement in C
#include <stdio.h>
int main()
{
for(int i=1; i <=10; i++)
{
if(i == 5)
{
break; // terminates the loop when i is equal to 5
}
printf("%d\n", i);
}
return 0;
}
Example of Break statement in a for
loop
#include<stdio.h>
void main ()
{
int i = 0;
while(1)
{ printf("%d\n",i);
i++;
if(i == 5)
break;
}
printf("The loop terminates");
}
Example of Break statement in a
do...while loop
#include<stdio.h>

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>

int main() C Program to use continue in a nested loop


{

// outer loop with 3 iterations


for (int i = 1; i <= 3; i++) {
// inner loop to print integer 1 to 4
for (int j = 0; j <= 4; j++) {

// continue to skip printing number 3


if (j == 3) {
continue;
}
printf("%d ", j);
}
printf("\n");
}

You might also like