Chapter 5 – Control Structures
Chapter 5 – Control Structures
PROGRAMMING BASIC
– IN C
MODULE CODE: CS 6224
MODULE NAME: Computer Programming Basic in C
FACILITATOR: Pangahela, R. A
Email: [email protected] Phone: +255 742 926 569
COMPUTER
PROGRAMMING BASIC – IN C
5 – CONTROL STRUCTURES
Chapter 5 – Control Structures
5.1 The for loop
5.2 The while Loop
5.3 The do-while Loop
5.4 Nesting of Loops
5.5 The break Keyword
5.6 The continue Keyword
5.7 The exit Function
/* Program-5.1 */
#include <stdio.h>
int main()
{
int counter;
for(counter=1; counter <= 5; counter++)
//loop 5 times
{
printf("This is a loop\n");
}
return 0;
}
First even number is 0 and the last even number is
100. By adding 2 to an even number the next even
number can be found. Therefore the counter
should be incremented by 2 in each round.
Program-5.2 is an implementation of the above
requirement.
/* Program-5.2 */
#include <stdio.h>
int main()
{
int counter, sum;
sum = 0;
for(counter=0; counter <= 100; (counter += 2)) //increment
by 2
{
sum += counter;
}
printf("Total : %d", sum);
return 0;
}
/* Program-5.3 */
#include <stdio.h>
int main()
{
int num1, num2, sum;
sum=0;
printf("Enter first number: ");
scanf("%d", &num1); //read num1
printf("Enter second number: ");
scanf("%d", &num2); //read num2
for(; num1 <= num2; num1++)
{
sum += num1; //sum = sum+num1
}
printf("Total : %d", sum);
return 0;
}
/* Program-5.4 */
#include <stdio.h>
int main()
{
int num;
printf("Enter number of times to repeat: ");
scanf("%d", &num);
while (num != 0)
{
printf("Hello World!\n");
num--;
}
return 0;
}
Execution of Program-5.4 with 5 as the input will
display the following:
Enter number of times to repeat: 5
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
This means that the statement(s) inside the loop
will be executed at least once regardless of the
condition being evaluated. Consider the following
example:
/* Program-5.5 */
#include <stdio.h>
int main()
{
float price, total;
total = 0 ; //set initial value to 0
do //request for price
{
printf("Enter price (0 to end): ");
scanf("%f", &price); //get price
total += price;
} while (price > 0); // if valid price continue loop
printf("Total : %.2f", total);
return 0;
}
Execution of Program-5.5 with some inputs will
display the following:
Enter price (0 to end): 10
Enter price (0 to end): 12.50
Enter price (0 to end): 99.99
Enter price (0 to end): 0
Total : 122.49
5.4 Nesting of Loops
Like the conditional structures loops can also
be nested inside one another. You can nest loops
of any kind inside another to any depth you want.
However having large number of nesting will
reduce the readability of your source code.
/* Program-5.6 */
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<=5;i++) //outer loop
{
for(j=0;j<=10;j++) // inner loop
{
printf("$");
} // end of inner loop
printf("\n");
} //end of outer loop
return 0;
}
/* Program-5.7 */
#include <stdio.h>
int main()
{
int n;
for(n=10;n>0;n--)
{
printf("Hello World!\n");
if(n == 5)
{
printf("Countdown aborted!");
break;
}
}
return 0;
}
/* Program-5.8 */
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
for(i=0;i<=10000;i++) // loop 10000 times
{
printf("Hello World! %d\n",i);
if(kbhit() != 0) // if a key is pressed
{
printf("Loop terminated");
break; //terminate loop
}
}
return 0;
}
/* Program-5.9 */
#include <stdio.h>
int main()
{
int i;
for(i=-5;i<=5;i++) // loop from -5 to 5
{
if (i == 0) // if 0 skip
continue;
printf("5 divided by %d is: \t %.2f \n", i, (5.0/i));
}
return 0;
}
END
THANK YOU