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

Chapter 3 Control Structures

Here are some key points about arrays in C language: - Arrays allow grouping of multiple variables of the same data type under a single name. - In C, arrays are zero indexed meaning the first element is at index 0, second at index 1 and so on. - Arrays are declared by specifying the data type of the elements, name of the array and number of elements inside square brackets. - The number of elements of an array needs to be a positive integer constant. Variable size arrays are not allowed in standard C. - Elements of an array are accessed using the array name and index inside square brackets like array_name[index]. - Arrays in C are stored contiguously

Uploaded by

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

Chapter 3 Control Structures

Here are some key points about arrays in C language: - Arrays allow grouping of multiple variables of the same data type under a single name. - In C, arrays are zero indexed meaning the first element is at index 0, second at index 1 and so on. - Arrays are declared by specifying the data type of the elements, name of the array and number of elements inside square brackets. - The number of elements of an array needs to be a positive integer constant. Variable size arrays are not allowed in standard C. - Elements of an array are accessed using the array name and index inside square brackets like array_name[index]. - Arrays in C are stored contiguously

Uploaded by

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

Chapter 3: Introduction to Control

Structures
By Mr. Samwel Tarus
Control Structures / Statements
Directs the flow of execution in a program.
Control structures enable the programmer to achieve iteration and branching.

C language control statement are divided into three categories:

1. Decision control structures


2. Looping control structures
3. Jumping control structures
Branching: The process of taking the alternative course of action against the
other.
Looping: The process of repeating / iterating a block of statements any number
of times.
Jumping: Moving to a specified location / point in a program
Decision Control Structures

Facilitate the action of branching

Examples of structures / statements;

i. if statement
ii. if ... else statement
iii. nested if … else statement
iv. switch case statement
Looping Control structures
Facilitate the action of repetition / iteration

Examples:

i. for loop
ii. do … while loop
iii. while loop
Jumping Control Structures

Facilitate the action of jumping to some other point in the program:

Examples:

i. break statement
ii. continue statement
iii. goto statement
If control structure
Control structure that considers only the true part of the condition;
Syntax:
Example 1;
if(condition) T
Statement;
Example 2
if(condition)
{
Statement;
Statement;
Statement;
}
Cont’d …
Program example (if control statement)

a. Write a program to check if any input number is a an even number.

b. Write a program to check if any input number is less than zero.

c. Nb: also draw a flowchart of the same


if … else
Syntax:

if(condition)
{
Statement; TP
Statement;
}
else
{
statement; FP
Statement;
}
Cont’d …
Program demo (if .. else)

a. Program to check if any input number is either odd or even number


b. Program to check if any input number is divisible by 5

Nb: also draw a flowchart for the same


Nested if … else
if(cond)

statement

else if(cond)

statement

else if(cond)

statement;

else

statement;
Cont’d …
Program demo for nested if … else
Write a program that reads the name and the marks scored by a
student, and to display the corresponding grade:
Use the given grading system:

< 0 marks Invalid marks


Between 0 and 39 grade = E, Fail
Between 40 and 49 Grade = D
Between 50 and 59 Grade = C
Between 60 and 69 Grade = B
Between 70 and 100 Grade = A
> 100 marks beyond the range
statement;
Switch case control structure switch(expression)
{
case 10:
Simplified version of Statement;
nested if… else break;
case 2:
control structure Statement;
break;
X+y= value case 133:
Statement;
break;
default:
Statement;
break;
}
Statement;
Switch case control structure
T

F
T

F
T

STAT
switch case: Explanation

An expression must evaluate to a value. The value of the expression is


compared upon several cases in the switch case control structure.

The case that matches the value of an expression, is executed.

The break statement in each of the cases transfers the control of execution
outside the switch control structure.

The default option handles any mismatch in the control structure.


Program demo for switch case:
Write a menu driven program to preform the following:
Compute the perimeter of a rectangle
Compute the sum of two numbers

Ie: *131#
Main menu
1. perimeter
2. sum
Enter your choice
Looping control structures

1. for Loop control structure

2. do… while loop control structure

3. while loop control structure


for loop control structure

Used to repeat a section of code a certain number of times known in advance

Syntax:

for(initialization; test expression; inc/dec)


Why initialization?
Example:
Provide initial value.
for(i = 0; i<5; i++) Eliminate Garbage values

for(i=5; i>0; i--) Done once


T
Demo programs using for loop

1. Program to display numbers 1 to 5

2. Program to check if any input number is a prime number

3. Program to display Fibonacci series to nth term


do ….. while loop control structure

1. It is an exit control structure.

2. The condition is checked after executing


the block of code at least once.

F
do… while loop (syntax)

Statement;
do
{
Statement;
Statement;
statement;
}
while(cond);
Statement;
Demo programs using do… while loop

1. Program to display numbers 1 to 5.

2. Program to find the sum of even numbers between 1 and 10.

3. Program to check if any input number is a palindrome number


While loop control structure

It is an entry control loop F

COND
The condition is checked before
executing the block of code. T

Syntax:
while(cond)
BLOCK
{
statement;
statement;
}
Demo programs using while loop

1. Program to display numbers 1 to 5

2. Program to compute the sum of odd numbers between 1 and 5

3. Program to check if any input number is an Armstrong number


Jumping Control structures

1. break control structure / statement

2. continue control structure / statement

3. goto control structure / statement


break control structure / statement
Transfers control of execution outside the switch control structure
Transfers control of execution from a deeply nested loop.
Transfers control of execution in the forward direction.
Syntax: break;

T
Cond break

Normal F
Loop
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d", i);
Continue control structure

1. Transfers control to the beginning


of the loop or program

2. Transfers control in the backward


direction

3. By passes the remainder of the


loop.
Program to find sum of +ve integer numbers from any 5 input numbers
for(i=0;i<5;i++)
{
printf("\nenter a number\n");
scanf("%d",&no);
if(no<0)
{
printf("\nIt is negative number\n");
continue;
}
sum = sum + no;
}
printf("\nsum of positive numbers = %d\n", sum);
goto control structure
goto unconditionally transfers control to some other place in the program.
There are some better ways on how this can be done
goto control structure makes some parts of the program permanently not
executable. This renders this omitted code irrelevant in the program.
goto program
/*goto control statement*/
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
goto addition;
}
addition:
printf("%d", sum);
return 0;
Assignment / Cat

• Write short notes on Arrays in C Language

You might also like