0% found this document useful (0 votes)
49 views79 pages

CSE - 109 Lec 67 Flow Control Statement

The document discusses different types of flow control statements in C including if, if-else, and sequential if statements. It provides syntax examples and sample code to demonstrate how each statement works. The if statement executes code conditionally based on the expression evaluating to true. The if-else statement provides two paths - one for true and one for false. Sequential if statements allow evaluating multiple conditions and executing code for each true condition.

Uploaded by

anisruhan6
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)
49 views79 pages

CSE - 109 Lec 67 Flow Control Statement

The document discusses different types of flow control statements in C including if, if-else, and sequential if statements. It provides syntax examples and sample code to demonstrate how each statement works. The if statement executes code conditionally based on the expression evaluating to true. The if-else statement provides two paths - one for true and one for false. Sequential if statements allow evaluating multiple conditions and executing code for each true condition.

Uploaded by

anisruhan6
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/ 79

FLOW CONTROL STATMENTS

PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
INTRODUCTION
 Take a user input a scanf(“%d”, &a);
 Print Hello World in the screen if a < 5? You are stuck now! 
Single If in C
 The if statement is one of C's selection statements (sometimes called conditional statements).
Its operation is governed by the outcome of a conditional test that evaluates to either true or
false.

 In its simplest form, the if statement allows your program in conditionally execute a statement.
 Syntax: if(expression) statement;

 If the expression evaluates as true, the statement will be executed. If it does not the statement is
bypassed, and the line of code following the if is executed.

 If the expression evaluates to zero, it is false, otherwise it is true.


Single If Statement

if (expression)
{
statement;
…………
statement;
}

Single if Syntax
Understanding If Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 1;

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 1; a = 1 < 5? YES

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 1 < 5?

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 1

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Hello World
3 int a = 1; a = 2 Good Bye!

Process returned 0 (0x0) execution time : 0.075 s


Press any key to continue.

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Understanding Invalid If Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 6;

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 6; a = 6 < 5? No

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
Sample Code
1 #include <stdio.h>
2 int main(){
Good Bye!
3 int a = 6; a = 6 Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.

4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }

8 printf(“Good Bye!”);

9 }
If Example
Write a C program to check whether the summation of two numbers are 24 or
not?

