SlideShare a Scribd company logo
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer
switch statement
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The number must be
any integer value from 1 to 7n");
scanf("%d",&day);
if (day == 1)
printf("Mondayn");
else if (day == 2)
printf("Tuesdayn");
else if (day == 3)
printf("Wednesdayn");
// You may complete it yourself
Dr. Yousaf, PIEAS
The if/else Selection Structure
Nested if/else structures
• In previous example, the nested if/else structure is to
be used.
• Test for multiple cases by placing if/else selection
structures inside if/else selection structures
• Deep indentation usually not used in practice
• How can we solve this example more conveniently?
• Switch statement is a convenient way to code it.
Dr. Yousaf, PIEAS
Switch Statement
If you have a large decision tree, and all
the decisions depend on the value of the
same variable, you will probably want to
consider a switch statement instead of a
ladder of if...else or else if constructions.
Dr. Yousaf, PIEAS
// Example of switch statement
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The
number must be any integer value from
1 to 7n");
scanf("%d",&day);
// Contd. (next page)
Dr. Yousaf, PIEAS
switch(day)
{
case 1: // 1 is one of possible value of day and so on.
printf("Mondayn");
break;
case 2:
printf("Tuesdayn");
break;
// please write cases 3 to 6 yourself
case 7:
printf("Sundayn");
break;
default:
printf("The number must be any integer value from 1 to 7n");
break;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
• switch
– Useful when a variable or expression is tested for all the
values which can happen and different actions are taken
• Format
– Series of case labels and an optional default case
switch ( value )
{
case 1:
actions
case 2:
actions
default:
actions
}
– break; exits from structure
Dr. Yousaf, PIEAS
The switch Structure
• Flowchart of the switch structure
true
false
.
.
.
case a case a
action(s)
break
case b case b
action(s)
break
false
false
case z case z
action(s)
break
true
true
default
action(s)
Dr. Yousaf, PIEAS
• The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed in
the default case.
Dr. Muhammad Yousaf Hamza
Switch Statement
Mathematical Functions
Dr. Yousaf, PIEAS
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
y = sin(PI/2.0); // argument is in radian
printf("%lf", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%lf", y);
getchar(); return 0; }
Output: 1.000000
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h>
#include<math.h>
int main()
{
int k, j, r;
double x = 25.0, y, z;
y = sqrt(x);
printf("y = %lfn", y); // y = 5.000000
z = pow(y,3); // y^3, z = 125.000000
printf(" z = %lfn", z);
k = floor(15.2);
printf(" k = %dn",k);
// output: 15
j = ceil (15.2);
printf(" j = %dn",j);
// output: 16
r = abs (-67);
printf(" r = %dn",r);
// output: 67
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*");
getchar();
return 0;
}
Examples
Write a program that prints 2 stars as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 2 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*n*");
getchar();
return 0;
}
Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*n*n*n*n*n*n*n*n*n*");
getchar();
return 0;
}
Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Dr. Yousaf, PIEAS
Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Too much labor work?
There MUST be an easier way
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting 1 to 10 as
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting 1 to 10 as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("1n2n3n4n5n6n7n8n9n10");
getchar();
return 0;
}
Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
• Too much labor work?
• There MUST be an easier way
• Is there any?
• YES
LOOPS
Dr. Yousaf, PIEAS
Loops
Dr. Yousaf, PIEAS
For Loop
Dr. Yousaf, PIEAS
Printing the stars
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
printf("*n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
printf("%dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 1000
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 1000; i++)
printf("i = %dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
20-Spans System
I/P
O/P
80 km
80 km 80 km
Span 1
OA
DCF1
Span 2 Span 20
OAOA OADCF2 DCF20OA
The for Statement
• The most important looping structure in C.
• Generic Form:
for (initial ; condition ; increment )
statement
• initial, condition, and increment are C expressions.
• For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
The for Statement
//For statement examples
#include <stdio.h>
int main ()
{
int count;
/* 1. simple counted for loop */
printf("Output with increment in loopn");
for (count =1; count <=10; count++)
printf ("%dn", count);
/* 2. counting backwards */
printf("Output with decrement in loopn");
for (count = 56; count >48; count--)
printf("%dn", count);
/* 3. for loop counting by
5's */
printf("Output with increment
of 5n");
for (count=0; count<32;
count += 5)
printf("%dn", count);
getchar();
return 0;
}
The for Statement
#include <stdio.h>
int main ()
{
int count;
/* initialization outside of loop */
count = 1;
for ( ; count < 7; count++)
printf("%d ", count);
printf(“n");
/* increment outside of loop */
count = 1;
for ( ; count < 7; )
{
printf("%d ", count);
count++;
}
getchar(); return 0; }
#include <stdio.h>
int main ()
{
int count;
int x, y;
/* compound statements increment */
for (x=0, y=100; x<y; x++, y--)
{
printf("%d, %dn", x,y);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The for Statement
The for Repetition Structure
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example:
for (i = 1; i <= 10; i++)
printf("i = %dn", i);
–Prints the integers from one to ten
No
semicolon
(;) after last
expression
Dr. Yousaf, PIEAS
The for Structure: Observations
• Arithmetic expressions
Initialization, loop-continuation, and increment can
contain arithmetic expressions.
x = 2;
y = 10;
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
Dr. Yousaf, PIEAS
The for Structure: Observations
• Notes about the for structure:
– "Increment" may be negative (decrement)
– If the loop continuation condition is initially
false
• The body of the for structure is not
performed
• Control proceeds with the next statement
after the for structure
– Control variable // for (i = 1; i <= 10; i++)
• Often printed or used inside for body, but not
necessary
Dr. Yousaf, PIEAS
For Loop
#include <stdio.h>
int main()
{
int x;
/* The loop goes if x < 10, and x increases by one in every
loop*/
for ( x = 0; x < 10; x++ )
{
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */
printf( "%dn", x );
}
return 0;
}
Dr. Yousaf, PIEAS

More Related Content

Similar to C Language Lecture 5 (20)

PDF
C Language Lecture 7
Shahzaib Ajmal
 
PDF
C Language Lecture 17
Shahzaib Ajmal
 
PDF
C Language Lecture 6
Shahzaib Ajmal
 
PPTX
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
PDF
C Language Lecture 16
Shahzaib Ajmal
 
PDF
C Language Lecture 4
Shahzaib Ajmal
 
PDF
C Language Lecture 3
Shahzaib Ajmal
 
PPTX
CONTROL FLOW in C.pptx
SmitaAparadh
 
PDF
C Language Lecture 9
Shahzaib Ajmal
 
PDF
control statement
Kathmandu University
 
PDF
Programming Fundamentals Decisions
imtiazalijoono
 
PDF
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
PPTX
LAB PROGRAMS SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
PPTX
unit 2-Control Structures.pptx
ishaparte4
 
PPTX
Introduction to c
Sayed Ahmed
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PPSX
Bsit1
jigeno
 
PDF
C Language Lecture 13
Shahzaib Ajmal
 
PDF
POP Unit 2.pptx.pdf for your time and gauss with example
siddarameshav871
 
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 6
Shahzaib Ajmal
 
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
C Language Lecture 16
Shahzaib Ajmal
 
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 3
Shahzaib Ajmal
 
CONTROL FLOW in C.pptx
SmitaAparadh
 
C Language Lecture 9
Shahzaib Ajmal
 
control statement
Kathmandu University
 
Programming Fundamentals Decisions
imtiazalijoono
 
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
LAB PROGRAMS SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
unit 2-Control Structures.pptx
ishaparte4
 
Introduction to c
Sayed Ahmed
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
Bsit1
jigeno
 
C Language Lecture 13
Shahzaib Ajmal
 
POP Unit 2.pptx.pdf for your time and gauss with example
siddarameshav871
 

More from Shahzaib Ajmal (9)

PDF
C Language Lecture 21
Shahzaib Ajmal
 
PDF
C Language Lecture 20
Shahzaib Ajmal
 
PDF
C Language Lecture 15
Shahzaib Ajmal
 
PDF
C Language Lecture 14
Shahzaib Ajmal
 
PDF
C Language Lecture 12
Shahzaib Ajmal
 
PDF
C Language Lecture 11
Shahzaib Ajmal
 
PDF
C Language Lecture 10
Shahzaib Ajmal
 
PDF
C Language Lecture 2
Shahzaib Ajmal
 
PDF
C Language Lecture 1
Shahzaib Ajmal
 
C Language Lecture 21
Shahzaib Ajmal
 
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 15
Shahzaib Ajmal
 
C Language Lecture 14
Shahzaib Ajmal
 
C Language Lecture 12
Shahzaib Ajmal
 
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
Shahzaib Ajmal
 
Ad

Recently uploaded (20)

PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Introduction to Probability(basic) .pptx
purohitanuj034
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Introduction to Probability(basic) .pptx
purohitanuj034
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
John Keats introduction and list of his important works
vatsalacpr
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Ad

C Language Lecture 5

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer
  • 3. #include <stdio.h> int main() { int day; printf("The day number 1 means Mondayn"); printf("Please enter the number of day. n The number must be any integer value from 1 to 7n"); scanf("%d",&day); if (day == 1) printf("Mondayn"); else if (day == 2) printf("Tuesdayn"); else if (day == 3) printf("Wednesdayn"); // You may complete it yourself Dr. Yousaf, PIEAS
  • 4. The if/else Selection Structure Nested if/else structures • In previous example, the nested if/else structure is to be used. • Test for multiple cases by placing if/else selection structures inside if/else selection structures • Deep indentation usually not used in practice • How can we solve this example more conveniently? • Switch statement is a convenient way to code it. Dr. Yousaf, PIEAS
  • 5. Switch Statement If you have a large decision tree, and all the decisions depend on the value of the same variable, you will probably want to consider a switch statement instead of a ladder of if...else or else if constructions. Dr. Yousaf, PIEAS
  • 6. // Example of switch statement #include <stdio.h> int main() { int day; printf("The day number 1 means Mondayn"); printf("Please enter the number of day. n The number must be any integer value from 1 to 7n"); scanf("%d",&day); // Contd. (next page) Dr. Yousaf, PIEAS
  • 7. switch(day) { case 1: // 1 is one of possible value of day and so on. printf("Mondayn"); break; case 2: printf("Tuesdayn"); break; // please write cases 3 to 6 yourself case 7: printf("Sundayn"); break; default: printf("The number must be any integer value from 1 to 7n"); break; } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 8. • switch – Useful when a variable or expression is tested for all the values which can happen and different actions are taken • Format – Series of case labels and an optional default case switch ( value ) { case 1: actions case 2: actions default: actions } – break; exits from structure Dr. Yousaf, PIEAS
  • 9. The switch Structure • Flowchart of the switch structure true false . . . case a case a action(s) break case b case b action(s) break false false case z case z action(s) break true true default action(s) Dr. Yousaf, PIEAS
  • 10. • The expression used in a switch statement must have an integral or character type. • You can have any number of case statements within a switch. Each case is followed by the value to be compared and a colon. • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Dr. Muhammad Yousaf Hamza Switch Statement
  • 12. #include<stdio.h> #include<math.h> // use of math.h #define PI 3.14 int main() { double y; y = sin(PI/2.0); // argument is in radian printf("%lf", y); getchar(); return 0; } Output: 1.000000 Dr. Yousaf, PIEAS
  • 13. // If given theta is in degree, then convert it first into radians. #include<stdio.h> #include<math.h> // use of math.h #define PI 3.14 int main() { double y; float theta_deg, theta_rad; theta_deg = 90.0; theta_rad = (PI/180)*theta_deg; y = sin(theta_rad); // argument is in radian printf("%lf", y); getchar(); return 0; } Output: 1.000000 Dr. Yousaf, PIEAS
  • 15. • log() ……..> natural log • log10() …………> base-10 logarithm Dr. Yousaf, PIEAS
  • 17. #include<stdio.h> #include<math.h> int main() { int k, j, r; double x = 25.0, y, z; y = sqrt(x); printf("y = %lfn", y); // y = 5.000000 z = pow(y,3); // y^3, z = 125.000000 printf(" z = %lfn", z); k = floor(15.2); printf(" k = %dn",k); // output: 15 j = ceil (15.2); printf(" j = %dn",j); // output: 16 r = abs (-67); printf(" r = %dn",r); // output: 67 getchar(); return 0; } Dr. Yousaf, PIEAS
  • 19. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS
  • 20. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*"); getchar(); return 0; }
  • 21. Examples Write a program that prints 2 stars as Dr. Yousaf, PIEAS
  • 22. Examples Write a program that prints 2 stars as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*n*"); getchar(); return 0; }
  • 23. Examples Write a program that prints 10 stars as Dr. Yousaf, PIEAS
  • 24. Examples Write a program that prints 10 stars as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*n*n*n*n*n*n*n*n*n*"); getchar(); return 0; }
  • 25. Examples Write a program that prints 100 stars in the format shown in the previous slides. Dr. Yousaf, PIEAS
  • 26. Examples Write a program that prints 100 stars in the format shown in the previous slides. Too much labor work? There MUST be an easier way Dr. Yousaf, PIEAS
  • 27. Examples Write a program that prints counting 1 to 10 as Dr. Yousaf, PIEAS
  • 28. Examples Write a program that prints counting 1 to 10 as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("1n2n3n4n5n6n7n8n9n10"); getchar(); return 0; }
  • 29. Examples Write a program that prints counting from 1 to 1000 in the format shown in the previous slide. Dr. Yousaf, PIEAS
  • 30. Examples Write a program that prints counting from 1 to 1000 in the format shown in the previous slide. • Too much labor work? • There MUST be an easier way • Is there any? • YES LOOPS Dr. Yousaf, PIEAS
  • 33. Printing the stars #include<stdio.h> int main() { int i; for (i = 1; i <= 10; i++) printf("*n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 34. Printing the counting from 1 to 10 #include<stdio.h> int main() { int i; for (i = 1; i <= 10; i++) printf("%dn", i); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 35. Printing the counting from 1 to 1000 #include<stdio.h> int main() { int i; for (i = 1; i <= 1000; i++) printf("i = %dn", i); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 36. 20-Spans System I/P O/P 80 km 80 km 80 km Span 1 OA DCF1 Span 2 Span 20 OAOA OADCF2 DCF20OA
  • 37. The for Statement • The most important looping structure in C. • Generic Form: for (initial ; condition ; increment ) statement • initial, condition, and increment are C expressions. • For loops are executed as follows: 1. initial is evaluated. Usually an assignment statement. 2. condition is evaluated. Usually a relational expression. 3. If condition is false (i.e. 0), fall out of the loop (go to step 6.) 4. If condition is true (i.e. nonzero), execute statement 5. Execute increment and go back to step 2. 6. Next statement
  • 38. The for Statement //For statement examples #include <stdio.h> int main () { int count; /* 1. simple counted for loop */ printf("Output with increment in loopn"); for (count =1; count <=10; count++) printf ("%dn", count); /* 2. counting backwards */ printf("Output with decrement in loopn"); for (count = 56; count >48; count--) printf("%dn", count); /* 3. for loop counting by 5's */ printf("Output with increment of 5n"); for (count=0; count<32; count += 5) printf("%dn", count); getchar(); return 0; }
  • 39. The for Statement #include <stdio.h> int main () { int count; /* initialization outside of loop */ count = 1; for ( ; count < 7; count++) printf("%d ", count); printf(“n"); /* increment outside of loop */ count = 1; for ( ; count < 7; ) { printf("%d ", count); count++; } getchar(); return 0; }
  • 40. #include <stdio.h> int main () { int count; int x, y; /* compound statements increment */ for (x=0, y=100; x<y; x++, y--) { printf("%d, %dn", x,y); } getchar(); return 0; } Dr. Yousaf, PIEAS The for Statement
  • 41. The for Repetition Structure • Format when using for loops for ( initialization; loopContinuationTest; increment ) statement • Example: for (i = 1; i <= 10; i++) printf("i = %dn", i); –Prints the integers from one to ten No semicolon (;) after last expression Dr. Yousaf, PIEAS
  • 42. The for Structure: Observations • Arithmetic expressions Initialization, loop-continuation, and increment can contain arithmetic expressions. x = 2; y = 10; for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Dr. Yousaf, PIEAS
  • 43. The for Structure: Observations • Notes about the for structure: – "Increment" may be negative (decrement) – If the loop continuation condition is initially false • The body of the for structure is not performed • Control proceeds with the next statement after the for structure – Control variable // for (i = 1; i <= 10; i++) • Often printed or used inside for body, but not necessary Dr. Yousaf, PIEAS
  • 44. For Loop #include <stdio.h> int main() { int x; /* The loop goes if x < 10, and x increases by one in every loop*/ for ( x = 0; x < 10; x++ ) { /* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when x equals 10 the loop breaks. x is updated before the condition is checked. */ printf( "%dn", x ); } return 0; } Dr. Yousaf, PIEAS