0% found this document useful (0 votes)
3 views35 pages

EE171 Lecture 5 Programming in C

The document is a lecture on computer programming with C, covering topics such as loop control statements, jump statements, and arrays. It explains different types of loops (for, while, do-while) and their syntax, as well as jump statements like break, continue, goto, and return. Additionally, it discusses arrays, their declaration, initialization, and accessing elements, along with exercises for practical application.

Uploaded by

dtweve124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

EE171 Lecture 5 Programming in C

The document is a lecture on computer programming with C, covering topics such as loop control statements, jump statements, and arrays. It explains different types of loops (for, while, do-while) and their syntax, as well as jump statements like break, continue, goto, and return. Additionally, it discusses arrays, their declaration, initialization, and accessing elements, along with exercises for practical application.

Uploaded by

dtweve124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

EE171

Introduction to Computers &


Programming for Engineers

Lecture 5:
Computer Programing with C
1
Online Compilers

https://www.onlinegdb.com/online_c_compiler#

https://www.programiz.com/c-programming/online-compiler/

https://www.tutorialspoint.com/compile_c_online.php

2
Loop Control Statements

Jump Statements

Arrays

3
Loop Control Statements

Jump Statements

Arrays

4
Loop Control Statements
5

 Looping can be defined as repeating the same process multiple times


until a specific condition is satisfied
 Using loops, we do not need to write the same code again and again.
 Loops are useful when working with arrays
 There are 3 types of loop statements in C language:
• for
• while
• do while
Loop Control Statements
6

If the Test Condition is TRUE,


the loop is executed

After each successfully


execution, the loop starts at the
entry point again

When the Test Condition is


FALSE, the execution breaks out
of the loop
for Loop
7

 The for loop is used to execute a block of statements for a predefined iterations
until a particular condition is satisfied.
 The general form of for loop is:
for (initialization; condition; increment/decrement)
{
statements;
}
 Initialization: Starting point of the loop. Checked only once.
 Condition:The test expression, evaluated on each iteration of the loop
 Increment/decrement: The modifier expression, which changes the value of loop
control variable.This expression is executed at the end of each loop.
for Loop…
8

1. /* Print 1 to 10 numbers */ Output


1
2. # include <stdio.h>
2
3. int main( ) { 3
4. int a; 4
5. for(a = 1; a <= 10; a++) 5
6
6. {
7
7. printf(“%d \n”, a); 8
8. } 9
9. } 10
while Loop
9

 The while loop is useful when we don't know the number of iterations
in advance.
 The general form of while loop is:

while (condition is true)


{
statements;
increment/decrement;
}
while Loop…
10

1. /* Print 1 to 10 numbers */ Output


2. # include <stdio.h> 1
2
3. int main( ) {
3
4. int a = 1; 4
5. while(a <= 10) 5
6. { 6
7. printf(“%d \n”, a); 7
8. a++; 8
9. } 9
10. } 10
do-while Loop
11

 The structure of do-while loop is similar to while loop.


 The general form of do-while loop is:
do {
statements;
increment/decrement;
} while (condition is true)
 Assignment: Compare and Contrast between the while loop and do-while
loop.
do-while Loop…
12

1. /* Print 1 to 10 numbers */ Output


2. # include <stdio.h> 1
2
3. int main( ) { 3
4. int a = 1; 4
5. do { 5
6. printf(“%d \n”, a); 6
7
7. a++;
8
8. } while (a <= 10); 9
9. } 10
Loop Control Statements

Jump Statements

Arrays

13
Jump Statements
14

 Jump statement makes the control jump to another section of


the program unconditionally when encountered.
 Used to terminate the loop or switch-case instantly.
 C language provides four jump statements:
• Break
• Continue
• Goto
• Return
break Statement
15

 A break statement is used to terminate the execution


of the rest of the block where it is present and takes the
control out of the block to the next statement.
 The break statement is used inside a loop or switch
statement.
 The use of break in switch statement has been
explained in the previous slides.
break Statement…
16

1. /* Print 1 to 10 numbers */ Output


