C Programming Lecture IV
Instructor zgr ZEYDAN
break and continue Statements
break statement is used to terminate any type
of loop such as while loop, do while loop and
for loop. C break statement terminates the
loop body immediately and passes control to
the next statement after the loop.
continue statement is used to skip over the
rest of the current iteration. After continue
statement, the control returns to the top of the
loop.
break and continue Statements
continue;
break;
Example:
Example:
for(i=0;i<10;i++)
for(i=0;i<10;i++)
{
{
if(i==5)
if(i==5) break;
continue;
printf("%d\n",i);
printf("%d\n",i);
}
}
The numbers 0
The numbers 0
through 9 are printed
through 4 are printed.
except for 5.
While loop
A loop statement allows you to execute a block of
code repeatedly.
The C while loop is used when you want to
execute a block of code repeatedly with checked
condition before making an iteration.
while ( expression ) statement...
statement is executed repeatedly as long as
expression is true. The test on expression takes
place before each execution of statement.
While Loop
While loop
Example:
j = 1;
while (j <= 6 )
{
printf(%d , j);
++j;
}
Output:
Infinite loops
while ( 10 )
{
statement;
while ( -12.6 )
{
statement;
}
}
Creates infinite loops. Expression part is always TRUE (1)
while ( 0 )
{
statement;
Never executed. Expression part is FALSE (0)
Example 1
Write a C program that reads in n (n is
given by user) numbers and displays their
maximum and minimum values.
Use while loop.
Example 1
#include <stdio.h>
#include <conio.h>
main()
{
int x, c=1, n, min, max; /* x:number, n: total numbers,
c:counter */
printf("How many numbers do you have:");
scanf("%d", &n);
printf("Enter the number:");
scanf("%d", &x);
max=x;
min=x;
c+=1;
Example 1
while (c<=n)
{
printf("Enter the number:");
scanf("%d", &x);
if (x>max) max=x;
if (x<min) min=x;
c+=1;
}
printf("Maximum is %d and minimum is %d",max,min);
getch();
return 0;
}
Add Watch (CTRL + F5)
Statement step over
Statement step over: runs your code
line by line
Step Over
Example 2
Repeat Example 1 without asking n (how many
numbers)
Use sentinel controlled loop instead of using
counter control loop.
For instance: use -999 as sentinel.
Example 2
#include <stdio.h>
#include <conio.h>
main()
{
int x, min, max;
printf("Enter the number (-999 to end): ");
scanf("%d", &x);
max=x;
min=x;
Example 2
while (x!=-999)
{
printf("Enter the number (-999 to end): ");
scanf("%d", &x);
if (x>max && x!=-999) max=x;
if (x<min && x!=-999) min=x;
}
printf("Maximum is %d and minimum is %d",max,min);
getch();
return 0;
}
Do While Loop
The C do while loop statement consists of a block
of code and a Boolean condition.
First the code block is executed, and then the
condition is evaluated.
If the condition is true, the code block is executed
again until the condition becomes false.
do statement... while( expression );
statement is executed repeatedly as long as
expression is true. The test on expression takes
place after each execution of statement.
Do While Loop
Do While Loop
Syntax:
do {
statement
} while ( condition );
Example:
j = 1;
do
{
printf("%d ", j);
++j;
}while (j <= 6 );
Output:
Common Programming Errors for Loops
Infinite loops are caused when the loopcontinuation condition in a while, for or
do...while statement never becomes false.
To prevent this, make sure there is not a
semicolon immediately after the header of a
while or for statement.
In a counter-controlled loop, make sure the
control variable is incremented (or
decremented) in the loop.
In a sentinel-controlled loop, make sure the
sentinel value is eventually input.
Example 3
Remember factorial example from previous
week.
Rewrite C code for factorial program by
using do-while loop instead of for loop.
Do not forget to check the number must be
positive.
Factorial example (for loop)
#include <stdio.h> #include <conio.h>
#include <stdlib.h>
main()
{
int n,f=1,c;
/*n: number, f: factorial, c:counter */
printf("Enter positive number to calculate its factorial: ");
scanf("%d",&n);
if (n<0) abort();
else if (n==0);
else for (c=1;c<=n;c++) f*=c;
printf("Factorial of %d is %d",n,f);
getch();
return(0);
}
Example 3
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
int n,f=1,c;
/*n: number, f: factorial, c:counter */
printf("Enter positive number to calculate its factorial: ");
scanf("%d",&n);
if (n<0) abort();
else if (n==0) printf("0! is 1");
Example 3
else
{ c=1;
do { f*=c; c+=1; }
while (c<=n);
printf("Factorial of %d is %d",n,f);
}
getch();
return(0);
}
Generating Random Numbers
#include <stdlib.h> /* required for randomize() and
random() */
randomize();
/* to reset random numbers */
rand() % value;
For example;
y = rand() % 100;
y will be in the range of 0 though 99.
if you want to generate numbers between the range (max
and min):
r = (rand() % (max+1-min))+min;
Example 4
Write a number guess game.
By using random number generator function generate a
secret number between 0 and 100.
Then computer will ask user to guess the number.
Use if control structure to display whether,
The written number is smaller than secret number
The written number is greater than secret number
To repeat asking guess use do-while loop.
Count the total number of guesses.
When then secret number is guessed correctly, program
displays a message Congratulations, you have guessed in
n times.
Example 4
/* Number Guess Game written by zgr ZEYDAN */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
int r,g,c; /*r: random number created by computer
g: number written by user
c: counter */
printf(" ** Number Guess Game by zgr ZEYDAN ** \n");
randomize();
r=rand()%101;
c=0;
Example 4
do {
printf("Write a number between 0 and 100\n");
scanf("%d",&g);
c=c+1;
if (r>g) printf("My number is bigger than your guess.\n");
else if (r<g) printf("My number is smaller than your
guess.\n");
else printf("Congratulations, you have guessed correctly in
%d guess.",c);
}
while (r!=g);
getch();
return 0;
}
Homework
Compare structures of loops:
For loop
While loop
Do-while loop