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

C Languag E: By: JCN

This document discusses different control structures in C language including sequential, selection, and iteration structures. It provides examples of each type of selection structure - simple if, if-else, if-else-if, nested if, and switch. The if-else-if structure allows checking multiple conditions and executing different code blocks. Nested if statements allow checking conditions in a nested manner. The switch statement provides a multiway decision based on matching a variable against constants.

Uploaded by

Nekumi Puni-nyan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

C Languag E: By: JCN

This document discusses different control structures in C language including sequential, selection, and iteration structures. It provides examples of each type of selection structure - simple if, if-else, if-else-if, nested if, and switch. The if-else-if structure allows checking multiple conditions and executing different code blocks. Nested if statements allow checking conditions in a nested manner. The switch statement provides a multiway decision based on matching a variable against constants.

Uploaded by

Nekumi Puni-nyan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

C LANGUAG E

By: JCN

Overview: IF Statement IF-ELSE Statement IF-ELSE-IF Statement Nested IF Statement Switch Statement

Objectives:

To Identify Different Control Structure. To Formulate a solution using the appropriate control structure To Create a program using Selection Control Structure in C Language.

Control Structure

Control the flow of execution in a program or function A combination of individual instruction into a single logical unit with one entry point and one exit point.

3 Kinds of Control Structure

Sequential Control Structure Selection Control Structure Iteration/Repetition Control Structure

Sequential Control Structure


Sequential

execution of statements Executing statements one after the other in order in which they are written.
2 types of statements:

single statement an expression followed by a semi-colon. compound statement a group of statements bracketed by { and } that are executed sequentially.

Example of Sequential:
#include<stdio.h> #include<conio.h> main() { printf(Hello World); getch(); }

Selection Control Structure


Chooses among alternative program statements Uses conditions to select a course of action. Condition an expression that is either false (0) or true (usually represented by 1) 5 types:

simple if if-else if-else-if nested if-else Switch

these are also called conditional branching constructs

Iteration/Repetition Control Structure


Uses loops repeats a group of steps in a program repetition of action is one reason we rely on computers. When there are large amount of data, it is very convenient to have control mechanism that repeatedly execute specific statements. Loop body contains the statements to be repeated in the loop. 3 types:

for while do-while

these are also called looping constructs.

N CONTROL STRUCTU RE
By: JCN

Selection Control Structure


Conditional Branching Construct

Simple IF

It is the very simple selection control structure because there must be only one condition and one statement.
CONDITION

Syntax : if (condition) statement; Example: if (grade >=75) printf(Passed);

STATEMENT

IF ELSE Statement

If Else Statement can have one condition and two statements. The two statements are being process if the condition of your program is being satisfied and not being satisfied.

Syntax :

if (condition) statement; else statement;

CONDITION

Example: if (grade >= 75) printf(Passed); else printf(Failed);

STATEMENTS

IF-ELSE-IF Statement

In this kind of control structure we can have more than one condition and many statements. A set of condition is being check whether it is INPUT being satisfied or not.

CONDITION CONDITION CONDITION

STATEMENTS

IF ELSE IF Statement
IF ELSE IF STATEMENTS A common programming constructs is the if else if statement. It look like this: Syntax :if (condition) Statement; else if (condition) statement; else if (condition) statement; else statement;

Example: if (x > 0)

printf(x is positive);
else if (x < 0) printf(x is negative); else printf(x is zero);

NESTED IF Statement

It is a conditional statement which is used when we want to check more than 1 conditions at a time in a same program. The conditions are executed from top to bottom checking each condition whether it meets the conditional criteria or not. If it found the condition is true then it executes the block of associated statements of true part else it goes to next condition to execute.

NESTED IF Statement
Syntax : if (condition) if (condition) statement; else statement; else statement;

NESTED IF Statement
INPU T
Con1 Co n1. 1 Co n1. 2 Co n2. 1 Co n2. 2 Statement1 ELSE Statement2 Statement1 ELSE Statement2 Statement1 ELSE Statement2 Statement1 ELSE Statement2

Con2

Else

Statement

SWITCH Statement

is a multiway conditional statement generalizing the if-else statement. is a built-in multiple branch decision statement. A variable is successively tested against a list of integer or character constants. When a match is found, a statement or block of statement is executed.

You might also like