Unit2 Controlstmts1
Unit2 Controlstmts1
Unit2 Controlstmts1
Syntax: -
if(expression)
{
//code to be executed
}
Example: -
//Example of if
#include<stdio.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
{
Printf (“The entered number is even”);
}
}
If else Statement
Syntax: -
if(expression)
{
//code to be executed if condition is
true
}
else
{
//code to be executed if condition is
false
}
//Example of if else statement
#include<stdio.h>
int main()
{
int n;
printf("enter a number:");
scanf("%d",&n);
if(n%2==0)
{
printf(“The entered number is even number");
}
else
{
printf("The entered number is odd number”);
}
}
Nested If else Statement
Syntax: -
The syntax for a nested if statement is as follows −
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else
{
/* Executes when the boolean expression 2 is false*/
}
}
else
{
/* Executes when the boolean expression 1 is false */
}
#include<stdio.h>
int main()
{
int a,b;
printf(“Enter the first number=”);
scanf(“%d”,&a);
printf(“Enter the second number=”);
scanf(“%d”,&b);
if(a==b)
{
printf(“Both numbers are equal”);
}
else
{
if(a>b)
{
printf(“The number a is greater than b”);
}
else
{
printf(“The number b is greater than a”);
}}}
Else If Ladder Statement
• The if-else-if ladder statement is an extension to the if-else statement. It is used
inthe scenario where there are multiple cases to be performed for different
conditions.
• In if-else-if ladder statement, if a condition is true then the statements definedin the
if block will be executed, otherwise if some other condition is true then
thestatements defined in the else-if block will be executed.
• at the last if none of the condition is true then the statements defined in the elseblock
will be executed.
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
else if(condition n)
{
//code to be executed if condition n is true
}
else
{
//code to be executed if all the conditions are false
}
#include<stdio.h>
int main()
{
int a,b; printf(“Enter the first number=”);
scanf(“%d”,&a);
printf(“Enter the second number=”);
scanf(“%d”,&b);
if(a==b)
{
printf(“Both numbers are equal”);
}
else if(a>b)
{
printf(“a is greater than b”);
}
else{
printf(“b is greater than a”);
}}
Switch Statement
Syntax=
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
Multiple Selection
• So far, we have only seen binary selection.
printf (“Wednesday”) ;
}
switch Statement Details
• The last statement of each case in the switch should almost always be
a break.
• The break causes program control to jump to the closing brace of the
switch structure.
• Without the break, the code flows into the next case. This is almost
never what you want.
• A switch statement will compile without a default case, but always
consider using one.
The switch Multiple-Selection
Structure
switch ( integer expression )
{
case value1/constant1 :
statement(s);
break ;
case value2/constant2 :
statement(s);
break ;
...
default:
statement(s);
break ;
}
switch Example
switch ( day )
break ;
break ;
equivalent nested if-else
case 3: printf (“Wednesday\n”) ; structure?
break ;
break ;
break ;
case 6: printf (“Saturday\n”) ;
break ;
break ;
Why Use a switch Statement?
• A nested if-else structure is just as efficient as a switch statement.
• However, a switch statement may be easier to read.
• Also, it is easier to add new cases to a switch statement than to a nested if-else
structure.
#include <stdio.h> int main(){
int week; /* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
Switch Case Program
{ case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
case 7: printf("Sunday");
break;
default: printf("Invalid input! Please enter week number between 1-7."); }
#include<stdio.h>
void main()
Switch Case Program
{
int a,b,n,s,m,su,d;
printf("enter two no's : ");
scanf("%d%d",&a,&b);
printf("enter 1 for sum\n2 for multiply\n3for subtraction\n4
for division: \n");
scanf("%d",&n);
switch(n)
{
case 1: s=a+b;
printf("sum=%d \n",s);
break;
case 2: m=a*b;
printf("multiply=%d \n",m);
break;
case 3: su=a-b;
printf("subtraction=%d \n",su);
break;
case 4: d=a/b;
printf("divission=%d \n",d);
break;
default: printf("wrong input \n");
break;
}
goto statement
The goto statement allows us to transfer control of the program to the
specified label.
Syntax of goto Statement
goto label;
. .. .. ...
... .. ...
label:
statement;
The label is an identifier. When the goto statement is encountered, the control of the
program jumps to label: and starts executing the code.
Break Statement
The break statement ends the loop immediately when it is encountered.
Its syntax is:
break;
The break statement is almost always used with if else statement inside the loop.
continue Statement
The continue statement skips the current iteration of the loop
and continues with the next iteration.
Its syntax is:
continue;
Many a times, we will require our functions to do some processing and return back the
result. This is accomplished with the return function in C.
Syntax-
return value;
Eg-
return 0;
C Loops
The looping can be defined as repeating the same process multiple
times until a specific condition satisfies.
• The looping simplifies the complex problems into the easy ones. It
enables us to alter the flow of the program so that instead of writing
the same code again and again, we can repeat the same code for a
finite number of times.
• For example, if we need to print the first 10 natural numbers then,
instead of using the printf statement 10 times, we can print inside a
loop which runs up to 10 iterations.
Looping Statements
Types of Loops in C
Depending upon the position of a control statement in a program, looping statement in C is classified into two types:
1. Entry controlled loop
2. Exit controlled loop
In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-
checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-
checking loop
Advantage of loops in C
➢ It provides code reusability.
➢ Using loops, we do not need to write the same code again and again.
➢ Using loops, we can traverse over the elements of data structures (array or
linked lists).
Types of C Loops
There are three types of loops in c language that is given below:
i) for loop
ii)while
iii)do while
Difference between for ,while & Do while loop
2. It is known as entry controlled loop It is known as entry controlled loop. It is known as exit controlled loop.
3. If the condition is not true first time If the condition is not true first time Even if the condition is not true
than control will never enter in a loop than control will never enter in a loop. for the first time the control will
enter in a loop.
4. There is no semicolon; after the There is no semicolon; after the There is semicolon; after the
condition in the syntax of the for loop. condition in the syntax of the while condition in the syntax of the do
loop. while loop.
5. Initialization and updating is the part of Initialization and updating is not the Initialization and updating is not
the syntax. part of the syntax. the part of the syntax
•for loop: -
•The for loop is used in the case where we need to execute
some part of the code until the given condition is satisfied.
•The for loop is also called as a per-tested loop. It is better to
use for loop if the number of iterations is known in advance.
The syntax: -
for(initialization;condition;increment/decrement)
{
//code to be executed
}
Flowchart of for loop: -
//Program for loop to display the numbers from 1 to 10
for(i=1;i<=10;i++) 4
5
{
6
printf("%d \n",i); 7
8
}
9
} 10
//for loop to display the numbers from 10 to 1
Output: -
#include<stdio.h>
10
int main()
9
{ 8
7
int i; for(i=10;i>=0;i--) 6
{ 5
printf("%d \n",i); 4
3
}
2
} 1
0
•while loop in C
•While loop is also known as a pre-tested loop.
•In general, a while loop allows a part of the code to be executed multiple
times depending upon a given boolean condition.
•It can be viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in advance.
Syntax: -
while(condition)
{
//code to be executed
}
Flowchart While loop
//Program using while loop to print numbers from 1 to 10.
#include<stdio.h> Output: -
int main() 1
{ 2
3
int i=1; 4
5
6
while(i<=10)
{ 7
8
printf("%d \n",i); i=i+1; 9
} 10
}
//Program using while loop to print numbers from 10 to 1.
Output: -
#include<stdio.h>
10
int main() 9
{ 8
int i=10; while(i>=0) 7
{ 6
5
printf("%d \n",i);
4
i=i-1; 3
} 2
} 1
0
do while loop in C:-
•The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of
several parts of the statements.
•The do-while loop is mainly used in the case where we need to execute the loop at least once. The
do-while loop is mostly used in menu-driven programs where the termination condition depends upon
the end user.
Syntax:-
Do
{
//code to be executed
}
while(condition);
Flowchart Do while loop
//Program using do while loop to print numbers from 1 to 10.
Output: -
#include<stdio.h>
1
int main() 2
{ 3
int i=1; 4
5
do
6
{ 7
printf("%d \n",i); 8
i++;
} 9
while(i<=10); 10
}
//Program using while loop to print numbers from 10 to 1.
#include<stdio.h> Output: -
int main()
{
10
int i=10;
9
do 8
{ 7
6
printf("%d \n",i);
5
i--; 4
} 3
while(i>=1); 2
} 1
0