SlideShare a Scribd company logo
Conditional execution and selection
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. Let's
observe an example of nesting loops in C.
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
1. Outer_loop
2. {
3. Inner_loop
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }
Outer_loop and Inner_loop are the valid loops that can be a 'for'
loop, 'while' loop or 'do-while' loop.
Nested for loop
The nested for loop means any type of loop which is defined inside the
'for' loop.
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
Example of nested for loop
#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%dt",(i*j)); // printing the value.
}
printf("n");
}
Explanation of the above code
o First, the 'i' variable is initialized to 1 and then program control
passes to the i<=n.
o The program control checks whether the condition 'i<=n' is true
or not.
o If the condition is true, then the program control passes to the
inner loop.
o The inner loop will get executed until the condition is true.
o After the execution of the inner loop, the control moves back to
the update of the outer loop, i.e., i++.
o After incrementing the value of the loop counter, the condition is
checked again, i.e., i<=n.
o If the condition is true, then the inner loop will be executed
again.
o This process will continue until the condition of the outer loop is
true.
Output:
Nested while loop
The nested while loop means any type of loop which is defined inside
the 'while' loop.
while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}
Example of nested while loop
#include <stdio.h>
int main()
{
int rows; // variable declaration
int columns; // variable declaration
int k=1; // variable initialization
printf("Enter the number of rows :"); // input the number of rows.
scanf("%d",&rows);
printf("nEnter the number of columns :"); // input the number of columns
scanf("%d",&columns);
int a[rows][columns]; //2d array declaration
int i=1;
while(i<=rows) // outer loop
{
int j=1;
while(j<=columns) // inner loop
{
printf("%dt",k); // printing the value of k.
k++; // increment counter
j++;
}
i++;
printf("n");
}
}
Explanation of the above code.
o We have created the 2d array, i.e., int a[rows][columns].
o The program initializes the 'i' variable by 1.
o Now, control moves to the while loop, and this loop checks
whether the condition is true, then the program control moves to
the inner loop.
o After the execution of the inner loop, the control moves to the
update of the outer loop, i.e., i++.
o After incrementing the value of 'i', the condition (i<=rows) is
checked.
o If the condition is true, the control then again moves to the inner
loop.
o This process continues until the condition of the outer loop is
true.
Nested do..while loop
The nested do..while loop means any type of loop which is defined
inside the 'do..while' loop.
do
{
do
{
// inner loop statements.
}
while(condition);
// outer loop statements.
}
while(condition);
Example of nested do..while loop.
#include <stdio.h>
int main()
{
/*printing the pattern
********
********
********
******** */
int i=1;
do // outer loop
{
int j=1;
do // inner loop
{
printf("*");
j++;
}
while(j<=8);
printf("n");
i++;
}
while(i<=4);
}
Output:
Explanation of the above code.
o First, we initialize the outer loop counter variable, i.e., 'i' by 1.
o As we know that the do..while loop executes once without
checking the condition, so the inner loop is executed without
checking the condition in the outer loop.
o After the execution of the inner loop, the control moves to the
update of the i++.
o When the loop counter value is incremented, the condition is
checked. If the condition in the outer loop is true, then the inner
loop is executed.
o This process will continue until the condition in the outer loop is
true.
Array
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which
can store the primitive type of data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers,
structure, etc. The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
C array is beneficial if you have to store similar elements.
For example, if we want to store the marks of a student in 6 subjects, then we don't
need to define different variables for the marks in the different subject.
Instead of that, we can define an array which can store the marks in each subject at
the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
Properties of Array
The array contains the following properties.
o Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
o Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code
only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we
can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we
will learn later.
Declaration of C Array a[10]
We can declare an array in the c language in the following way.
1. data_type array_name[array_size];
Now, let us see the example to declare the array.
1. int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We can
initialize each element of the array by using the index. Consider the following example.
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;
C array example
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d n",marks[i]);
}//end of for loop
return 0;
}

More Related Content

