0% found this document useful (0 votes)
54 views30 pages

If Else, - Nested If, If - Else Ladder - Switch Statement - Goto Statement

The document discusses different conditional statements in C programming including if, if-else, nested if-else, if-else ladder (else if), switch statement, and goto statement. It provides syntax and examples for each statement type. Key points covered include using conditional statements to make decisions based on conditions, the syntax and use of if, if-else, else if ladder, and switch statements, and when to use goto statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views30 pages

If Else, - Nested If, If - Else Ladder - Switch Statement - Goto Statement

The document discusses different conditional statements in C programming including if, if-else, nested if-else, if-else ladder (else if), switch statement, and goto statement. It provides syntax and examples for each statement type. Key points covered include using conditional statements to make decisions based on conditions, the syntax and use of if, if-else, else if ladder, and switch statements, and when to use goto statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lab-2

• If else,
• Nested if, If –else Ladder .
• Switch Statement .
• Goto Statement .
Already Know
• To read values from user
• Write arithmetic expressions in C
• Print values in a formatted way
Click to add text
Yet to learn
• Check a condition
Conditional Statements
• Conditional statements help you to make a decision based on certain
conditions. These conditions are specified by a set of conditional
statements which are evaluated to a boolean value true or false.
There are following types of conditional statements in C.
• If statement
• If-Else statement
• Nested If-else statement
• If-Else If ladder
• Switch statement
If statement

The single if statement in C language is used to execute the code


if a condition is true. It is also called one-way selection statement.

Syntax
if(expression)
{
//code to be executed
}
If statement- Example
#include<stdio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(num%2==0)
{
printf("%d number in even",num);
}
}
If-else statement

The if-else statement in C language is used to execute the code if


condition is true or false. It is also called two-way selection
statement.
Syntax
if(expression)
{
//Statements
}
else
{
//Statements
}
If-else statement

#include<stdio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(num%2==0)
{
printf("%d number in even",num);
}
else
{
printf("%d number in odd",num);
}
}
Nested If-else statement

The nested if...else statement is used when a program requires more than one test expression. It is also called a multi-way selection statement. When a
series of the decision are involved in a statement, we use if else statement in nested form.

Syntax
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
• Example
#include<stdio.h>
void main( )
{
int a,b,c;
printf("Please Enter 3 number");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b>c)
{
printf("b is greatest");
}
else
{
printf("c is greatest");
}
}
}
If..else If ladder

• The if-else-if statement is used to execute one code from multiple


conditions.
• It is also called multipath decision statement.
• It is a chain of if..else statements in which each if statement is
associated with else if statement and last would be an else statement.
Syntax
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{
//statements
}
Example
#include<stdio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
}
}
Switch statement
• Switch statement in C tests the value of a variable and compares it
with multiple cases. Once the case match is found, a block of
statements associated with that particular case is executed.
• Each case in a block of a switch has a different name/number which is
referred to as an identifier. The value provided by the user is
compared with all the cases inside the switch block until the match is
found.
• If a case match is NOT found, then the default statement is executed,
and the control goes out of the switch block.
Switch -Syntax
• The expression can be integer expression or a character expression.
• Value-1, 2, n are case labels which are used to identify each case individually.
Remember that case labels should not be same as it may create a problem
while executing a program. Suppose we have two cases with the same label
as ‘1’. Then while executing the program, the case that appears first will be
executed even though you want the program to execute a second case. This
creates problems in the program and does not provide the desired output.
• Case labels always end with a colon ( : ). Each of these cases is associated
with a block.
• A block is nothing but multiple statements which are grouped for a particular
case.
• Whenever the switch is executed, the value of test-expression is compared with all
the cases which we have defined inside the switch.
• Suppose the test expression contains value 4. This value is compared with all the
cases until case whose label four is found in the program. As soon as a case is found
the block of statements associated with that particular case is executed and control
goes out of the switch.
• The break keyword in each case indicates the end of a particular case. If we do not
put the break in each case then even though the specific case is executed, the
switch in C will continue to execute all the cases until the end is reached. This
should not happen; hence we always have to put break keyword in each case.
• Break will terminate the case once it is executed and the control will fall out of the
switch.
• The default case is an optional one. Whenever the value of test-
expression is not matched with any of the cases inside the switch,
then the default will be executed. Otherwise, it is not necessary to
write default in the switch.
• Once the switch is executed the control will go to the statement-x,
and the execution of a program will continue.
Example
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
•}
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. 
• Syntax:
label:   
//some part of the code;   
goto label;  
Example
#include <stdio.h>  
int main()   
{  
  int num,i=1;   
  printf("Enter the number whose table you want to print?");   
  scanf("%d",&num);  
  table:   
  printf("%d x %d = %d\n",num,i,num*i);  
  i++;  
  if(i<=10)  
  goto table;    
}  
Nested If Statement in C
Switch Statement
• Useful when the selection is based on the value of a
single variable or of a simple expression (called the
controlling expression)

• Value of this expression may be of type int or char , but


not of type double
Output

BattleshipCruiserDestroyerFrigateNo match

When input is b
Enter an operator (+, -, *,): * Enter two operands: 1.5 4.5 1.5 * 4.5 = 6.8

3.  WAP to create a simple calculator in C programming using the switch


statement.
Input
Enter an operator (+, -, *,): *
Enter two operands: 1.5 4.5
Output
1.5 * 4.5 = 6.8
Exercises
1. WAP to check whether the smallest of three numbers
2. WAP in C to give Grades According To Mark Obtained

You might also like