SlideShare a Scribd company logo
1 of 11
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

What's hot

Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsTomer Ben David
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C Self employed
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3Rumman Ansari
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in pythonnitamhaske
 
Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Young Ha Kim
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 

What's hot (19)

Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Storage classes
Storage classesStorage classes
Storage classes
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and Pitfals
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
 
Fifth session
Fifth sessionFifth session
Fifth session
 
Storage classes
Storage classesStorage classes
Storage classes
 
C language presentation
C language presentationC language presentation
C language presentation
 
L8
L8L8
L8
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
강의자료8
강의자료8강의자료8
강의자료8
 
C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
 
Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Queue
QueueQueue
Queue
 

Similar to Nested Loops in C unit2.docx

Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx320126552027SURAKATT
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in Carshpreetkaur07
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forpattinsonhenry524
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second sessionAhmad Bashar Eter
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptxMrhaider4
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cppsharvivek
 
Break and continue in C
Break and continue in C Break and continue in C
Break and continue in C vishnupriyapm4
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 

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

Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Nested Loops in C.pptx
Nested Loops in C.pptxNested Loops in C.pptx
Nested Loops in C.pptx
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx for
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Java part 2
Java part  2Java part  2
Java part 2
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
 
Break and continue in C
Break and continue in C Break and continue in C
Break and continue in C
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
JAVA LESSON-02.pptx
JAVA LESSON-02.pptxJAVA LESSON-02.pptx
JAVA LESSON-02.pptx
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Java best practices
Java best practicesJava best practices
Java best practices
 

More from JavvajiVenkat

unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxJavvajiVenkat
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 

More from JavvajiVenkat (6)

CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 

Recently uploaded

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 

Recently uploaded (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 

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