[go: up one dir, main page]

0% found this document useful (0 votes)
3 views16 pages

Conditional and Looping

Uploaded by

swanandk878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Conditional and Looping

Uploaded by

swanandk878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CONDITIONAL

STATEMENTS
format:
if (expression)

statement1;
else

statement2;

The else portion is optional

• Nested if-else :Test for multiple cases by placing if-else construct inside another if-else
Program to calculate largest of two numbers using if condition
#include <stdio.h>
main()
{
int a=10, b=20, max;
max = a;
if(b > max)
max=b;
printf(“Larger number is %d\n”,max);
}
Program to check validity of age to vote
#include <stdio.h>
main()
{
int age;
scanf(%d”,&age);
if(age >= 18)
printf(“Eligible to vote”);
else
printf(“Not eligible to vote”);
}
Multiple if condition
• Tests for multiple if conditions
• Once condition is met, rest of the statements are skipped
• Format:
if( expression1 )
statement1;
else if( expression2 )
statement2;
…………..
else
statementn;
next_statement
Example

#include<stdio.h>
main()
{
int grade;
if (grade >= 90)
printf( “Grade A\n”);
else if ( grade >= 80)
printf( “Grade B\n”);
else if (grade >= 70)
printf( “Grade C\n”);
else if (grade>= 60)
printf (“Grade D\n”)
else
printf( “Grade F\n”);
}
Switch-case construct
• Useful when a variable or expression is tested for all the values it can assume and different
actions are taken accordingly
Format: switch (expression)
{
case expression_1: statement_1;
break;
……….
case expression_n: statement_n;
break;
default: statement;
}
Expression has to be of integral type
No two expressions in case should evaluate to same value
Example:
#include<stdio.h>
main()
{
char operator;
float op1, op2;
printf ( “ \n Please enter two numbers and operator: ");
scanf("%f %f %c",&op1,&op2, &operator);
switch (operator)
{
case '+’ : printf ( “Sum of %f and %f is %f \n", op1 + op2) ;
break;
case '-’ : printf ( “Difference of %f and %f is %f \n", op1 - op2);
break;
case '* ': printf ( “Product of %f and %f is %f \n ", op1 * op2);
break;
default: printf ( “Sorry…option not available \n”);
break;
}
Tips and traps of switch case
(i) The expression in switch(expr) should return an integer or character constant.
(ii) A case constant can only be a integer or character constant. Expression like
case i<=20 or float values are not allowed. Eg case 12.5: invalid
case j<=10: invalid
(iii) A break statement is essential to come out of the switch after executing a
particular case. Take examples and show what the output will be if we drop a break
statement.
(iv) Sometimes there may not be any statement is some case statements,but they
still may be useful.
(v) Multiple statements inside a case need not be enclosed in parenthesis.
(vi) if there is no default it doesn’t give error but control falls to the next statement.
Example: main()
{
char ch;
printf(“Enter a character a,b,or c \n”);
scanf(“%c”, &ch);
switch (ch)
{
case ‘a’ :
case ‘A’:
printf(“You have entered A\n”);
break;
case ‘b’ :
case ‘B’:
printf(“You have entered B\n”);
break;
case ‘c’ :
case ‘C’:
printf(“You have entered A\n”);
break;
default:
printf(“invalid input\n”);
}
Is switch-case a replacement to if-elseif-else
Comparison between if-elseif-else and switch case-

If-elseif-else Switch-case

It is cumbersome to match the ifs and It leads to a more structured program and
the elses and to manage the parenthesis is more manageable if the conditions are
many.

Can be used to test all types of Can be used only if the condition is a int
conditions or char constant. Floats or
logical/relational expressions are not
allowed.
Exercise: Write a program to read a number between
(1-5) and print its value using if- elseif-else construct.
Rewrite the above example using switch case.
#include<stdio.h> #include<stdio.h> #include<stdio.h>
int main() int main() int main()
{ { {
int a=1;
if (-1) if (1) if (0)
{ { {
printf(“It’s True”); printf(“It’s True”); printf("It’s True");
} } }
else else else
printf(“It’s False”); printf(“It’s False”); printf("It’s False");
} } }

o/p: True o/p: True o/p: False


Eg: 4
Eg: 5 #include<stdio.h> int x=1;
#include<stdio.h>
int main() if(x==0);
int main()
{ printf(“x is zero”);
{
int x; printf("end of pgm");
int x;
if(!x)
if(x)
{ int x=0;
{
printf("It’s True"); if(x==0);
printf("It’s True");
} printf(“x is zero”);
}
else else
else
printf("It’s False"); printf(“x is non zero”);
printf("It’s False");
printf("%d\n",x);
printf("%d\n",x);
}
}

o/p: False o/p: True


int x=0, y=1; Ans: x=1, y=1
if(x++&&++y); Since the first part of the compound condition is false it won’t
printf(“x =%d, y=%d”, x, y); go to second part of the condition ++y.

int x=0, y=1; Ans: x=1, y=2


if(++x&&++y); Since the first part of the compound condition is true as it is
pre-increment it will go to the second part of the condition
printf(“x =%d, y=%d”, x, y); ++y.

int x=0, y=1;


Ans: x=1, y=2
if(x++||++y); Since the first part is false it goes to the second part and
printf(“x =%d, y=%d”, x, y); increases y.
int x=0, y=1; Ans: x=1, y=1
Since the first part is true it does not go to the second part and
if(++x||++y); increase y.
printf(“x =%d, y=%d” x, y);

o/p
Hello
Printf(“%d”, printf(“Hello\n”));
6

If(printf(“hi\t”)) o/p
Printf(“True block”); hi True block

a=0;
if(a++)
printf(“True block”); O/P:
False Block – a will be zero during condition check, hence false
else
printf(“False block”);

You might also like