You are on page 1of 20

BIT 2104

Introduction to Programming and


Algorithms

LECTURE 9
Decision Making and Looping

July 2, 2021 Introduction to C Programming 1


Content
• Describe and use appropriately the C decision
making and Looping control structures
•  The while statement
•  The do statement
•  The for statement
•  Nesting for statements
• Describe and use appropriately syntax to jumping
in and jumping out of a Loop.
•  Break statement
July 2, 2021 Introduction to C Programming 2
Introduction
• The looping capabilities in C enables us to develop concise
programs containing repetitive processes without the use
of GOTO statements.
• In looping the sequence of statements are executed until
some conditions for termination of the loop is satisfied.
• A program loop consists of two segments, one known as
the body of the loop and the other as the control
statement.
• The control statement test certain conditions and then
directs the repeated execution of the statement contained
in the body of loop.

July 2, 2021 Introduction to C Programming 3


Introduction
• Classification of Loop Control Structures;
• Depending on the position of the control statement in the
loop a control structure may be classified as either a
Entry-controlled loop(Pre-test) or
• Exit-controlled loop (Post-test),
• In the entry controlled loop, the control conditions are
tested before the start of the loop execution,
• while in the Exit- controlled loop the control conditions
are tested before the end of the loop execution, thus the
body of loop is executed unconditionally for the first time

July 2, 2021 Introduction to C Programming 4


Introduction
• Entry Controlled Loop • Exit Controlled Loop flow
flow chart chart

July 2, 2021 Introduction to C Programming 5


Introduction
• The C language provides for three constructs for
performing loop operations;
•  The while statement
•  The do statement
•  The for statement
• Based on the nature of control variable and the kind of
value assigned to it for testing the control expression, the
loop may be classified into two general categories;
• Counter-controlled loops
• Sentinel-controlled loops

July 2, 2021 Introduction to C Programming 6


Introduction
• Counter-controlled loops:
• when in advance the number of time the loop is to be
executed is known.
• Control variable called Counter is used, which is initialized,
tested and updated properly for the desired loop
operations.
• The number of times the loop is to be executed may be a
constant or a variable that is assigned a value.
• It is also called definite repetition loop.

July 2, 2021 Introduction to C Programming 7


Introduction
• Sentinel-controlled loops:
• It is a special value is used to change the loop control
expression from true to false.
• The number of repetitions are not known before the loop
begins executing and is thus called indefinite repetition
loop.

July 2, 2021 Introduction to C Programming 8


while Loop

• The simplest of all the The while is an entry-controlled loop
statement.
looping structures in • The test-condition is evaluated and if true the
C. body of the loop is executed.
• After the execution the test-condition is
• The while statement evaluated again and if true the body of the
take the following loop is executed again.
form; • This process is repeated until the test-
condition finally becomes false and the
• while (test condition) control is transferred outside the loop where
the program continues with the statements
• { immediately after the body of the loop.
• body of loop
• } •

 
July 2, 2021 Introduction to C Programming 9
while Loop
• Example 1 • while (n <= 10)
• • {
#include<stdio.h>
• sum = sum + n*n;
• #include<conio.h>
• n = n+ 1; 
• main()
• }
• { sum= 0;
• printf (“Sum = %d\n”,
• n = 1; sum);
• getch();
• }

July 2, 2021 Introduction to C Programming 10


while Loop
• Example 2 •  y =1 ;
• •  count = 1
#include<stdio.h>
•  while (count <= n)
•  #include<conio.h>
•  {
•  main()
•  y = y*x;
•  {
•  count++;
•  int count, n; •  }
•  float x, y; •  printf(“\nx = %f; n = %d; x
•  printf(“Enter the values to power n = %f\n”, x,n,y); 
for x and b”); • getch();
•  scanf(“%f %d”, &x, &n); • }
July 2, 2021 Introduction to C Programming 11
do-while Statement
• Use when the body of • On reaching the do statement, the
the loop needs to be program proceeds to evaluate the
body of the loop first, and at the end
executed first before of the loop the test-condition in the
evaluating the test- while statement is evaluated.
condition. • If the test-condition is true the
• Format programs evaluates the body of loop
and these process is repeated until
• do
the test-condition is false.
• { • If the test- condition is false the loop
• body of the loop is terminated and the control goes to
the statement that appears
• } immediately after the while
• while (test-condition); statement
July 2, 2021 Introduction to C Programming 12
do-while Statement
• printf(" MULTIPLICATION TABLE \n");
• Program to print the • printf(" -------------------------------------- \n");
multiplication table 1x1 to • do /* Outer loop begins */

