0% found this document useful (0 votes)
23 views

Lecture 10 Oct 22 2019

The document discusses various C programming concepts including infinite loops, break and continue statements, and the exit() function. It provides code examples to demonstrate how these concepts work, such as using while(1) or for(;;) to create infinite loops and using break or continue to exit or continue loops early. It also shows how to print patterns of stars in multiple rows using for loops. The document is a set of lecture slides on computing fundamentals and C programming basics provided by Dr. Muhammad Yousaf Hamza.

Uploaded by

john kevin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 10 Oct 22 2019

The document discusses various C programming concepts including infinite loops, break and continue statements, and the exit() function. It provides code examples to demonstrate how these concepts work, such as using while(1) or for(;;) to create infinite loops and using break or continue to exit or continue loops early. It also shows how to print patterns of stars in multiple rows using for loops. The document is a set of lecture slides on computing fundamentals and C programming basics provided by Dr. Muhammad Yousaf Hamza.

Uploaded by

john kevin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Infinite Loops

Dr. Yousaf, PIEAS


Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
while (1) /* any non zero number can be provided
here. It is an infinite loop */
{
printf("i = %d\n", i);
i++;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Infinite Loop
// Infinite Loops, Page 226
#include<stdio.h>
int main()
{
int i = 7;
for (; ;)
{
printf("i = %d\n", i);
i++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
break, continue Statements

Dr. Yousaf, PIEAS


Break Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Break Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5) // what’s about if we write if ( i > 3)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Break Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5) // what’s about if we write if ( i < 3)
break;
printf("i = %d\n", i);
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
// Infinite loop using while and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
while (1)
{
scanf("%d",&num);
if (num>= 10 && num <=20)
break;
else
printf("Invalid. Please enter an integer number from 10
to 20\n");
}
// Rest of the code goes here
Dr. Yousaf, PIEAS
// Infinite loop using for loop and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
for( ; ;)
{
scanf("%d",&num);
if (num >= 10 && num <=20)
break;
else
printf("Please enter an integer number from 10 to 20\n");
}
// Rest of the code goes here

Dr. Yousaf, PIEAS


// Infinite loop using do while loop and break statement
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number from 10 to 20\n");
do
{
scanf("%d",&i);
if (num >= 10 && num <=20)
break;
else
printf("Invalid. Please enter an integer number from 10 to
20\n");
} while (1);
// Rest of the code goes here
Dr. Yousaf, PIEAS
The break statement
//More about break; //Page 221 for (i = 2; i<=n/2; i++)
/* if used in loop, it only breaks the { if(n%i == 0)
loop and then executes next
statements */ { prime = 0;
#include<stdio.h> break;
int main() }
{ }
int i, n, prime = 1; if (prime == 0)
printf("Enter a positive integer"); printf("The number %d is not
scanf("%d", &n); a prime number\n", n);
if (n ==2) else
printf("The number %d is a printf("The number %d is a
prime number\n", n); prime number\n", n);
else }
{ getchar(); return 0; }

Dr. Yousaf, PIEAS


The break Statement
• break
– Causes immediate exit from a while, for,
do/while or switch structure
– Program execution continues with the first
statement after the structure
– Common uses of the break statement are:
• Escape early from a loop
• Skip the remainder of a switch structure

Dr. Yousaf, PIEAS


Another Example: for and break Together
int mynum = 3;
int guess;
The notation for(;;) is
for(;;)
used to create an
{
infinite for loop.
printf("Guess my number:");
while(1) creates an
scanf("%d",&guess);
infinite while loop
if(guess==mynum)
instead.
{
printf("Good guess!\n");
break; To get out of an
} infinite loop like this
else one, we have to use
printf("Try again.\n"); the break statement.
}

Dr. Yousaf, PIEAS


continue Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5) // what’s about if we write if ( i > 3)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
continue Statement
#include<stdio.h>
int main()
{
int i;

for (i = 1; i <= 10; i++)


{
if ( i ==5) // what’s about if we write if ( i < 3)
continue;
printf("i = %d\n", i);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
Continue Statement
/*Enter 20 integer values. This program would tell how many odd
numbers you entered */
#include<stdio.h>
int main()
{
int i = 0, num, count=0;
while(i < 20)
{
printf("Enter 20 integer numbers:");
scanf("%d",&num);
i++;
if(num%2==0)
continue;
count++;
}
printf("You entered %d odd numbers", count);
getchar(); return 0; } Dr. Yousaf, PIEAS
The continue Statement
Used for skipping the remainder of the body of a
while, for or do/while structure and proceeding
with the next iteration of the loop
– while and do/while
• Loop-continuation test is evaluated
immediately after the continue statement is
executed
– for
• Increment expression is executed, then the
loop-continuation test is evaluated

Dr. Yousaf, PIEAS


exit() function

Dr. Yousaf, PIEAS


exit()
// About exit() function. Page 236
#include<stdio.h>
#include<stdlib.h> // to use exit() function.
int main()
{
int i, j, k;
for (i = 0; i<5; i++)
printf("%d\n", i);
getchar();
exit(0); // it will exit the program
j = 23;
printf("j = %d\n", j);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Continue and exit together
#include<stdio.h>
// Continue to enter integer values. This program
#include<stdlib.h>
//will terminate when you enter 5 odd numbers.
int main()
{
int num;
int count=0;
while(1)
{
printf("\nEnter an odd number :");
scanf("%d",&num);
if(num%2==0)
continue;
count++;
if(count==5)
exit(0);
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 1 star as
#include<stdio.h>

int main()
{
printf("*");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

Dr. Yousaf, PIEAS


Examples
Write a program that prints 10 stars as

#include<stdio.h>
int main()
{
int i;
for (i = 0; i<10; i++)
printf("*");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 2 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 2 Rows as
#include<stdio.h>
int main()
{
int i; // use of 2 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 3 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 3 Rows as
#include<stdio.h>
int main() getchar();
{ return 0;
int i; // use of 3 for loops }
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 4 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 4 Rows as
#include<stdio.h> printf("\n");
int main()
{ for (i = 0; i<10; i++)
int i; // use of 4 for loops {
for (i = 0; i<10; i++) printf("*");
{ }
printf("*");
} getchar();
printf("\n"); return 0;
for (i = 0; i<10; i++) }
{
printf("*");
}
printf("\n");
for (i = 0; i<10; i++)
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 5 Rows as

Dr. Yousaf, PIEAS


Write a program that prints 10 stars in 5 Rows as
#include<stdio.h> printf("\n");
int main()
{ for (i = 0; i<10; i++)
int i; // use of 5 for loops {
for (i = 0; i<10; i++) printf("*");
{ }
printf("*"); printf("\n");
}
printf("\n"); for (i = 0; i<10; i++)
for (i = 0; i<10; i++) {
{ printf("*");
printf("*"); }
} getchar();
printf("\n"); return 0;
for (i = 0; i<10; i++) }
{
printf("*");
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 50 Rows as

Use of 50 for loops???

It’s not efficient/realistic approach

We need NESTED Loops

Dr. Yousaf, PIEAS


Nested Loops

Dr. Yousaf, PIEAS


Code should be efficient
10 stars in 5 rows
(The realistic approach)

Dr. Yousaf, PIEAS


Code should be efficient
10 stars in 5 rows
(The realistic approach)
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<5; i++) // only two for loops
{
for (j = 0; j < 10; j++)
{
printf("*");
}
printf("\n");
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
Code should be efficient
10 stars in 50 rows
(The realistic approach)
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<50; i++) // only two for loops
{
for (j = 0; j < 10; j++)
{
printf("*");
}
printf("\n");
}
getchar();
return 0;
} Dr. Yousaf, PIEAS
Nested Loops --- Example
#include<stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <=3; j++)
printf(“ %d\n", i*j);
}
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Nested Loops --- Example
#include<stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <=3; j++)
printf(“ %d\n", i*j);
}
getchar();
return 0;
}

Dr. Yousaf, PIEAS


Nested Loops --- Example
#include<stdio.h> //Nesting of for and while loops
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
j= 1;
while(j <=3)
{
printf(“%d\n", i*j);
j++;
}
}
getchar(); return 0;} Dr. Yousaf, PIEAS
(Once again see this example, 5x5
Stars)
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<5; i++) // only two for loops
{
for (j = 0; j < 5; j++)
{
printf("*");
}
printf("\n");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Try to modify the previous program such that
the program prints the following pattern

0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4

Dr. Yousaf, PIEAS


Try to modify the previous program such that
the program prints the following pattern
#include<stdio.h>
int main ()
0 0 0 0 0 0
{
1 1 1 1 1 1 int i, j;
2 2 2 2 2 2 for(i=0; i<5; i++)
3 3 3 3 3 3 {
for(j=0; j<6; j++)
4 4 4 4 4 4 printf("%d\t",i);
printf("\n");
}
getchar();
return 0; }
Dr. Yousaf, PIEAS
Try to modify the previous program such that
the program prints the following pattern

0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4

Dr. Yousaf, PIEAS


Try to modify the previous program such that
the program prints the following pattern
#include<stdio.h>
0 1 2 3 4 int main ()
0 1 2 3 4 {
0 1 2 3 4 int i, j;
0 1 2 3 4 for(i=0; i<6; i++)
{
0 1 2 3 4 for(j=0; j<5; j++)
0 1 2 3 4 printf("%d\t",j);
printf("\n");
}
getchar(); return 0;
Dr. Yousaf, PIEAS
}

You might also like