C Languag E: By: JCN
C Languag E: By: JCN
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.
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(); }
N CONTROL STRUCTU RE
By: JCN
Simple IF
It is the very simple selection control structure because there must be only one condition and one statement.
CONDITION
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 :
CONDITION
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.
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.