12x12 using Nested do .. {
• column = 1;
while loops • do /* inner loop begins */
• #include<stdio.h> •  {
•  y = row * column;
• #include<conio.h> • printf("%4d", y);
• column = column + 1;
• #define COLMAX 10 • } 
• #define ROWMAX 12 main() • while(column <= COLMAX); /*Inner loop Ends*/
• printf("\n");
• {  • row = row + 1;
• }
• int row, column, y; • while (row <= ROWMAX); /* Outer loop Ends*/ printf ("
• row = 1; •
--------------------------\n");
getch();
• }

July 2, 2021 Introduction to C Programming 13


do-while Statement
• The printf in the inner loop does not contain any new (\n) , thus
allowing the printing of all row values in one line.
• The empty printf in the outer loop initiates a new line to print the
next row.

July 2, 2021 Introduction to C Programming 14


for Statement
• The for loop is an entry-controlled loop that provides a more
concise loop control structure.
• The general form for the for loop is
• for (initialization ; test-condition; increment) 
• {
• body of the loop
• }

July 2, 2021 Introduction to C Programming 15


for Statement
• #include<stdio.h> • In the above example, the
• #include<conio.h> for loop is executed 10
• main() times and prints the digits

0 to 9.
{
int x;  • Note: the three sections in
• for (x =0; x<=9; x= x+1) the for ( ) statement
must be separated by
• {
semicolons(;).
• printf("%d", x);
• There is no (;) after the
• }
increment section (x = x+1)
printf("\n");
• getch();
• }
July 2, 2021 Introduction to C Programming 16
for Statement
• #include<stdio.h> • In the above example, the
• #include<conio.h> for loop is executed 10
• main() times and prints the digits
• {
0 to 9.
• Note: the three sections in
• int x;

the for ( ) statement
for (x =9; x>=0; x= x-1)
must be separated by
• { semicolons(;).
• printf("%d", x); • There is no (;) after the
• } increment section (x = x+1)
• printf("\n");
• getch();
• }
July 2, 2021 Introduction to C Programming 17
Nested for Statement
• Nesting of the for loop ie one for statement within another for
statement is allowed in C. Two loops can be nested as follows;

July 2, 2021 Introduction to C Programming 18


for Statement
• #include<stdio.h> • for (column = 1; column <= COLMAX; ++column)
• #include<conio.h> • {
• #define COLMAX 10 • y = row * column;
• #define ROWMAX 12 • printf("%4d", y);
• main() • }
• { • printf("\n");
• int row, column, y;
• }
• row = 1;
• printf (" ------------\n");
• printf(" MULTIPLICATION TABLE \n");
• getch();
• printf(" --------------------- \n");
• for (row=1; row<=ROWMAX; ++row) • }
• {
July 2, 2021 Introduction to C Programming 19
CAT2
Write a c program that OUTPUT
will prompt user to Name: Naomi Fatuma
enter exam details and Registration Number: BE213/007/2016
display the following Course: BBIT
output 
Unit Marks:
1.System Analysis and Design = 67
2.Development Studies and Ethics = 78
3.Insurance and Risk Management = 65
4.Data Communication and Network = 89
5.Introduction to Business = 78
6.Principles of Management = 98
7.Introduction to Programming = 90
TOTAL MARKS = 560
AVERAGE MARKS = 67
July 2, 2021 Introduction to C Programming 20
AVERAGE GRADE = B

You might also like