#include <stdio.h>
int main(){
int a, b, answer;
printf(“Is the summation 24?");
scanf("%d %d", &a, &b);
answer = a + b;
if (answer == 24) printf("Right!");
return 0;
}
If-else in C
 You can add an else statement to the if. When this is done, the if statement looks like this:

if (expression) statement 1;
else statement2;

 If the expression is true, then the target of the if will execute, and the else portion will be skipped.
However, if the expression is false, then the target of the if is bypassed, and the target of the else
will execute.

 Under no circumstances will both statements execute. Thus, the addition of the else provides a
two-way decision path.
If-else Statement
if (expression)
{
statement;
…………
statement;
}
else
{
statement;
…………
statement;
}
If and else Syntax
Understanding If-else Statement
Sample Code
1 #include <stdio.h>
2 int main(){
3 int a = 5;
4 if ( a < 5 ){
5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 else {
9 printf(“Bye World\n”);
10 a = a + 1;
11 }
12 printf(“Good Bye!”);
13 }
Sample Code
1 #include <stdio.h>
2 int main(){ a = 1 < 5? YES
Hello World
3 int a = 1; Good Bye!
Process returned 0 (0x0) execution time : 0.075 s
4 if ( a < 5 ){ Press any key to continue.

5 printf(“Hello World\n”);
6 a = a + 1;
7 }
8 else {
9 printf(“Bye World\n”);
10 a = a + 1;
11 }
12 printf(“Good Bye!”);
13 }
If-else Example
Write a C program to Divide 2 numbers. Remember, if the divisor if 0, then the
output might become infinity.
#include <stdio.h>
int main()
{
int numl, num2;
printf("Enter first number:");
scanf("%d“, &num1);
printf("Enter second number:" );
scanf("%d", &num2);
if(num2 == 0) printf("Can not divide by zero!");
else printf("Answer is: %d", num1, num2);
return 0;
}
Sequence of If’s in C
 In C programming, sequential if statements allow you to evaluate a series of conditions one after
the other. The conditions are checked in the order they appear, and as soon as a true condition is
found, the corresponding block of code is executed.
if (condition1) {
// Code to execute if condition1 is true
}
if (condition2) {
// Code to execute if condition2 is true
}
// Continue with additional if statements as needed...
 Unlike if-else statements, where only one block of code is executed, with sequential if statements,
multiple blocks of code can be executed if multiple conditions are true.
 Since all conditions are checked, even if an earlier one evaluates to true, there may be a slight performance
overhead
Sequence of If’s Statement
if (expression)
{
statements;
}
if(expression)
{
statements;
}
......................
if(expression)
{
statements;
}
Understanding If Ladder Statement
Sample Code
#include <stdio.h>
int main(){
int a = 1;
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1 < 3? YES
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2 < 3? YES
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3 < 3? NO
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 3
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
Hello World
a = a + 1; Good Bye!
} Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Write a C program to calculate average
of five numbers and count the numbers
that are above average.
Solve using if ladder
#include <stdio.h>
int main()
{ if(d>=avg)
int a,b,c,d,e, sum, count=0; {
float avg; count++;
printf("\nEnter the value of a,b,c,d,e :\n "); }
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); if(e>=avg)
sum = a+b+c+d+e; {
avg = (float)sum/5; count++;
printf("average: %.2f\n",avg); }
if(a>=avg) printf("numbers above average %d",count);
{
count++; return 0;
} }
if(b>=avg)
{
count++;
}
if(c>=avg)
{
count++;
}
IF-Else if-Else in C
 The conditions are evaluated in the order they appear. The first if statement is checked first, then
the second, and so on.

 Once a true condition is found, the corresponding block of code is executed, and the rest of the
conditions are skipped.

 else Block: The else block is optional. If none of the conditions are true, the code within the else
block (if present) is executed.

 Limited to a Single Choice: It's designed for situations where you have one choice among many. If
you need multiple choices to be executed, you might need separate if statements or a switch
statement.
IF-Else if-Else in C
if (condition1) {
// Code to execute if condition1 is true
}
else if (condition2) {
// Code to execute if condition2 is true and condition1 is false
}
else if (condition3) {
// Code to execute if condition3 is true and conditions 1 and 2 are false
}
//...
else {
// Code to execute if none of the conditions are true
}
If-Else if-else Ladder Statement
if (expression)
{
statements;
}
else if(expression)
{
statements;
}
......................
else
{
statements;
}
Understanding If-Else if-else Ladder
Statement
Sample Code
#include <stdio.h>
int main(){
int a = 1;
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1 < 3? YES
if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 1
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”);
a = a + 1;
}
else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){
int a = 1; a = 2
if ( a < 3 ){
Hello World
printf(“Hello World\n”); Good Bye!
a = a + 1; Process returned 0 (0x0) execution time : 0.075 s
} Press any key to continue.

else if ( a < 3 ){
printf(“Hello World\n”);
a = a + 1;
}
else {
printf(“Hello World\n”);
a = a + 1;
}
printf(“Good Bye!”);
}
If-Else if-Else Ladder Example
Write a C program to check whether a number is positive, negative or zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num > 0) {
printf("The number is positive\n");
}
else if (num < 0) {
printf("The number is negative\n");
}
else {
printf("The number is zero\n");
}
return 0;
}
Understanding Nested If Else
Statement
Nested If Else Statement
Nested If Else Statement

if (expression)
{
if (expression)
{
statement;
…………
statement;

}
}
Sample Code
#include <stdio.h>
int main(){
int a = 5;
if ( a > 3 ){
printf(“a is greater than 3\n”);
if ( a < 7 ){
printf(“ and less than 7\n”);
}
else {
printf(“but not less than 7\n”);
}
}
else {
printf(“a is less than 3\n”);
}
printf(“Good Bye!”);
}
Sample Code
#include <stdio.h>
int main(){ a = 5 >
< 3?
7? YES
int a = 5;
if ( a > 3 ){
printf(“a is greater than 3\n”); a is greater than 3
and less than 7
if ( a < 7 ){ Good Bye!
printf(“ and less than 7\n”); Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
}
else {
printf(“but not less than 7\n”);
}
}
else {
printf(“a is less than 3\n”);
}
printf(“Good Bye!”);
}
When to use which Control Statement
Type When to Use
A single If Just need to check one condition

Sequence of only “If”s Need to check multiple condition one after another
(usually on same vars, might be true for multiple
cases)
If and else Only two conditions, only one of them might be true.
Second condition doesn’t even need to be checked.

If- else-if- else Ladder Only one condition out of multiple may be true.
Check them one after another. The last or default
condition is not checked and written with an “else”
Switch Statement
 Multi-branch Decision-Making: When you have a variable with multiple possible values and each
value requires a different set of actions, a switch statement provides a clean and efficient way to
handle this.

 In some cases, switch statements can be more efficient than a series of if-else statements,
especially when dealing with a large number of cases.

 However, it's worth noting that switch statements have certain limitations. For example, they only
work with integral types (like int, char, etc.) and not with floating-point types. Additionally, case
labels must be constants known at compile time.
Switch Statement
switch (expression)
​{
case constant 1:
// statements
break;
case constant 2:
// statements
break;
...
default: // default statements
}
Switch Statement
Switch Statement
Week Day
Day No Week
Write a C program to input day of week number(1-7) and 1 Monday
print day of week name using switch case.
Input: Input day of week number(1-7): 2 2 Tuesday
Output: Tuesday
3 Wednesday

