0% found this document useful (0 votes)
9 views25 pages

5 Control Structures

The document outlines control structures and statements in the C programming language, focusing on decision-making, loop control, and unconditional control statements. It details various types of control statements such as if, if-else, nested if, and switch statements, along with their syntax and examples. Additionally, it includes programming exercises for practical application of these concepts.

Uploaded by

shadowjheller17
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)
9 views25 pages

5 Control Structures

The document outlines control structures and statements in the C programming language, focusing on decision-making, loop control, and unconditional control statements. It details various types of control statements such as if, if-else, nested if, and switch statements, along with their syntax and examples. Additionally, it includes programming exercises for practical application of these concepts.

Uploaded by

shadowjheller17
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

CONTROL STRUCTURES

/ STATEMENTS IN C
LANGUAGE

LOUDIE B. SULIVA, MScIT


Subject Instructor
CONTROL STRUCTURES /
STATEMENTS IN C
LANGUAGE

OUTLINE
• To define control statements
• To be able to define three types of
control statements
• Discuss decision-making statements,
loop control and unconditional
control statements
CONTROL STRUCTURES / STATEMENTS IN C LANGUAGE

Introduction
Control Structures are just a way to
specify the flow of control in programs.
Any algorithm or program can be clearer
and more understood using self-contained
modules called logic or control structures.
It basically analyzes and chooses in which
direction a program flows based on certain
parameters or conditions.
PRESENTATION TITLE

This involves a kind of decision-makingto see whether a particular


condition has occurred or not and direct the computer to execute
certain statements accordingly.
Based on application, it is necessary / essential

1. To alter the flow of a program


2. Test the logical conditions
3. Control the flow of execution as per the selection these conditions
can be placed in the program using decision-making statements.

9/3/20XX 4
5

Decision-
makingstatements
“C”
SUPPORTS
MAINLY Loop control statements
THREE TYPES
OF CONTROL
STATEMENTS. Unconditional control
statements
6

Simple if Statement

if – else Statement
Decision-
making Nested if-else statement
statements else – if Ladder

switch statement
Control Statements Description.

If statements If statement is used to execute the code if condition is true

If else statement is used to execute a code when condition


If else statements
is true otherwise it execute another code in else section

We can use if statement inside another if statement, if we


Nested if statements want to execute a code when multiple conditions become
true

If else ladder is used, If we want to check for multiple


If else if ladder
conditions one after another

Switch statement allows us to perform equality test of


Switch statement
value of a variable against a list of possible values.

7
Decision-making
statements
if statement is the most simple decision-making
statement. It is used to decide whether a certain
statement or block of statements will be executed or
not i.e if a certain condition is true then a block of
statements is executed otherwise not.

8
Decision-making statements

// C program to illustrate If statement


#include <stdio.h>
main()
{
int i = 10;

if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
9
Decision-making
statements
if-else
The formal syntax of if-else is:
The if statement alone tells us that
if a condition is true it will execute a
if (condition) block of statements and if the
statement1; condition is false it won’t. But what
else if we want to do something else if
the condition is false. Here comes
statement2; the C else statement. We can use
the else statement with the if
statement to execute a block of
code when the condition is false.

10
Decision-making statements
if-else
The if statement alone tells us that if a
condition is true it will execute a block of
statements and if the condition is false it
won’t. But what if we want to do
something else if the condition is false.
Here comes the C else statement. We can
use the else statement with the if
statement to execute a block of code
when the condition is false.

11
Decision-making statements
// C program to illustrate If statement
#include <stdio.h>
main()
{
int i = 20;
if (i < 15) {

printf("i is smaller than 15");


}
else {

printf("i is greater than 15");


}
return 0;
}
12
13

Syntax:
NESTED-IF if (condition1)
A nested if in C is an if
statement that is the target {
of another if statement.
Nested if statements mean // Executes when condition1 is true
an if statement inside
another if statement. Yes, if (condition2)
both C and C++ allow us to
nested if statements within {
if statements, i.e, we can
place an if statement // Executes when condition2 is true
inside another if
statement. }
}
14

NESTED-IF
A nested if in C is an if
statement that is the target
of another if statement.
Nested if statements mean
an if statement inside
another if statement. Yes,
both C and C++ allow us to
nested if statements within
if statements, i.e, we can
place an if statement
inside another if
statement.
Output:
#include <stdio.h>
main()
{
int i = 10;
i is smaller than 15
if (i == 10) { i is smaller than 12 too
// First if statement
if (i < 15)
printf("i is smaller than 15\n");

// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}

return 0;
}
15
Example
Write a program that determines of the input age is qualified
to vote or not. The qualifying age is 18 years old and above.

Algorithm:
Step 1: Enter Age
Step 2: if (age>18)
Step 3: printf(“\n Qualified to vote);
Step 4: else
Step 5: printf(“\n Too young!”);

16
Solution 1
#include<stdio.h>
main()
{
int age;
printf(“\n Enter Age: “);
scanf(“%d”, &age);
if (age>=18)
printf(“\n Qualified to Vote”);
else
printf(“\n Too young !”);
getch();
}
17
Solution 2
#include<stdio.h>
main()
{
int age;
printf(“\n Enter Age: “);
scanf(“%d”, &age);
if ((age>=1) && (age<=100))
if(age>=18)
printf(“\n Qualified to Vote”);
else
printf(“\n Too young !”);
else
printf(“Out of Range”);
getch();
} 18
Solution (Final)
#include<stdio.h>
main()
{
int age;
printf(“\n Enter Age: “);
scanf(“%d”, &age);
if ((age>=18) && (age<=100))
printf(“\n Qualified to Vote”);
else if ((age>=1) && (>=17))
printf(“\n Too young !”);
else
printf(“Out of Range”);
getch();
19
}
Try this
1. Write a program that determines if the input number is
POSITIVE or NEGATIVE. Consider 0 as positive
(considering that it contains no negative sign).

2. Write a program that determines if the input number is


ODD or EVEN number.

20
Activity 4
1. Write a program to assist a teacher in calculating student
grades at the end of the semester. It accepts a numerical
grade as input, then it will display the character grade as
output, based on the given scale:

Range Grade
90 and above A
80 – 89 B
70 – 79 C
60 – 69 D
Below 60 F

22
Activity 4
2. Write a C program to accept
a coordinate point in an XY
coordinate system and
determine in which quadrant
the coordinate point lies.

23
Thank You
LOUDIE B. SULIVA, MSIT
Subject Instructor
9/3/20XX 25

PRESENTATION TITLE

THANK YOU
Presenter name
Email address
Website

You might also like