2. # include <stdio.h>
3. int main( ) { 1
4. for(int a = 1; a <= 10; a++) 2
5. { 3
6. if (a == 7)
7. break;
4
8. printf(“%d \n”, a); 5
9. } 6
10.}
continue Statement
17

 The continue statement is similar to the break statement.


 The continue statement causes the program to skip the
execution of the current iteration and jump to the next
iteration within the same loop.
 Continue skips WHILE break exits
 The continue statement can be used only in loops.
continue Statement…
18

1. /* Print 1 to 10 numbers */ Output


2. # include <stdio.h> 1
3. int main( ) { 2
4. int a; 3
5. for(a = 1; a <= 10; a++) 4
6. { 5
7. if (a == 7)
6
8. continue;
8
9. printf(“%d \n”, a);
10. } 9
11.} 10
goto Statement
19

 When a goto statement is encountered in a C program, the


control jumps directly to the label mentioned in the goto
statement
 Syntax for goto:
goto label_name;
..
..
label_name: C-statements;
goto Statement…
20

1. # include <stdio.h> Output


2. int main( ) {
3. int a = 35, b = 10;
4. if (a>b)
5. {
6. goto addition; End of the program
7. }
8. printf("This statement will be skipped! \n");
9. printf(“Even this one will be skipped! \n");
10. addition: printf("End of the program \n");
11. }
Return Statement
21

 Will be discussed when we learn Functions


Loop Control Statements

Jump Statements

Arrays

22
23
Arrays
24

 Array is a data structure that hold finite sequential


collection of homogeneous data.
 Is a collection of data items, all of the same type, accessed
using a common name.
 The elements of an array are referred by a common name and
are differentiated from one another by their position within
an array.
 Arrays consist of contiguous memory locations.
Arrays Indexing
25

 A specific element in an array is accessed by an index.


 Arrays in C are indexed starting at 0.

 Suppose A[20] is an array of 20 elements, we can reference


each element as
• A[0] – 1st element
• A[1] – 2nd element
• …
• …
• A[19] -20th element
Arrays Declaration
26

 The general form of declaring a array is:


data_type array_name[size];

int marks[5];
char letters [100];
float temperature[31];
Arrays Initialization
27

(1) Static Initialization:


int marks[5] = {90, 86, 89, 15, 91};
char letters[10] = {‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’};
float rate[2][2] = {20.5, 15.7, 12.34, 24.12};
 Size of array is optional when using Static Declaration:
int marks[] = {90, 86, 89, 15, 91};
 Array can be initialized to zero:

int marks[] = { }; or int marks[] = { 0 };


Arrays Initialization…
28

(1) Dynamic Initialization:


int marks[5];

marks[0] = 90;
marks[1] = 86;
………..
marks[4] = 91;
Arrays Dimensions
29

 Single-Dimensional Array
int marks[5] = {3, 2, 1, 5, 4};

 Multi-Dimensional Array
int manymarks[2][3] = {10, 20, 30, 40, 60, 70};
int manymarks[2][3] = {{10, 20, 30}, {40, 60, 70}};
int manymarks[][3] = {{10, 20, 30}, {40, 60, 70}};
Accessing Array Elements
30

 We can access elements of an array by indices/position


 Array indexing starts at zero
 Suppose int marks[5] = {10, 20, 30, 40, 50};
 Then mark[2] = 70; changes 3rd element of marks
array
 Also int temp = mark[1]; Stores 2nd element to
another variable temp
Exercise 1
31

 Write a C program which stores five numbers


in an array. The program should calculate the
sum of those numbers and display the result.
# include <stdio.h>
int main( ) {
int number[5] = {10, 20, 30, 40, 50};
int sum = 0;
for( int i = 0; i < 5; i++)
{
sum = sum + number[i];
}
printf("The sum of five numbers is %d “, sum);
}
32
Exercise 2
33

 Write a C program which stores five numbers


in an array. The program should calculate the
average of those numbers and display the
result.
# include <stdio.h>
int main( ) {
int number[5] = {10, 20, 30, 40, 50};
int sum = 0;
for( int i = 0; i < 5; i++)
{
sum = sum + number[i];
}
float average = sum / 5;
printf(“The average of five numbers is %f”, average);
34
}
35

You might also like