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

C Programming Condition

The document provides an overview of conditional statements and branching in programming, specifically focusing on the use of if, if-else, and switch statements. It explains how to evaluate conditions using relational operators and logical connectives, along with examples of grade computation and finding the maximum or minimum of numbers. Additionally, it discusses the importance of proper indentation and the potential pitfalls of nesting if-else structures.

Uploaded by

ABHISHEK GOUTAM
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 views

C Programming Condition

The document provides an overview of conditional statements and branching in programming, specifically focusing on the use of if, if-else, and switch statements. It explains how to evaluate conditions using relational operators and logical connectives, along with examples of grade computation and finding the maximum or minimum of numbers. Additionally, it discusses the importance of proper indentation and the potential pitfalls of nesting if-else structures.

Uploaded by

ABHISHEK GOUTAM
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/ 53

CS1101: Foundations of Programming

Condition and Branching

Dept. of Computer Science & Engineering


Indian Institute of Technology Patna

IIT Patna 1
Conditional statement
• Allow different sets of instructions to be executed depending on truth or falsity
of a logical condition
• How do we specify these conditions?
• Using expressions and relational operators (>,<,>=,<=,==,!=)
• Non-zero value signifies condition is true
• Value 0 indicates the condition is false
• Using logical connectives (&&, ||, !)
• Example:
(a + b) <= 60
((age == 18) && (height > 5))
((grade == 'A') && ((bankbalance > 6000) || (age >= 45))

IIT Patna 2
Branching: if statement
if(expression) if(expression){
statement; Block of statements
}

Indentation - (leaving horizontal spaces in the following lines of if) easy to read

The expression is evaluated and if it is non-zero, the statement gets exe-


cuted. For 2nd case whole block of statements get executed.

Semi-colon may be noted in the first case.

IIT Patna 3
Example

if(marks >= 90){


printf("Passed\n"); marks>=90
true print ”Passed”
printf("Good luck\n"); print ”Good luck”
}
false

IIT Patna 4
Branching: if-else statement
if(expression){ if(expression){
Block of statements; Block of statements;
} }
else{ else if(expression){
Block of statements; Block of statements;
} }
else{
Block of statements;
}

IIT Patna 5
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90)
printf("A");
else if(marks>=80)
printf("B");
else if(marks>=70)
printf("C");
else
printf("failed");
return 0;
}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
printf("B");
else if(marks>=70)
printf("C");
else
printf("failed");
return 0;
}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
printf("B"); m≥90

else if(marks>=70)
printf("C");
else
printf("failed");
return 0;
}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
printf("B"); m≥90

else if(marks>=70) yes


printf("C"); Write A
else
printf("failed");
return 0;
}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
no
printf("B"); m≥90 m≥80

else if(marks>=70) yes


printf("C"); Write A
else
printf("failed");
return 0; Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
no
printf("B"); m≥90 m≥80

else if(marks>=70) yes yes


printf("C"); Write A Write B
else
printf("failed");
return 0; Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
no no
printf("B"); m≥90 m≥80 m≥70

else if(marks>=70) yes yes


printf("C"); Write A Write B
else
printf("failed");
return 0; Stop Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m
printf("A");
else if(marks>=80)
no no
printf("B"); m≥90 m≥80 m≥70

else if(marks>=70) yes yes yes


printf("C"); Write A Write B Write C
else
printf("failed");
return 0; Stop Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start

scanf("%d",&marks);
if(marks>=90) Read m Write F
printf("A");
no
else if(marks>=80)
no no
printf("B"); m≥90 m≥80 m≥70

else if(marks>=70) yes yes yes


printf("C"); Write A Write B Write C
else
printf("failed");
return 0; Stop Stop Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks; Start Stop

scanf("%d",&marks);
if(marks>=90) Read m Write F
printf("A");
no
else if(marks>=80)
no no
printf("B"); m≥90 m≥80 m≥70

else if(marks>=70) yes yes yes


printf("C"); Write A Write B Write C
else
printf("failed");
return 0; Stop Stop Stop

}
IIT Patna 6
Example: grade computation
int main(){
int marks;
scanf("%d",&marks);
if(marks>=90)
{ printf("A\n"); printf("Well done\n"); }
else if(marks>=70)
printf("B");
else if(marks>=50)
printf("C");
else
{ printf("failed\n"); printf("Study hard\n"); }
return 0;
}
IIT Patna 7
Maximum of two numbers
#include <stdio.h> Start

