0% found this document useful (0 votes)
2 views35 pages

CONTROL STATEMENTS frAluF

The document provides an overview of control statements in programming, detailing types such as decision-making and loop control statements. It explains various forms of if statements, including simple if, if-else, nested if, and else-if ladders, along with their syntax and examples. Additionally, it covers the switch statement for multi-way branching, illustrating its usage with examples.

Uploaded by

akash sarkar
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)
2 views35 pages

CONTROL STATEMENTS frAluF

The document provides an overview of control statements in programming, detailing types such as decision-making and loop control statements. It explains various forms of if statements, including simple if, if-else, nested if, and else-if ladders, along with their syntax and examples. Additionally, it covers the switch statement for multi-way branching, illustrating its usage with examples.

Uploaded by

akash sarkar
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/ 35

CONTROL STATEMENTS

• The statements which are used to control the flow of control in a


program is called Control Statements.
• Types of Control Statements:-
1. Decision Making Control Statements
2. Loop Control Statements
• Decision Making Control Statements:-
These Statements allow the computer to take a decision as to which
statement is to be executed next based on certain condition.
• Types Decision Making Control Statements:-
1) If statement 3) nested if statement 5) switch statement

2) If else statement 4) else if ladder


Simple if statement:-
• Simple if is a one-way branching statement.
• When the condition of if is true, the statements within the if block are
executed otherwise the if block is skipped by transferring the control
directly to the first statement after the if block.
• Syntax:-
if ( condition)
{
Statement;
}
where
Statement can be simple statement or compound statement.
Flowchart:-
Forms of if Statements:-
Simple Statement in if - block Compound Statement in if -
block

if(condition) if(condition)
{ {
Statement 1; Statement 1;
} Statement 2;
----------------
Statement 3;
}
Examples:-
Program to check whether accepted number is greater than 20 or not.
#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf(“\n Enter the number:”);
scanf(“%d”, &a);
if(a>20)
printf(“\n The number is greater than 20”);
getch();
}
Output:-
Enter the number: 40
The number is greater than 20
Program to check equivalence of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &a, &b);
if(a==b)
printf(“\n Two numbers are equal”);
}
Output:-
Enter two numbers: 10 10
Two numbers are equal
Program to find largest of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
if(num1> num2)
printf(“\n %d is largest number”, num1);
if(num2> num1)
printf(“\n %d is largest number”, num2);
}
Output:-
1) Enter two numbers: 10 20
20 is largest number
2) Enter two numbers: 30 15
30 is largest number
If Else Statement
• if else is a two-way branching statement.
• When the condition of if is true, the statements within the if block are executed
otherwise the else block are executed.
• Syntax:-
if(condition)
{
True-block statement(s); if block
}
else
{
False-block statement(s); else block
}
Flowchart
Different Forms of if-else statement:-
if (condition) if (condition) if (condition) if (condition)
statement 1; { statement 1; {
else statement 1; else statement 1;
statement 2; statement 2; { statement 2;
} statement 2; }
else statement 3; else
statement 3; } {
statement 3;
statement 4;
}
Examples:-
Program to find largest of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
if(num1> num2)
printf(“\n %d is largest number”, num1);
else
printf(“\n %d is largest number”, num2);
}
Output:-
1) Enter two numbers: 10 20
20 is largest number
Program to check whether a number is even or odd.
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf(“\n Enter a number:”);
scanf(“%d”, &num);
if(num % 2==0)
printf(“\n Number is Even”);
else
printf(“\n Number is Odd”);
}
Output:-
Enter a number: 5
Number is Odd
Nesting of If Else Statements

• A Structure consisting of an If statement within another if statement or