PPT
Arrays Basics
PPT
Two Dimensional Array
PPTX
Break and continue in C
PPTX
Conditional Statement in C#
PPT
Arrays Data Structure
PPTX
DATA STRUCTURE CLASS 12 .pptx
PPTX
Virtual function and abstract class
PDF
Dsa circular queue
Arrays Basics
Two Dimensional Array
Break and continue in C
Conditional Statement in C#
Arrays Data Structure
DATA STRUCTURE CLASS 12 .pptx
Virtual function and abstract class
Dsa circular queue

What's hot (20)

PPTX
Python- Regular expression
PPT
Object-Oriented Programming Concepts
PDF
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
PDF
Exception handling
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
PPTX
Python comments and variables.pptx
PPTX
Java if else condition - powerpoint persentation
PPTX
Introduction to Data Structures & Algorithms
PDF
Left and Right Folds - Comparison of a mathematical definition and a programm...
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
PDF
Assembly language (coal)
PPT
Chapter 3 ds
PPTX
Functions in c
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PDF
Features of c
PPTX
Chapter 04 object oriented programming
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPTX
Object Oriented Program Class 12 Computer Science
PPTX
concept of oops
Python- Regular expression
Object-Oriented Programming Concepts
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Exception handling
Computer Science-Data Structures :Abstract DataType (ADT)
Python comments and variables.pptx
Java if else condition - powerpoint persentation
Introduction to Data Structures & Algorithms
Left and Right Folds - Comparison of a mathematical definition and a programm...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly language (coal)
Chapter 3 ds
Functions in c
358 33 powerpoint-slides_14-sorting_chapter-14
Features of c
Chapter 04 object oriented programming
Data Structure and Algorithms The Tower of Hanoi
Object Oriented Program Class 12 Computer Science
concept of oops
Ad

Similar to Nested Loops in C unit2.docx (20)

PPTX
Nested Loops in C.pptx
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PPTX
C programming language tutorial
PPT
12 lec 12 loop
PDF
C programing Tutorial
PPTX
matrices_and_loops.pptx
PPTX
What is c
ODP
Programming basics
PPTX
All type looping information and clearly
PDF
175035 cse lab-05
PDF
C_Program_Yr1[1].pdf for computer science
PPTX
Introduction to C programming
PPTX
C language (Part 2)
PPTX
C programming language tutorial
PPT
Chapter06.PPT
PPTX
CHAPTER 5
PPTX
Programming in C
Nested Loops in C.pptx
Lecture 1 mte 407
Lecture 1 mte 407
C programming language tutorial
12 lec 12 loop
C programing Tutorial
matrices_and_loops.pptx
What is c
Programming basics
All type looping information and clearly
175035 cse lab-05
C_Program_Yr1[1].pdf for computer science
Introduction to C programming
C language (Part 2)
C programming language tutorial
Chapter06.PPT
CHAPTER 5
Programming in C
Ad

More from JavvajiVenkat (6)

PPTX
CONTROL STMTS.pptx
DOCX
itretion.docx
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
DOCX
C Control Statements.docx
DOCX
loops and iteration.docx
DOCX
UNIT-II CP DOC.docx
CONTROL STMTS.pptx
itretion.docx
unit2 C-ProgrammingChapter 2 Control statements.pptx
C Control Statements.docx
loops and iteration.docx
UNIT-II CP DOC.docx

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Digital Logic Computer Design lecture notes
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Welding lecture in detail for understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
R24 SURVEYING LAB MANUAL for civil enggi
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Digital Logic Computer Design lecture notes
Mechanical Engineering MATERIALS Selection
Welding lecture in detail for understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Internet of Things (IOT) - A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx

