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

Cs112 - Programming Fundamental: Lecture # 12 - Selection Control in C Syed Shahrooz Shamim

This document discusses selection control in C programming using if/else statements. It provides examples of basic if/else syntax to conditionally execute code, if-then statements without else blocks, multi-way if-else statements, nested if statements, and finding the minimum of three numbers using nested if statements. The document is a lecture on selection control from a CS112 programming fundamentals course taught by Syed Shahrooz Shamim at UIT.

Uploaded by

Ghazan Aqeel
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)
26 views

Cs112 - Programming Fundamental: Lecture # 12 - Selection Control in C Syed Shahrooz Shamim

This document discusses selection control in C programming using if/else statements. It provides examples of basic if/else syntax to conditionally execute code, if-then statements without else blocks, multi-way if-else statements, nested if statements, and finding the minimum of three numbers using nested if statements. The document is a lecture on selection control from a CS112 programming fundamentals course taught by Syed Shahrooz Shamim at UIT.

Uploaded by

Ghazan Aqeel
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/ 13

CS112 - PROGRAMMING FUNDAMENTAL

Lecture # 12 – Selection Control in C


Syed Shahrooz Shamim
Junior Lecturer,
CS Department, UIT
C if else
Introduction:

The if statement is used to conditionally execute a


statement or a block of statements. Conditions can be
true or false, execute one thing when the condition is
true, something else when the condition is false.
C if else
A FLOWCHART OF IF-ELSE STATEMENT
C if else
Syntax:

if (expression)
statement(s);
else statement(s);
C if else
Example :

if (a == b)
printf ("%d is equal to %d", a, b);
else
printf ("%d is not equal to %d", a, b);
If-then statements

Syntax:

if (expression)
statement(s);
If-then statements

Example:

if (a == b)
printf ("%d is equal to %d", a, b);
else
printf ("%d is not equal to %d", a, b);

Note : There is no indentation rule in writing C programming, we


can write the above code in following ways :
If-then statements

Example:

if (a == b)
printf ("if a is equal to b");
printf ("%d is equal to %d", a, b);

if (a == b)
{
printf ("if a is equal to b");
printf ("%d is equal to %d", a, b);
}

Note : The second way of writing code is a good practice.


A complete example on conditional if-else
statement:
Example:
#include<stdio.h>
main()
{
int num;
printf("Input a number : ");
scanf("%d",&num);
if(num>0)
{
printf("This is a positive integer\n");
}
else // else portion of if statement
{
printf(" This is not a positive integer..Try again\n"
);
}
}
Sequential if-then statements
Example:

if (a == b)
printf ("a = b");
if (a == c)
printf ("a = c");
if (b == c)
printf ("b = c")
Multi-way if-else-Statement
Syntax :

if (expression_1)
statement_1
else if (expression_2)
statement_2
.
.
.
else if (expression_n)
statement_n
else
other_statement
Multi-way if-else-Statement
#include<stdio.h>
main() else if(num>=50 && num<60)
{ {
int num; printf("Grade : Average");
printf("Input score :");
scanf("%d",&num); }
if (num>=90) else if(num>=40 && num<50)
{ {
printf("Grade : Excellent"); printf("Grade : Poor");
}
} else
else if(num>=80 && num<90) {
{ printf("Grade : Not Promote
printf("Grade : Very Good"); d");
}
} }
else if(num>=60 && num<80)
{
printf("Grade : Good");
}
Nested if-then-else statements
#include<stdio.h>
main()
{
int num1=5, num2=3, num3=-12, min;
if(num1<num2)
{
if(num1<num3)
min = num1;
else
min = num3;
}
else
{
if(num2<num3)
min = num2;
else
min = num3;
}
printf("Among %d, %d, %d minimum number is %d",num1,num2,num3,
min);
}

You might also like