Module 2
Module 2
:- 2022-2023
Chapter No: 2
Prof. Vandana V.
If Statement
The if statement is used to check some given condition and perform some operations depending
upon the correctness of that condition. It is mostly used in the scenario where we need to
perform the different operations for the different conditions. The syntax of the if statement is
given below.
1. if(expression){
2. //code to be executed
3. }
FLOWCHART
Prof. Vandana V.
EXAMPLE (Simple if)
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d", &number);
if( number%2 == 0)
{
printf("%d is even number", number);
}
printf(“End of Program”);
return 0;
}
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
simiulteneously. Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition. The syntax of the if-else statement is given below.
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }
Prof. Vandana V.
Flowchart of the if-else statement in C
EXAMPLE
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d", &age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
return 0;
}
Prof. Vandana V.
The Nested If Statement
In this case, the condition available in the next if statement (the second statement) will only get
evaluated if the evaluation of the condition available in the first statement turns out to be true. This
occurs throughout the program that has a nested statement
Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
FLOWCHART
EXAMPLE
int main()
{
int age, sal;
Prof. Vandana V.
if(age > 50)
{
if( sal > 45000)
{
sal = sal +10000;
printf("\n Incremented Salary =%d ",sal);
}
else
{
sal = sal + 5000;
printf("\n Incremented Salary = %d",sal);
}
}
else
{
sal = sal +3000;
printf("\n Incremented Salary = %d",sal);
}
printf("\n \nEND OF PROGRAM\n");
return 0;
}
Prof. Vandana V.
The Else If Ladder
In this statement, the execution of an array of instructions occurs only when the available
condition is correct. The verification of the next condition occurs when this first condition is
incorrect. In case all of the specifications fail even after the verification, then there will be an
execution of the default block statements. The remainder of the program’s ladder is shown
below,
Syntax:
if(boolean_expression 1)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else if( boolean_expression 2)
{ /* Executes }
else
{ /* executes }
Prof. Vandana V.
EXAMPLE
#include <stdio.h>
int main () {
int a = 100,b=80,c=90;
if( a >> b && a>>c )
{
printf(“a is greater" );
}
else if(b>>a && b>>c )
{
printf(“b is greater\n " );
}
else if( c>>a && c>>b )
{
Prof. Vandana V.
printf(“ C is greater\n" );
}
else
{
printf(“ All are Equal\n" );
}
Printf(“End of Program”);
return 0;
}
Prof. Vandana V.
Every block is shown here with the use of the case keyword. As a matter of fact, the case keyword is
also followed by the block label. Note that the break statement and default block statement are very
optional in the case of the switch statement.
FLOWCHART
Prof. Vandana V.
EXAMPLE
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}
return 0;
}
Prof. Vandana V.
DIFFERENCE IN IF ELSE AND SWITCH
If-else switch
Definition Depending on the condition in the 'if' The user will decide which statement is to be
statement, 'if' and 'else' blocks are executed.
executed.
Expression It contains either logical or equality It contains a single expression which can be
expression. either a character or integer variable.
Evaluation It evaluates all types of data, such as It evaluates either an integer, or character.
integer, floating-point, character or
Boolean.
Sequence of First, the condition is checked. If the It executes one case after another till the break
execution condition is true then 'if' block is keyword is not found, or the default statement
executed otherwise 'else' block is executed.
Default execution If the condition is not true, then by If the value does not match with any case, then
default, else block will be executed. by default, default statement is executed.
Editing Editing is not easy in the 'if-else' Cases in a switch statement are easy to maintain
statement. and modify. Therefore, we can say that the
removal or editing of any case will not interrupt
the execution of other cases.
Speed If there are multiple choices If we have multiple choices then the switch
implemented through 'if-else', then the statement is the best option as the speed of the
speed of the execution will be slow. execution.
Prof. Vandana V.
3. The Goto Statement
The Goto statement is especially known in the case of jumping control statements. We
mainly use the goto statement when we want to transfer a program’s control from any one
block to another. Also, we use the goto keyword for the declaration of the goto statement.
The syntax of the goto statement is as follows:
goto name_of_label;
name_of_label;
In the syntax given above, we have used the goto as a keyword for transferring the control
of the program to the name_of_label.
Here, the name_of_label refers to a variable’s name. Thus, in simpler words, the goto here
will ultimately transfer the program’s control to the name_of_label. Thus, there will occur
an execution of all those statements that are followed by the name_of_label.
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. There are three types of loops used in the C language. In this part of
the tutorial, we are going to learn all the aspects of C loops.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
Prof. Vandana V.
For Loop
Definition: A for loop is a repetition control structure which allows us to write a loop
that is executed a specific number of times. The loop enables us to perform n number
of steps together in one line.
Syntax:
FLOWCHART
#include <stdio.h>
int main()
{
int i=0;
Prof. Vandana V.
}
return 0;
}
While Loop
The while loop in c is to be used in the scenario where we don't know the number of iterations
in advance. The block of statements is executed in the while loop until the condition specified
in the while loop is satisfied. It is also called a pre-tested loop.
1. while(condition){
2. //code to be executed
3. }
Example
// C program to illustrate while loop
#include <stdio.h>
int main()
int i=1;
Prof. Vandana V.
printf( "Hello World\n");
i++;
return 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.
do{
//code to be executed
}while(condition);
//Simple program of c language do while loop where we are printing the table of 1.
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
}
Prof. Vandana V.
Branching Statements
● Branching statements are the statements used to jump the flow of execution from one
part of a program to another.
● The branching statements are mostly used inside the control statements. Java has
mainly three branching statements, i.e., continue, break, and return.
● The branching statements allow us to exit from a control statement when a certain
condition meet.
break statement
The break is a keyword in C which is used to bring the program control out of the
loop. The break statement is used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops. The break statement in C can be
used in the following two scenarios:
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
C continue statement
The continue statement in C language is used to bring the program control to the beginning
of the loop. The continue statement skips some lines of code inside the loop and continues with
the next iteration. It is mainly used for a condition so that we can skip some code for a particular
condition.
Prof. Vandana V.
Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped
Example
#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}
C goto statement
The goto statement is known as jump statement in C. As the name suggests, goto is used to
transfer the program control to a predefined label. The goto statment can be used to repeat some
part of the code for a particular condition. It can also be used to break the multiple loops which
can't be done by using a single break statement. However, using goto is avoided these days
since it makes the program less readable and complecated.
Syntax:
label:
//some part of the code;
goto label;
#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
Prof. Vandana V.
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
FLOWCHART
Prof. Vandana V.