Nested Loops in C unit2.docx

  • 1. Conditional execution and selection 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. Let's observe an example of nesting loops in C. 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 1. Outer_loop 2. { 3. Inner_loop 4. { 5. // inner loop statements. 6. } 7. // outer loop statements. 8. } Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop. Nested for loop The nested for loop means any type of loop which is defined inside the 'for' loop. for (initialization; condition; update) { for(initialization; condition; update)
  • 2. { // inner loop statements. } // outer loop statements. } Example of nested for loop #include <stdio.h> int main() { int n;// variable declaration printf("Enter the value of n :"); // Displaying the n tables. for(int i=1;i<=n;i++) // outer loop { for(int j=1;j<=10;j++) // inner loop { printf("%dt",(i*j)); // printing the value. } printf("n"); } Explanation of the above code o First, the 'i' variable is initialized to 1 and then program control passes to the i<=n. o The program control checks whether the condition 'i<=n' is true or not.
  • 3. o If the condition is true, then the program control passes to the inner loop. o The inner loop will get executed until the condition is true. o After the execution of the inner loop, the control moves back to the update of the outer loop, i.e., i++. o After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n. o If the condition is true, then the inner loop will be executed again. o This process will continue until the condition of the outer loop is true. Output: Nested while loop The nested while loop means any type of loop which is defined inside the 'while' loop. while(condition) { while(condition)
  • 4. { // inner loop statements. } // outer loop statements. } Example of nested while loop #include <stdio.h> int main() { int rows; // variable declaration int columns; // variable declaration int k=1; // variable initialization printf("Enter the number of rows :"); // input the number of rows. scanf("%d",&rows); printf("nEnter the number of columns :"); // input the number of columns scanf("%d",&columns); int a[rows][columns]; //2d array declaration int i=1; while(i<=rows) // outer loop { int j=1; while(j<=columns) // inner loop { printf("%dt",k); // printing the value of k. k++; // increment counter j++;
  • 5. } i++; printf("n"); } } Explanation of the above code. o We have created the 2d array, i.e., int a[rows][columns]. o The program initializes the 'i' variable by 1. o Now, control moves to the while loop, and this loop checks whether the condition is true, then the program control moves to the inner loop. o After the execution of the inner loop, the control moves to the update of the outer loop, i.e., i++. o After incrementing the value of 'i', the condition (i<=rows) is checked. o If the condition is true, the control then again moves to the inner loop. o This process continues until the condition of the outer loop is true.
  • 6. Nested do..while loop The nested do..while loop means any type of loop which is defined inside the 'do..while' loop. do { do { // inner loop statements. } while(condition); // outer loop statements. } while(condition);
  • 7. Example of nested do..while loop. #include <stdio.h> int main() { /*printing the pattern ******** ******** ******** ******** */ int i=1; do // outer loop { int j=1; do // inner loop { printf("*"); j++; } while(j<=8); printf("n"); i++; } while(i<=4); } Output:
  • 8. Explanation of the above code. o First, we initialize the outer loop counter variable, i.e., 'i' by 1. o As we know that the do..while loop executes once without checking the condition, so the inner loop is executed without checking the condition in the outer loop. o After the execution of the inner loop, the control moves to the update of the i++. o When the loop counter value is incremented, the condition is checked. If the condition in the outer loop is true, then the inner loop is executed. o This process will continue until the condition in the outer loop is true. Array An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.
  • 9. C array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. By using the array, we can access the elements easily. Only a few lines of code are required to access the elements of the array. Properties of Array The array contains the following properties. o Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. o Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. o Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Advantage of C Array 1) Code Optimization: Less code to the access the data. 2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting: To sort the elements of the array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array. Disadvantage of C Array 1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.
  • 10. Declaration of C Array a[10] We can declare an array in the c language in the following way. 1. data_type array_name[array_size]; Now, let us see the example to declare the array. 1. int marks[5]; Here, int is the data_type, marks are the array_name, and 5 is the array_size. Initialization of C Array The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example. 1. marks[0]=80;//initialization of array 2. marks[1]=60; 3. marks[2]=70; 4. marks[3]=85; 5. marks[4]=75; C array example #include<stdio.h> int main(){ int i=0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85;
  • 11. marks[4]=75; //traversal of array for(i=0;i<5;i++){ printf("%d n",marks[i]); }//end of for loop return 0; }