SlideShare a Scribd company logo
1 of 25
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer
While Loop
Dr. Yousaf, PIEAS
Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("i = %dn", i);
i++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
while Loop
For loops can usually be rewritten as while
loops:
initialization;
while ( loopContinuationTest )
{
statement;
increment;
}
Dr. Yousaf, PIEAS
while Loop
• Generic Form
while (condition)
statement
• Executes as expected:
1. condition is evaluated
2. If condition is false (i.e. 0), loop is exited (go to step 5)
3. If condition is true (i.e. nonzero), statement is executed
4. Go to step 1
5. Next statement
• Note:
for (exp1; exp2; exp3)
stmt;
is equivalent to
exp1;
while(exp2)
{ stmt; exp3; }
Dr. Yousaf, PIEAS
// While loop is a top-test loop
#include<stdio.h>
int main()
{
int x = 7;
while (x < 9)
{
printf(" %d is less than 9n", x);
x++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
while Loop
#include <stdio.h>
int main()
{
int x = 0; /* Don't forget to declare variables */
while ( x < 10 )
{ /* While x is less than 10 */
printf( "%dn", x );
x++; /* Update x so the condition can be met
eventually */
}
return 0;
}
Dr. Yousaf, PIEAS
do while Loop
Dr. Yousaf, PIEAS
Counting from 1 to 10
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("i = %dn", i);
i++;
} while (i <= 10) ;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
// Do While loop demonstration
// It is a bottom-test loop instead of a top-test loop
#include<stdio.h>
int main()
{
int x = 11;
do
{
printf(" %d is less than 9n", x);
x++;
} while (x < 9);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
• The do/while repetition structure
– Similar to the while structure
– Condition for repetition tested after the body of
the loop is performed
• All actions are performed at least once
– Generic Format:
do {
statement;
} while ( condition );
Dr. Yousaf, PIEAS
The do while Loop
The do while Loop
• Standard repeat until loop
• Like a while loop, but with condition test at bottom.
• Always executes at least once.
• The semantics of do...while:
1. Execute statement
2. Evaluate condition
3. If condition is true go to step 1
4. Next statement
Dr. Yousaf, PIEAS
Flowchart of the do/while Loop
true
false
action(s)
condition
Dr. Yousaf, PIEAS
The do while Loop
#include <stdio.h>
int main()
{
int x;
x = 0;
do
{
/* The value of x will be printed at least one time
even though the condition is false*/
printf( "%dn", x );
x++;
} while ( x != 10 );
getcahr(); return 0;
}
Dr. Yousaf, PIEAS
Comparison of
for, while, and do-while
loops
Dr. Yousaf, PIEAS
Loops
for (i = 0; i < 12; i++)
{
dowork();
}
i = 0;
while ( i < 12)
{
dowork();
i++;
}
i = 0;
do
{
dowork();
i++;
} while (i < 12); // use of semicolon
Dr. Yousaf, PIEAS
for loop
Preferably used where exact number of
iterations are known in prior.
Dr. Yousaf, PIEAS
The while Repetition Structure
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;
product <= 1000 product = 2 * product
true
false
Dr. Yousaf, PIEAS
The while Repetition Structure
• Repetition structure
– Programmer specifies an action to be repeated
while some condition remains true
– Psuedocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
– while loop repeated until condition becomes
false
Dr. Yousaf, PIEAS
do while loop
Preferably used where loop should must run
atleast once.
Dr. Yousaf, PIEAS
Examples
Dr. Yousaf, PIEAS
Write a complete and an efficient C program that takes an
integer number from the user and calculates its factorial.
#include <stdio.h>
int main()
{
int n, num=1,factorial=1;
printf(“Enter value of N: ”);
scanf(“%d”,&n);
for(num=1;num<=n;num++)
{
factorial=factorial*num;
}
printf(”The factorial of %d = %d “ ,n,factorial);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
For Loop -- Example
Write a C program that gets a number from the user. If the number is even
the program will terminate otherwise the program will get another number.
The while loop should terminate only when an even number is provided.
Dr. Yousaf, PIEAS
while Loop -- Example
Write a C program that gets a number from the user. If the number is even
the program will terminate otherwise the program will get another number.
The while loop should terminate only when an even number is provided.
#include <stdio.h>
int main()
{
int a=1;
while(a%2!=0)
{
printf(“n Enter Value of a = “);
scanf(“%d”,&a);
}
printf(“nEnd of While loop”);
getchar(); return 0; } Dr. Yousaf, PIEAS
while Loop -- Example
Try to print even numbers between two given integer
numbers A and B such that A can be greater or smaller
than B. Numbers A and B are to be taken from the
user.
(Try it Yourself)
Dr. Yousaf, PIEAS
while Loop -- Example

More Related Content

Similar to C Language Lecture 6

Similar to C Language Lecture 6 (20)

Loops in c
Loops in cLoops in c
Loops in c
 
Bsit1
Bsit1Bsit1
Bsit1
 
M C6java6
M C6java6M C6java6
M C6java6
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C++ loop
C++ loop C++ loop
C++ loop
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Loops c++
Loops c++Loops c++
Loops c++
 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 

More from Shahzaib Ajmal (15)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 

C Language Lecture 6

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer
  • 3. Printing the counting from 1 to 10 #include<stdio.h> int main() { int i = 1; while (i <= 10) { printf("i = %dn", i); i++; } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 4. while Loop For loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest ) { statement; increment; } Dr. Yousaf, PIEAS
  • 5. while Loop • Generic Form while (condition) statement • Executes as expected: 1. condition is evaluated 2. If condition is false (i.e. 0), loop is exited (go to step 5) 3. If condition is true (i.e. nonzero), statement is executed 4. Go to step 1 5. Next statement • Note: for (exp1; exp2; exp3) stmt; is equivalent to exp1; while(exp2) { stmt; exp3; } Dr. Yousaf, PIEAS
  • 6. // While loop is a top-test loop #include<stdio.h> int main() { int x = 7; while (x < 9) { printf(" %d is less than 9n", x); x++; } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 7. while Loop #include <stdio.h> int main() { int x = 0; /* Don't forget to declare variables */ while ( x < 10 ) { /* While x is less than 10 */ printf( "%dn", x ); x++; /* Update x so the condition can be met eventually */ } return 0; } Dr. Yousaf, PIEAS
  • 8. do while Loop Dr. Yousaf, PIEAS
  • 9. Counting from 1 to 10 #include<stdio.h> int main() { int i = 1; do { printf("i = %dn", i); i++; } while (i <= 10) ; getchar(); return 0; } Dr. Yousaf, PIEAS
  • 10. // Do While loop demonstration // It is a bottom-test loop instead of a top-test loop #include<stdio.h> int main() { int x = 11; do { printf(" %d is less than 9n", x); x++; } while (x < 9); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 11. • The do/while repetition structure – Similar to the while structure – Condition for repetition tested after the body of the loop is performed • All actions are performed at least once – Generic Format: do { statement; } while ( condition ); Dr. Yousaf, PIEAS The do while Loop
  • 12. The do while Loop • Standard repeat until loop • Like a while loop, but with condition test at bottom. • Always executes at least once. • The semantics of do...while: 1. Execute statement 2. Evaluate condition 3. If condition is true go to step 1 4. Next statement Dr. Yousaf, PIEAS
  • 13. Flowchart of the do/while Loop true false action(s) condition Dr. Yousaf, PIEAS
  • 14. The do while Loop #include <stdio.h> int main() { int x; x = 0; do { /* The value of x will be printed at least one time even though the condition is false*/ printf( "%dn", x ); x++; } while ( x != 10 ); getcahr(); return 0; } Dr. Yousaf, PIEAS
  • 15. Comparison of for, while, and do-while loops Dr. Yousaf, PIEAS
  • 16. Loops for (i = 0; i < 12; i++) { dowork(); } i = 0; while ( i < 12) { dowork(); i++; } i = 0; do { dowork(); i++; } while (i < 12); // use of semicolon Dr. Yousaf, PIEAS
  • 17. for loop Preferably used where exact number of iterations are known in prior. Dr. Yousaf, PIEAS
  • 18. The while Repetition Structure Example: int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 product = 2 * product true false Dr. Yousaf, PIEAS
  • 19. The while Repetition Structure • Repetition structure – Programmer specifies an action to be repeated while some condition remains true – Psuedocode: While there are more items on my shopping list Purchase next item and cross it off my list – while loop repeated until condition becomes false Dr. Yousaf, PIEAS
  • 20. do while loop Preferably used where loop should must run atleast once. Dr. Yousaf, PIEAS
  • 22. Write a complete and an efficient C program that takes an integer number from the user and calculates its factorial. #include <stdio.h> int main() { int n, num=1,factorial=1; printf(“Enter value of N: ”); scanf(“%d”,&n); for(num=1;num<=n;num++) { factorial=factorial*num; } printf(”The factorial of %d = %d “ ,n,factorial); getchar(); return 0; } Dr. Yousaf, PIEAS For Loop -- Example
  • 23. Write a C program that gets a number from the user. If the number is even the program will terminate otherwise the program will get another number. The while loop should terminate only when an even number is provided. Dr. Yousaf, PIEAS while Loop -- Example
  • 24. Write a C program that gets a number from the user. If the number is even the program will terminate otherwise the program will get another number. The while loop should terminate only when an even number is provided. #include <stdio.h> int main() { int a=1; while(a%2!=0) { printf(“n Enter Value of a = “); scanf(“%d”,&a); } printf(“nEnd of While loop”); getchar(); return 0; } Dr. Yousaf, PIEAS while Loop -- Example
  • 25. Try to print even numbers between two given integer numbers A and B such that A can be greater or smaller than B. Numbers A and B are to be taken from the user. (Try it Yourself) Dr. Yousaf, PIEAS while Loop -- Example