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

If-Else Introduction Week 1

The document discusses if-else statements in C programming. It explains that if-else statements allow executing one block of code or another depending on the evaluation of a conditional expression. Specifically: 1. An if-else statement checks a conditional expression and executes the code in the if block if it's true, or the else block if it's false. 2. Braces {} are used to group statements into blocks so they are treated as a single statement syntactically. 3. An example is provided to print the minimum of two integers using an if-else statement to check which is smaller.

Uploaded by

Rajesh Choudhary
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)
26 views16 pages

If-Else Introduction Week 1

The document discusses if-else statements in C programming. It explains that if-else statements allow executing one block of code or another depending on the evaluation of a conditional expression. Specifically: 1. An if-else statement checks a conditional expression and executes the code in the if block if it's true, or the else block if it's false. 2. Braces {} are used to group statements into blocks so they are treated as a single statement syntactically. 3. An example is provided to print the minimum of two integers using an if-else statement to check which is smaller.

Uploaded by

Rajesh Choudhary
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

If-else Introduction

Week 1
Statements and Blocks
¢An expression such as x=0 or printf(…) becomes a
statement when it is followed by a semi-colon, as in
x = 0;
printf( … );

¢ Braces { and } are used to group variable type declarations


and statements together into a compound statement, or a
block so that it is syntactically equivalent to a single
statement.
{
int x; float y; /* 2 statements */
x = 10; A single block
printf(“x = %d\n”,x);
}
Statements and Blocks
¢An expression such as x=0 or printf(…) becomes a
statement when it is followed by a semi-colon, as in
x = 0;
printf( … );

¢ Braces { and } are used to group variable type declarations


and statements together into a compound statement, or a
block so that it is syntactically equivalent to a single
statement.
{
int x; float y; /* 2 statements */
x = 10; A single block
printf(“x = %d\n”,x);
}
If-Else statement (example)
¢ Read two integers and print their minimum.

# include <stdio.h>
main() {
int x; int y;
scanf( “%d%d”, &x, &y );
if (x < y) 1. Checks if x is less
printf(“%d”,x); than y.
else 2. If so, print x
printf(“%d”, y); 3. Otherwise, print y.
}

¢ (x < y) is an expression. It evaluates to 0 or 1.


¢ A value of 0 is considered FALSE in C, non-zero
values are considered TRUE.
Tracing execution of an if-else statement
Tracing execution of an if-else statement
# include <stdio.h>
main() {
int x; int y;
scanf(“%d%d”, &x,&y);
if (x < y) {
printf(“%d\n”,x);
}else {
printf(“%d\n”,y);
}
}
Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y);
if (x < y) {
printf(“%d\n”,x);
}else {
printf(“%d\n”,y);
}
}
Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x);
}else {
printf(“%d\n”,y);
x y
}
}
Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x);
}else {
printf(“%d\n”,y);
x y
}
}
6 10
Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x);
}else {
printf(“%d\n”,y);
x y
}
}
6 10

6 < 10 so the if-branch is taken


Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x); 6
}else { $
printf(“%d\n”,y);
x y
}
}
6 10

6 < 10 so the if-branch is taken


Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x); 6
}else { $
printf(“%d\n”,y);
x y
}
}
6 10

6 < 10 so the if-branch is taken


Tracing execution of an if-else statement
Run the program
# include <stdio.h>
main() {
int x; int y; $./a.out
scanf(“%d%d”, &x,&y); 6 10 <enter>
if (x < y) {
printf(“%d\n”,x); 6
}else { $
printf(“%d\n”,y);
x y
}
}
6 10

6 < 10 so the if-branch is taken


If-else statement (general form)
¢ General form of the if-else statement
if (expression)
statement1
else if (expression)
statement2
else
statement3

¢ Execution of if-else statement is as follows:


1. First the expression is evaluated.
2. If it evaluates to a non-zero value, then statement1 is
executed and then control moves to the statement after
statement3.
3. If expression evaluates to 0, then statement2/3 is
executed based on the conditions and then control moves to
the statement after statement3.
if statement
¢ There is an if statement also. It has no else part.

if (expression)
statement1
statement2

¢ If expression evaluates to TRUE then statement1 is


executed and then statement 2 is executed.
¢ If expression evaluates to FALSE then only statement2 is
executed.
¢ Statement1 and Statement2 can be statement blocks and can
have if or if-else statements inside them.
Acknowledgments: This lecture slide is based on the material prepared by Prof.
Sumit Ganguly, CSE, IIT Kanpur. The slide design is based on a template by Prof.
Krithika Venkataramani.

“The instructor of this course owns the copyright of all the course materials. This lecture
material was distributed only to the students attending the course ESC-101 of IIT
Kanpur and should not be distributed in print or through electronic media without the
consent of the instructor. Students can make their own copies of the course materials
for their use.”

You might also like