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

Unit3 Control Statements

The document provides an overview of control statements in C, including branching and looping. It details various types of branching statements such as if, if-else, and switch-case, along with examples. Additionally, it covers looping constructs like while, do-while, and for loops, explaining their syntax and functionality.

Uploaded by

Prathmesh Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit3 Control Statements

The document provides an overview of control statements in C, including branching and looping. It details various types of branching statements such as if, if-else, and switch-case, along with examples. Additionally, it covers looping constructs like while, do-while, and for loops, explaining their syntax and functionality.

Uploaded by

Prathmesh Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

C Control Statements

Flow control in C
C provides two styles of flow control:
Branching
Looping

Branching is deciding what actions to take


looping is deciding how many times to take a certain action.
Branching: If statement

If statements in C is used to control the program flow based on some condition,


it's used to execute some statement code block if the expression is evaluated to
true. Otherwise, it will get skipped.
The if statement in C can be used in various forms depending on the situation and
complexity.
There are four different types of if statement in C.
1. Simple if Statement
2. if-else Statement
3. Nested if-else Statement
4. If-else-if Ladder
Simple if Statement

The basic format of if statement is:


if(test_expression)
{
statement 1;
statement 2;
...
}
Sample Program with output

#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

printf("You are eligible to vote...");


}
else
{
printf("Sorry ... you can't vote");
}
If else-if ladder Statement
It is used in the scenario where there are multiple cases to be performed for different conditions.

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

constant2: are executed until break is encountered.

If there is no match, the default statements are executed.

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.

A loop consists of two parts, a body of a loop and a control statement.

The control statement is a combination of some conditions that direct the body of

the loop to execute until the specified condition becomes false.

The purpose of the loop is to repeat the same code a number of times.
While loop

It is an entry-controlled loop. In while loop, a


condition is evaluated before processing a body of
the loop.
If a condition is true then and only then the body of
a loop is executed.
After the body of a loop is executed then control
again goes back at the beginning, and the condition
is checked if it is true, the same process is executed
until the condition becomes false.
Once the condition becomes false, the
After exiting the loop, the control goes to the
statements which are immediately after the loop
control goes out of the loop.
while(condition)
Sample Code {
//statements;
}
#include <stdio.h>
int main ()
{ value of a: 10
/* local variable definition */ value of a: 11
int a = 10; value of a: 12
/* while loop execution */ value of a: 13
while( a < 20 ) value of a: 14
{ value of a: 15
printf("value of a: %d\n", a); value of a: 16
a++; value of a: 17
} value of a: 18
return 0; value of a: 19
}
do while loop
• A do while loop is similar to while loop with one exception that it executes the
statements inside the body of do-while before checking the condition
• The do-while loop is mainly used in the case where we need to execute the loop at least
once

Syntax of do-while loop


do
{
//Statements
}while(condition test);
Sample Code

Example of do while loop


#include <stdio.h>
int main() Output:
{ Value of variable j is: 0
int j=0;
do
Value of variable j is: 1
{ Value of variable j is: 2
printf("Value of variable j is: %d\n", j); Value of variable j is: 3
j++;
}while (j<=3);
return 0;
}
For loop
A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.

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;

How break statement works?


Write a program to print table of particular
number

You might also like