Control Statements in C
Control Statements in C
Relational Operators in C
Relational Operators in C are used to compare
values. They are:
START
YES CONDITION NO
TRUE?
STOP
Flowchart of an “If else if” Control Statement
START
YES CONDITION
TRUE?
NO
NO
EXECUTE IF ELSE BLOCK
STOP
Series of “if, if else, if else if” statement in
the main body of C program intended to
control every probable condition.
LOOP (STATEMENTS) IN C
• Used to execute the block of code several times according to the condition given
in a loop.
• It executes the same code multiple times so it saves code.
There are three types of loops in C
• While loop
• Do-while loop
• For Loop
1. While Loop
While loop executes the code until condition is false
START
SYNTAX:
while(condition){
TRUE
//code CONDITION
EXECUTE
BLOCK(S) OF
CODE
}
FALSE
STOP
Example: Output:
#include <stdio.h> 0123456789
#include<conio.h>
void main( )
{
int i=0;
while (i<=10)
{
printf(“%d”, i);
i++;
}
}
2. Do While
Also executes the code until condition is false, at
least once. START
SYNTAX:
EXECUTE
do{ BLOCK(S) OF
CODE
//code
}while(condition);
TRUE
CONDITION
FALSE
STOP
Example: Output:
#include <stdio.h> HelloHelloHello
#include<conio.h>
void main( )
{
int i=0;
do
{
printf(“Hello”);
i++;
} while (i<=3);
}
3. Do While
Also executes the code until condition is false with the condition of
complying the three parameters: initialization, condition, and
increment/decrement.
START
SYNTAX:
For(initialization; condition; increment/decrement)
{
//code TRUE EXECUTE
BLOCK(S) OF
} CONDITION
CODE
FALSE
STOP
Example: Output:
#include <stdio.h> 20
#include<conio.h> 21
void main( ) 22
{ 23
int i; 24
For(i=20; i<25; i++)
{
printf(“%d ”,i);
}
}
SWITCH STATEMENT IN C
Allows us to execute one code block among many alternatives.
SYNTAX: How does the switch statement work
switch (expression) The “expression” is evaluated once and compared with
{ the values of each “case” label.
case constant1:
//statements • If there is a match, the corresponding statements
break; after matching label are executed. For example, if the
case constant2: value of the expression is equal to constant2,
//statements statements after case constant2 are executed until
break; break is encountered.
.
. • If there is no match, the default statements are
. executed.
default:
//default statements NOTES:
} If we do not use the break statement, all statements
after the matching label are also executed. The default
clause inside the switch statement is optional.
EXAMPLE:
#include<stdio.h>
int main() Output:
{
int guessNum=4; Sorry, You’ve made a horrible guess.