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

Control Statements in C

Relational operators in C compare values and include ==, !=, <, >, <=, >=. Control flow statements like if, if/else if, and switch direct program execution based on conditions. Loops like while, do/while, and for execute blocks of code repeatedly according to initialization, condition, and increment/decrement parameters.
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)
66 views25 pages

Control Statements in C

Relational operators in C compare values and include ==, !=, <, >, <=, >=. Control flow statements like if, if/else if, and switch direct program execution based on conditions. Loops like while, do/while, and for execute blocks of code repeatedly according to initialization, condition, and increment/decrement parameters.
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
You are on page 1/ 25

CONTROL STATEMENTS IN C

Relational Operators in C
Relational Operators in C are used to compare
values. They are:

1.) == (equal to)


2.) != (not equal to)
3.) < (less than)
4.) > (greater than)
5.) <= (less than or equal to)
6.) >= (greater than or equal to)
Flowchart of an If Control Statement

START

YES CONDITION NO
TRUE?

EXECUTE CODE EXECUTE CODE NEXT


IN IF BLOCK /BELOW IF BLOCK

STOP
Flowchart of an “If else if” Control Statement

START

YES CONDITION
TRUE?

NO

EXECUTE CODE IN IF BLOCK IF ELSE YES


CONDITION
TRUE?

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.

printf(“Guess a number in my mind:”);


scanf(“%d”,guessNum);
switch (guessNum)
{
case 1:
printf(“Nope! That’s not it”);
break;
case 2:
printf(“Pretty close! But it’s not…”);
break;
case 3:
printf(“Yeap! You’re quite a Psychic!”);
break; .
default:
printf(“Sorry, You made a horrible guess.”);
}
return 0;
}

You might also like