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

Lecture 5 CSC1261

Uploaded by

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

Lecture 5 CSC1261

Uploaded by

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

CSC1261 Computer Programming in C

Class: Year 1 CEGE (Civil Engineering)

Lecture #5: Looping

Department of Computer and Software Engineering

16/09/2024 – 20/12/2024

Richard Mugisha
Course Plan (Provisional)
• Week 1: No Lecture due EPT
• Week 2: No Lecture due to EPT
• Week 3: Lecture #0 + Lecture #1
• Week 4: Lecture #2
• Week 5: Lab #1
• Week 6: Lecture #3 + Lecture #4
• Week 7: Lecture #5 + Lecture #6
• Week 8: Lab #2
• Week 9: Lecture #7
• Week 10: Lecture #8 + Lecture #9
• Week 11: Lab #3
• Week 12: Lecture #10 (Course Summary/Repetition)
• Week 13: Self Study
• Week 14: Self Study
Richard Mugisha
LOOPING

Chapter 5
Introduction
• We use loops when you want to
execute statement several times until a
condition is reached.
• Generally, loops consist of two parts:
❑ One or more control expressions which control the
execution of the loop.
❑ Body , which is the statement or a set of statement
which is executed over and over
3 types of loops
– For loop
– While loop
– Do while loop
For Loop
• The basic format of the for statement is,

for( start_condition; continue_ condition; re-


evaluation/incrementation)
{
program statement;
}
For Loop
Sample program
#include <stdio.h>

main()
{
int count;
for(count = 1; count <= 10; count=count+1)
printf("%d ", count);
}
Sample Program Output: 1 2 3 4 5 6 7 8 9 10
For Loop
Example
// An example of using a for loop to print out
characters
#include <stdio.h>
main()
{
char letter;
for( letter = 'A'; letter <= 'E'; letter = letter + 1 )
printf("%c", letter);
}
• Sample Program Output: A B C D E
For Loop
Sum of the first n numbers
#include <stdio.h>

main()
{
int i,n,sum=0;
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\n THE SUM OF FIRST %d NUMBERS = %d", n, sum);
}
For Loop
Multiplication Table
#include<stdio.h>
main()
{
int i, table, counter=1;

printf("ENTER THE MULTIPLICATION TABLE \n");


scanf("%d",&table);
printf("ENTER THE COUNTER VALUE\n");
scanf("%d",&counter);
printf("THE REQUIRED TABLE IS SHOWN BELOW:\n");
for(i=0;i<=counter;i++)
printf("\n%d * %d = %d",table,i,i*table);
}
For Loop
Multiplication Table from 1 to 10
#include<stdio.h>
main() Sometime any loop could be placed
{ inside the other loop
int i,j;
for(i=1; i<=10; i++)
{
for(j=1; j<=10; j++)
{
printf("%d * %d = %d\n" ,i,j,i*j);
}
printf("\n\n");
printf("Multiplication table of %d\n" ,i);
printf("\n\n");
}
}
For Loop
Inside the other loop
➢ Sometime any loop could be placed inside the other loop
#include<stdio.h>
main()
The output is:
{
int i,j;
1
for(i=1;i<=5;i++) 1 2
{ 1 2 3
for(j=1;j<=i;j++)
printf("%d\t",j);
1 2 3 4
printf("\n"); 1 2 3 4 5
}
}
For loop
Example
#include<stdio.h>
main(){
• The output is:
int i,j;
for(i=1;i<=5;i++) 5432 1
5432
{
for(j=5;j>=i; j--)
543
printf("%d\t",j);
54
printf("\n"); 5
}
}
Sometime any loop could be placed inside
the other loop
While loop
• The while provides a mechanism for repeating C
statements whilst a condition is true.
• While loop has one control expression and executes as
long as that expression is true.

• General structure (its format is):


while(expression)
{
statements;
}
While Loop
/*Sample program including While loop*/
#include<stdio.h>
main()
{

int loop = 0;
while(loop <= 10)
{
printf ("%d\n",loop);
++loop;
}
}
While loop
Multiplication table from 1 to 10
#include<stdio.h>
main(){
int i,j;i=1;
while(i<=10){
j=1;
while(j<=10){
printf("%d x %d = %d\n",i,j,i*j);
j++;
}
printf("\n\n");i++;
}
}
Do While Loop
• When developing programs, it sometimes become
desirable to have the test made at the end of the loop
rather than at the beginning.
• This looping statement is known as the do statement.

• The syntax is:


do{ statements;}
while(expression);
{
statements;
}
Do While Loop
• Execution of the do statement proceeds as follows

– Program statement is executed first.


– Next, the expression inside the parenthesis is evaluated, if the result
of evaluating is TRUE, the loop continues and the program statement
is once again executed.
– As long as evaluation of expression continues to be true, program
statement is repeatedly executed.
– When evaluation of the expression is FALSE, the loop is terminated
and the next statement in the program is executed in the normal
sequential manner.
Do While Loop
• Execution of the do statement proceeds as follows(cont’d)

– The do statement is simply a transposition of the while loop


statement with the looping conditions placed at the end of the loop
rather than at the beginning.

– N.B: Remember that, unlike the for and while loops, the do statement
guarantees that the body of the loop will be executed at least once.
Do While Loop
/* Program that displays number from 1 to 9 using Do
while */
#include<stdio.h>
main(){
int i;
i=1;
Do
{
printf("\n i is:%d",i);
i=i+1;
}
while(i<10);
}
Exercise
• Make a program to print out multiplication table of 7 using do
while.

Answer: #include<stdio.h> main(){


int i,j; i=7,j=1;
do{
printf ("\n %d x %d=%d", i, j, i*j);
j++;
}
while ( i<=10 );
}
Assignment
1. Write a program that prints a Fahrenheit to Celsius conversion
table using While Loop and For Loop.
2. C program to check whether a number is ven or odd
3. C program to check whether a character is vowel or consonant
4. C program to find the largest among three numbers entered by
user
5. C Program to find all roots of a quadratic equation. Consider even
the negative delta.
6. Using a for loop, write a program to draw this pattern below:
Pyramid program using *
#include
<stdio.h> int
main()
{
int i, space, rows, k=0;
printf("Enter number of rows:
"); scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i;
++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("*");
++k;
}
printf("\n");
}

return 0;
}
Pascal’s Triangle
#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
For(initialization; condition;
incrementation) body;
CSC1261 Computer Programming in C

Class: Year 1 CEGE (Civil Engineering)

Lecture #5: Looping

Department of Computer and Software Engineering

16/09/2024 – 20/12/2024

Richard Mugisha

You might also like