else block is called as nesting of if else statements.
Syntax:-
Flowchart:-
Different forms of Nested If Statements
Syntax Example
if(condition 1) if (marks >= 60)
{ {
if(condition 2) if ( marks < 70)
{ printf(“First Class”);
statement 1; }
statement 2;
}
}
if(condition 1) if( gender == ‘M’)
{ {
if(condition 2) if ( salary > 5000)
{ {
statement 1; printf(“ You have to pay tax”);
statement 2; }
} else
else {
} printf(“No tax”);
{ }
statement 1; }
statement 2;
}
}
Syntax Example
if(condition 1) if( gender == ‘M’)
{ {
if(condition 2) if ( salary > 5000)
statement 1; printf(“ You have to pay tax”);
else else
statement 2; printf(“No tax”);
} }
else else
statement 3; printf(“No tax for women”);
if(condition 1) if( i < 0)
statement 1; printf(“Negative number”);
else else
{ {
if(condition 2) if(i > 100)
statement 2; printf(“ I is greater than 100”);
else else
statement 3; printf(“ I is not greater than 100”);
} }
Syntax Example
if(condition 1) If ( a > b)
{ {
if(condition 2) if ( a > c)
statement 1; printf(“ %d is largest”, a);
else else
statement 2; printf(“ %d is largest”, c);
else }
{ else
if(condition 3) {
statement 3; if ( c > b)
else printf(“ %d is largest”, c);
statement 4; else
} printf(“ %d is largest”, b);
}
Example:-
Program to find the smallest among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, smallest;
printf(“\n Enter three numbers:”);
scanf(“%d %d %d”, &a , &b, &c);
if( a > b)
{
if (a < c)
smallest =a;
else
smallest =c;
}
else
{
if (c < b)
smallest = c;
else
smallest = b;
}
printf(“ \n The smallest = %d”, smallest);
}
Output:-
Enter three numbers: 20 10 15
The smallest = 10
Else-if ladder

• The else if ladder is a multi way decision maker which contains two or
more else-if , from which one block is executed.
Syntax:-
if ( condition 1)
{
statement 1;
}
else If ( condition 2)
{
statement 2;
}
else If ( condition 2)
{
statement 3;
}
----------------
----------------
else if ( condition n-1)
{
statement n-1;
}
else
}
statement n;
----------------
}
Flowchart:-
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
float percentage;
printf(“\n Enter the percentage: “);
scanf(“%f”, &percentage);
if(percent >= 75)
printf(“\n Distinction”);
else if(percent >= 60)
printf(“\n First Class”);
else if(percent >= 50)
printf(“\n Second class”);
else if(percent >= 40)
printf(“\n Third Class”);
else
printf(“\n Fail”);
}
Output:-
1) Enter the percentage: 44
Third Class
2) Enter the percentage: 90
Distinction
Switch Statement:-
• The switch statement provides multi-way branching.
• The switch allows the user to select any one of the alternatives
depending on the value of the expression.
• The expression present within a parenthesis of switch statement must
be of the type int or char.
• Based on the expression the control is transferred to a particular case
label and the statement associated with that label are executed.
• The case label must be int or char type.
• The case default is executed only when none of the cases are true to
the value of the expression present in switch construct.
Syntax:-
switch(expression)
{
case label 1: statement 1;
………………
break;
case label 2: statement 2;
………………
break;
case label 3: statement 3;
………………
break;
………………………………………..
………………………………………..
case label n: statement n;
………………
break;
default: default statements;
…………………………
}
statement-x;
Flowchart:-
Examples:-
1) int a=2;
switch(a)
{
case 1: printf(“Number is 1”);
break;
case 2: printf(“Number is 2”);
break;
case 3: printf(“Number is 3”);
break;
default: printf(“Invalid number”);
}
Program to check whether the input character is vowel or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“\n Enter a character:”);
scanf(“%c”, &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’: printf(“%c is vowel”, ch);
break;
default: printf(“%c is not a vowel”, ch);
}
}
Program to perform the operations Add, Subtract, Multiplication, Division, Largest of two
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, ch;
printf(“\n 1. Addition”);
printf(“\n 2. Subtraction”);
printf(“\n 3. Multiplication”);
printf(“\n 4. Division”);
printf(“\n 5. Largest of two numbers”);
printf(“Enter your choice:”);
scanf(“%d”, &ch);
if(ch <= 5 && ch > 0)
{
printf(“ Enter two numbers:”);
scanf(“%d %d”, &a, &b);
}
switch(ch)
{
case 1: c = a+b;
printf(“\n Addition: %d”, c);
break;
case 2: c = a-b;
printf(“\n Subtraction: %d”, c);
break;
case 3: c = a*b;
printf(“\n Multiplication: %d”, c);
break;
case 4: c = a/b;
printf(“\n Division: %d”, c);
break;
case 5: if(a>b)
printf(“\n a is larger”);
else if (b> a)
printf(“\n b is larger”);
break;
default: printf(“Invalid Choice”);
}
Output:-
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Largest of two numbers
1) Enter your choice: 5
Enter two number : 8 9
b is larger
2) Enter your choice: 4
Enter two number : 20 10
Division : 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy