Unit3 Control Statements
Unit3 Control Statements
Flow control in C
C provides two styles of flow control:
Branching
Looping
#include<stdio.h>
int main()
//OUTPUT
{
int number=0; Enter a number:4
printf("Enter a number:"); 4 is even number
scanf("%d", &number);
Enter a number:5
if(number%2==0)
{
printf("%d is even number", number);
}
return 0;
}
Program to find the largest number of the three.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c) //Output
{
printf("%d is largest", a);
Enter three numbers?
} 12 23 34
if(b>a && b > c) 34 is largest
{
printf("%d is largest", b);
}
if(c>a && c>b)
{
printf("%d is largest", c);
If-else Statement
The if-else statement is used to perform two operations for a single condition.
The if-else statement is an extension to the if statement using which, we can perform two
different operations, i.e., one is for the correctness of that condition, and the other is for
the incorrectness of the condition.
Here, we must notice that if and else block cannot be executed simultaneously.
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Sample Program with output
#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:"); //OUTPUT
scanf("%d", &number);
if(number%2==0) enter a number:4
{ 4 is even number
printf("%d is even number", number); enter a number:5
} 5 is odd number
else
{
printf("%d is odd number", number);
}
return 0;
}
Program to check whether a person is eligible to vote or not.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?"); //OUTPUT
scanf("%d", &age); Enter your age?18
You are eligible to vote...
if(age>=18)
Enter your age?13
{
Sorry ... you can't vote
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all
//the conditions are false
}
Program to calculate the grade of the student
according to the specified marks
#include <stdio.h>
else if (marks > 40 && marks <= 60)
int main() {
{ printf("You scored grade B ...");
}
int marks; else if (marks > 30 && marks <= 40)
printf("Enter your marks?"); {
printf("You scored grade C ...");
scanf("%d", &marks); }
if(marks > 85 && marks <= 100) else
{
{ printf("Sorry you are fail ...");
printf("Congrats ! you scored grade A ..."); }
}
}
//OUTPUT
else if (marks > 60 && marks <= 85) Enter your marks?10
{ Sorry you are fail ...
Enter your marks?40
printf("You scored grade B + ...");
You scored grade C ...
} Enter your marks?90
Congrats ! you scored grade A ...
switch case statement
The switch case statement is used when we have multiple options
and we need to perform a different task for each option.
switch (expression)
{
case constant1:
// statements
break;
default:
// default statements
case constant2:
// statements
break;
...
}
How does the switch statement work?
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are
executed.
For example, if the value of the expression is equal to constant2, statements after case
If we do not use break, all statements after the matching label are executed.
By the way, the default clause inside the switch statement is optional.
Rules of Using Switch Case
1. Case Label must be unique
2. Case Labels must ends with Colon
3. Case labels must have constants / constant expression
4. Case label must be of integral Type ( Integer,Character)
5. Case label should not be ‘floating point number ‘
6. Switch case should have at most one default label
7. Default label is Optional
8. Default can be placed anywhere in the switch
9. Break Statement takes control out of the switch
10. Relational Operators are not allowed in Switch Statement.
11. Const Variable is allowed in switch Case Statement.
12. Empty Switch case is allowed.
Sample Code
Loop Statements
A Loop executes the sequence of statements many times until the stated condition
becomes false.
The control statement is a combination of some conditions that direct the body of
The purpose of the loop is to repeat the same code a number of times.
While loop
Syntax
for ( initialization statement ; test expression ; update statement)
{
//statements;
}
How for loop works?
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
• However, if the test expression is evaluated to true, statements inside the body of for loop
are executed, and the update expression is executed.
• Again the test expression is evaluated.
• This process goes on until the test expression is false. When the test expression is false, the
Sample Code
#include <stdio.h>
Output:
int main() Hello World
{ Hello World
int i; Hello World
Hello World
for (i = 1; i <= 10; i++) Hello World
{ Hello World
printf( "Hello World\n"); Hello World
} Hello World
Hello World
return 0; Hello World
}
Imp points related to for loop
Initialization, test expression and update statement of for loop can be replaced by
any valid expression.
for (num=10; num<20; )
Eg. {
for(i=10;i;i- -) //Statements
num++;
{ }
printf("%d", i);
if(i ==0) int num=10;
for (;num<20;)
break;
{
//Statements
} num++;
}
int num=10;
for(num=20; num>10; num--)
for (;num<20;num++)
Example of for loop with multiple test conditions
#include <stdio.h>
int main() //OUTPUT
{ 1, 1
int i,j; 2, 2
for (i=1,j=1 ; i<3 || j<5; i++, j++)
{
3, 3
printf("%d, %d\n",i ,j); 4, 4
}
return 0;
}
Note: You cannot use multiple test conditions separated by comma, you must use logical
operator such as && or || to join conditions.
Nested For Loop in C
Nesting of loop is also possible.
Output:
#include <stdio.h> 0, 0
int main()
{ 0, 1
int i , j; 0, 2
for (i=0; i<2; i++)
{
0, 3
for (j=0; j<4; j++) 1, 0
{ 1, 1
printf("%d, %d\n",i ,j);
} 1, 2
} 1, 3
return 0;
}
continue statement
The continue statement is used inside loops. When a continue statement is
encountered inside a loop, control jumps to the beginning of the loop for next
iteration, skipping the execution of statements inside the body of loop for the
current iteration.
Syntax:
continue;
Sample code
#include <stdio.h>
int main()
{
int j;
for (j=0; j<=8; j++)
{
if (j==4)
{
/* The continue statement is encountered //Output:
when * the value of j is equal to 4. */ 01235678
continue;
}
/* This print statement would not execute for the * loop
iteration where j ==4 because in that case * this statement would be skipped.
*/
printf("%d ", j);
}
return 0;
break statement
The break statement ends the loop immediately when it is encountered.
Its syntax is:
break;