int main()
{ Read x,y
int x,y;
scanf("%d%d",&x,&y);
yes x>y no
if(x>y)
printf("Largest is %d\n",x);
else print x print y
printf("Largest is %d\n",y);
return 0;
Stop Stop
}

IIT Patna 8
Example: minimum of 3 numbers
Start

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z

x>y

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z

yes x>y

min=y

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z

yes x>y no

min=y min=x

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z z>min

yes x>y no

min=y min=x

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z yes z>min

yes x>y no Write min

min=y min=x

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z yes z>min

yes x>y no Write min

min=y min=x
Stop

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z yes z>min no

yes x>y no Write min Write z

min=y min=x
Stop

IIT Patna 9
Example: minimum of 3 numbers
Start

Read x,y,z yes z>min no

yes x>y no Write min Write z

min=y min=x
Stop Stop

IIT Patna 9
Minimum of three numbers
#include <stdio.h>
int main()
{
int x,y,z,min;
scanf("%d%d%d",&x,&y,&z);
if(x > y) { min=y; }
else { min=x;}
if(z > min) { printf("%d",min); }
else { printf("%d",z); }
return 0;
}

IIT Patna 10
Maximum of three numbers
int main(){
int x,y,z;
scanf("%d%d%d", &x, &y, &z);
if((x >= y) && (x >= z))
printf("%d",x);
if((y >= x) && (y >= z))
printf("%d",y);
if((z >= y) && (z >= x))
printf("%d",z);
return 0;
}

IIT Patna 11
Nesting of if-else structures
• It is possible to nest if-else statements, one within another
• All if statements may not be having the else part
• Confusion??
• Rule to be remembered:
• An ”else” clause is associated with the closest preceding unmatched ”if”

IIT Patna 12
Dangling else problem
• if(exp1) if(exp2) stamta else stmtb

if(exp1){ if(exp1){
if(exp2) if(exp2)
stmta stmta
else }
stmtb else
} stmtb

While implementing a code, it is good practice


to provide explicit { and } to avoid any confusion
IIT Patna 13
Dangling else problem
• if(exp1) if(exp2) stamta else stmtb

if(exp1){ if(exp1){
if(exp2) if(exp2)
stmta stmta
else }
stmtb else
} stmtb
×
While implementing a code, it is good practice
to provide explicit { and } to avoid any confusion
IIT Patna 13
More examples
if e1 s1
else if e2 s2

IIT Patna 14
More examples
if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2}

IIT Patna 14
More examples
if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2}

if e1 s1
else if e2 s2
else s3

IIT Patna 14
More examples
if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2}

if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2 else s3}
else s3

IIT Patna 14
More examples
if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2}

if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2 else s3}
else s3

if e1 if e2 s1
else s2
else s3

IIT Patna 14
More examples
if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2}

if e1 s1 if e1 s1
else if e2 s2 else {if e2 s2 else s3}
else s3

if e1 if e2 s1 if e1 {if e2 s1 else s2}


else s2 else s3
else s3

IIT Patna 14
Example
• Print PASSED if a given marks is between 60 and 100, or FAILED if it is below
60. Do not print anything in other cases
int main(){
int m;
scanf("%d", &m);
if(m >= 60)
if(m <= 100)
printf("PASSED\n");
else
printf("FAILED\n");
return 0;
}
IIT Patna 15
Example
• Print PASSED if a given marks is between 60 and 100, or FAILED if it is below
60. Do not print anything in other cases
int main(){
int m;
Output:
scanf("%d", &m); 150
if(m >= 60) FAILED
if(m <= 100)
printf("PASSED\n"); Output:
else 30
printf("FAILED\n");
return 0;
}
IIT Patna 15
Example
• Print PASSED if a given marks is between 60 and 100, or FAILED if it is below
60. Do not print anything in other cases
int main(){
int m;
scanf("%d", &m); Output:
if(m >= 60){ 150
if(m <= 100)
printf("PASSED\n"); Output:
} else 30
FAILED
printf("FAILED\n");
return 0;
}
IIT Patna 16
Conditional operator ?:
• Conditional expression written with the ternary operator ?: provides alter-
nate way to write if-else statement
• It is of the form — expr1 ? expr2 : expr3 ;
• The expression expr1 is evaluated first. If it is non-zero, then expr2 is eval-
uated. Otherwise expr3 will be evaluated
• Example: z = (a > b) ? a : b ;
if (a > b)
z = a;
else
z = b;
• Conditional expression can be represented in succinct way ?: operator
printf("%c", (i%10==9 || i==n-1) ? '\n' : ' ');
IIT Patna 17
Equality (==) vs Assignment (=) operators
• It can lead to fatal error and does not cause syntax error in general
• Any expression that produces a value can be used for control structure
• Non-zero values are treated as true and zero as false
• Example:
if(age = 18)
printf("You can cast vote!");
• printf statement will always get executed!!
• if (18 = age) — leads to syntax error, this can be good practice