4 Friday

5 Saturday

6 Sunday

7 Saturday

Any other Input Wrong Input!


Switch Statement
#include <stdio.h>
int main(){
int dayOfWeek;
scanf(“%d”, &dayOfWeek);
switch(dayOfWeek) {
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
// ...
default:
printf("Invalid day");
}
}
Conditional/Ternary Operator Statement
C Conditional Operator

The conditional operator, often denoted as ? :, is a ternary operator in C and many


other programming languages. It provides a concise way to express a conditional
statement. It evaluates a condition and returns one of two values based on whether
the condition is true or false.

Syntax:
condition ? expression_if_true : expression_if_false;
Conditional/Ternary Operator Statement
Conditional/Ternary Operator Statement

#include <stdio.h>

int main() {
int num = 10;
int result;

// Using the conditional operator to assign a value to 'result'


result = (num > 5) ? 100 : 200;

printf("The result is: %d\n", result);

return 0;
}
Write a C program to find maximum
between two numbers.
Step by Step Logic
 Below is step by step descriptive logic to find maximum.

 Input two numbers from user. Store it in some variable say num1 and num2.

 Check if(num1 > num2) then print num1 is maximum.

 Check if(num2 > num1) then print num2 is maximum.

 Check if(num1 == num2) then both the numbers are equal.


#include <stdio.h>
int main()
{ Solve using if ladder
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
if(num1 > num2)
{
printf("%d is maximum", num1);
}
if(num2 > num1)
{
printf("%d is maximum", num2);
}
if(num1 == num2)
{
printf("Both are equal");
}
return 0;
}
#include <stdio.h>
int main()
{ Solve using if else
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
/* Compare num1 with num2 */
if(num1 > num2)
{
/* True part means num1 > num2 */
printf("%d is maximum", num1);
}
else
{
/* False part means num1 < num2 */
printf("%d is maximum", num2);
}
return 0;
}
Logical Operators - Combine Conditions

Symbol Operator Name (Higher to


Lower Precedence)
! NOT
&& AND
|| OR
Logical Operators - Combine Conditions
AND && OR ||
Value 1 Value 2 Result Value 1 Value 2 Result
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1

NOT!
Value 1 Result
0 1
1 0
Logical Operators - Combine Conditions
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
Write a C program to find maximum
between three numbers.
Step by Step Logic

 Below is step by step descriptive logic to find maximum between three numbers:

 Input three numbers from user. Store it in some variable say num1, num2 and num3.

 Compare num1 with num2 and num3. If the statement is true then num1 is surely the max
value.

 If not then it means num1 is smaller than num2 and num3. So, Compare num2 with num3.
If the statement is true then num2 is surely the max value.

 Otherwise, num3 is surely the max value.


#include <stdio.h> num1 = 45
num2 13 > num2 = 23
num3 45
int main()
{ YES
NO
int num1=13; >
int num2=45; max = 45 num3 = 23
num1 = 13
int num3=23, max; NO
if((num1 > num2) && (num1 > num3))
{
max = num1; Maximum = 45
} Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
else if(num2 > num3)
{
max = num2;
}
else
{
max = num3;
}
printf("Maximum = %d", max);
return 0;
}
#include <stdio.h>
int main()
{ Solve using if-else
int num1, num2, num3, max; ladder
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if((num1 > num2) && (num1 > num3))
{
max = num1;
}
else if(num2 > num3)
{
max = num2;
}
else
{
max = num3;
}
printf("Maximum among all three numbers = %d", max);
return 0;
}
Write a C program to check whether a
year is leap year or not.
Step by Step Logic
 Wikipedia states leap year as a special year containing one extra day i.e.
total 366 days in a year.

 A year is said to be leap year, if the year is exactly

 divisible by 4 but and not divisible by 100.

 Year is also a leap year if it is exactly divisible by 400.


#include <stdio.h> Solve using if-else
int main()
{
int year;
printf("Enter year : ");
scanf("%d", &year);
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("%d is a LEAP YEAR",year);
}
else
{
printf("%d is a COMMON YEAR",year);
}
return 0;
}
Write a C program to input any
character and check whether it is
alphabet, digit or special character.
#include <stdio.h>
int main() Solve using if-else
{ ladder
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
return 0;
}
Write a C program to find maximum
between five numbers.
Solve using if-else
ladder
#include <stdio.h>
int main()
{ else if(c>d && c>e)
int a,b,c,d,e; {
printf("c is maximum\n");
printf("\nEnter the value of a,b,c,d,e :\n "); }
else if(d>e)
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); {
printf("d is maximum\n");
if(a>b && a>c && a>d && a>e) }
{ else
printf("a is maximum\n"); {
} printf("e is maximum\n");
else if(b>c && b>d && b>e) }
{ return 0;
printf("b is maximum\n"); }
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy