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

C-Lecture-4 Control Statement

Uploaded by

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

C-Lecture-4 Control Statement

Uploaded by

pkpartho069
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Control Statement

Decision Making
In practice, users may have a number of
situations where they may have to change the
order of statements based on certain
conditions. This is decision making.

The decision making statements in C are as


follows,
i) if statement
ii) switch statement
iii) conditional operator statement
iv) goto statement
The if statement
It is basically a two way decision-statement. It
takes the following form,
if(test expression)
This point of program has two paths to follow,
one for the true condition and the other for
the false condition.
That is shown in the following figure:

The different forms are,


i) Simple if statement
ii) if....else statement
iii) Nested if......else statement
Simple if statement
The general form of a simple if statement is as
follows:
if (test expression)
{
statement-block;
}
statement – x;
Next statement;
Simple if statement (con’d)
#include <stdio.h>
void main()
{
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num>0) {
/* checking whether number is greater than 0 or not. */
printf("Number = %d\n",num);
} /*If test condition is true, statement above will be executed, otherwise it
will not be executed */
printf("The if statement in C programming is easy.");
}
The if…else statement
The general form of the if....else statement is as
follows,
if (test expression)
{
True-block statements;
}
else
{
False-block statements;
}
statement-x;
Next statement;
The if…else statement (con’d)
#include <stdio.h>
int main()
{
int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if((num%2)==0) //checking whether remainder is
0 or not.
printf("%d is even.",num);
else
printf("%d is odd.",num);
}
Check whether it is vowel or not
#include <stdio.h>

int main()
{
char ch;

printf("Enter a character\n");
scanf("%c", &ch);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o'


|| ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);

return 0;
}
Nested if else statement
When a series of decisions are involved, we
may have to use more than one if else
statement in nested form as shown below:
Nested if else statement (con’d)
#include <stdio.h>
int main( )
{
char letter;
printf(“Enter a character ?”);
scanf(“%c”, &letter);
if (letter >= ‘A’)
{
if (letter <= ‘Z’)
printf(“The character is within A to Z\n”);
}
else
printf(“The character is not Alphabet\n”)
return 0;

}
Nested if else statement (con’d)
#include <stdio.h>
void main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>b)
{
if(a>c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
The else if ladder
When a multipath decision is involved then we use else if ladder. A
multipath decision is a chain of ifs in which the statement associated
with each else in an if. It takes the following general form:
The else if Ladder (con’d)
#include<stdio.h>
void main(){
int marks;
printf("Give the value of percentages\n");
scanf("%d", &marks);
if(marks > 80)
printf("Grade : A");
else if(marks > 60
printf("Grade : B");
else if(marks > 50)
printf("Grade : C");
else if(marks > 40)
printf("Grade : D");
else
printf("Grade : Fail");
}
#include<stdio.h>
int main()
{
float m1,m2,m3,m4,m5,avg,per;
printf("Enter 5 subject marks out of 100:\n");
scanf("%f %f %f %f %f",&m1,&m2,&m3,&m4,&m5);
per = (m1+m2+m3+m4+m5)*100/500;
printf("Marks is %0.2f percentage. \n So your result is ",per);
if(per>=60)
printf("1st Division");
else if(per>=50 && per<=59)
printf("2nd Division");
else if(per>=40 && per<=49)
printf("3rd Division");
else if(per<40)
printf("Fail");
return 0;
}
Switch Statement
switch (expression)
{
case value 1:
program statement;
case value 2:
program statement;
........
break;
case value n:
program statement;
.............
break;
default:
............
break;
}
Switch Statement
void main ()
{ char grade ;
printf("\nEnter your grade to check [Input must be A,B,C,D or F]\n\n");
scanf("%c",&grade);
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" ); }

printf("Your grade is %c\n", grade ); }


Switch Statement (con’d)
#include <stdio.h>
int main(void)
{
int menu, numb1, numb2, total;
printf("enter in two numbers -->");
scanf("%d %d", &numb1, &numb2);
printf("enter is choice\n");
printf("1 = addition\n");
printf("2 = subtraction\n");
scanf("%d:", &menu);
switch (menu)
{
case 1: total = numb1 + numb2; break;
case 2: total = numb1 - numb2; break;
default: printf("Invalid option selected\n");
}
if (menu == 1)
printf("%d plus %d is %d\n", numb1, numb2, total );
else if (menu == 2)
printf("%d minus %d is %d\n", numb1, numb2, total);
}
# include <stdio.h>
int main() {
char op; float num1,num2;
printf("Select an operator either + or - or * or / \n");
scanf("%c",&op);
printf("Enter two operands: ");
scanf("%f%f",&num1, &num2);
switch(op) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct. Please try again");
break; }
return 0;
}
Conditional operator
Write a c program to find largest among three numbers using
conditional operator

#include<stdio.h>
int main()
{
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is: %d",big);
return 0;
}
goto statement
When compiler encounters goto statement in a C program,
the control
jumps to the corresponding label mentioned along with goto.
goto statement (con’d)
#include<stdio.h>
int main()
{
int n=0;
a: n=n+1; /* 'a:' is the label */
printf("\t %d",n);
if(n>=100)
{
goto b;
}
goto a; /* Will go to the 'a' label
*/
b: printf("\n End of the program"); /* 'b' label */
}
Write a program that will read the value of x and evaluate the following function
y= { 1 for x >0
0 for x=0 (a) nested if statements.
-1 for x<0 (b) conditional operator ?

Solution:
/*…………evaluate the (b)conditional operator:
equation………..*/
#include<stdio.h>
(a)nested if statements:
int main()
#include<stdio.h>
int main() {
{ float y, x;
float x,y; printf("Input x\n");
printf("Input x\n"); scanf("%f",&x);
scanf("%f",&x); y=(x!=0)? ((x>0)?1:-
if(x!=0) 1) :0;
{
printf("%d",y);
if(x>0)
printf("y=1"); return 0;
if(x<0) }
printf("y=-1");
}
if(x==0)
printf("y=0");
return 0;
#include <stdio.h>

int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}
#include <stdio.h>

int main() {
int year;
year = 2016;

if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))


printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);

return 0;
}

You might also like