IIT Patna 18
The switch statement
• The switch statement is a multi-way decision that tests whether an expres-
sion matches one of a number of constant integer values and branches accord-
ingly
switch (expr){ • expr is any integer valued expression
case cexpr1: S1 • cexpr1, cexpr2, … are constant integer
case cexpr2: S2 valued expression and values must be distinct
.
.
. • S1,S2,…, S are statements or compound
default: S statements
} • default is optional and can come anywhere
not necessary at the end. Usually it is put at
the end

IIT Patna 19
The switch statement
switch (expr){
case cexpr1: S1 • When a switch statement is executed, the
case cexpr2: S2 expr is evaluated and control is transferred to
.
. the group of case statements
.
• It is then compared with cexpr1, cexpr2,
default: S
… for equality in order
} • If there is a match, all statements from that
point till the end of switch is executed (includ-
ing default)
• Need to use break statement to transfer con-
trol to the end
• Statements for default gets executed if
none of the expression is matched
IIT Patna 20
Example
switch (choice){
case 'r':
printf("RED");
break;
case 'g': Output: (choice = ’r’)
printf("GREEN"); RED
break; Output: (choice = ’G’)
default: OTHER
printf("OTHER");
}

IIT Patna 21
Example
switch (choice){ Since no break statement is there, the control
case 'r': passes to the next statement without checking
case 'R': the next condition.
printf("RED");
break; Output: (choice = ’r’)
case 'g': RED
case 'G':
printf("GREEN"); Output: (choice = ’G’)
GREEN
break;
default: Output: (choice = ’z’)
printf("OTHER"); OTHER
}
IIT Patna 22
Alternative way
switch (toupper(choice)){
case 'R':
printf("RED");
break;
case 'G': Output: (choice = ’r’)
printf("GREEN"); RED
break; Output: (choice = ’G’)
default: GREEN
printf("OTHER");
} Output: (choice = ’z’)
OTHER

IIT Patna 23
The break statement
• Used to exit from a switch or terminate from a loop
• With respect to switch, the break statement causes a transfer of control out
of the entire switch statement, to the first statement following the switch
statement
• Can be used with other statements also

IIT Patna 24
Example
int x;
scanf("%d",&x);
switch(x){
case 1: printf("One \n");
case 2: printf("Two \n");
default: printf("Not one or two \n");
}

• Let entered value is 1, output will be

IIT Patna 25
Example
int x;
scanf("%d",&x);
switch(x){
case 1: printf("One \n");
case 2: printf("Two \n");
default: printf("Not one or two \n");
}

• Let entered value is 1, output will be


One
Two
Not one or two
IIT Patna 25
Example: Find 2nd max of 3 numbers
int main(){
int x, y, z, fmax, smax;
scanf("%d%d%d",&x,&y,&z);
if(x > y) { fmax = x; smax = y;}
else { fmax = y; smax = x;}
if(z > fmax) { smax = fmax;}
else if(z > smax) { smax = z;}
else { }
printf("Second max = %d\n", smax);
return 0;
}

IIT Patna 26
Practice problems
• Read in 4 integers and print the second maximum number
• Read in the coefficient a, b, c of the expression ax2 + bx + c = 0. Print the
roots of the equation nicely (For imaginary roots, use x + iy format)
• Read in 3 points on a 2D plane and check if they are collinear. Print suitable
message
• Read in a number (integer), convert it into grade. Marks >= 90 is AA, 80-89 —
AB, 70-79 — BB, 60-69 — BC, 50-59 —CC, 40-49 — CD, 30-39 — DD and marks
< 30 will get F

IIT Patna 27

You might also like