L10-L13-Decision Making, Branching and Looping
L10-L13-Decision Making, Branching and Looping
• The if Statement
• The if-else Statement
• Nested if Statements
• The else if Ladder
• The switch Statement
Sequential (Serial)
In a Sequential approach, all the statements are executed in the same
order as it is written.
Iterational (Repetition)
In an Iterational approach certain statements are executed repeatedly.
1. if statement
2. switch statement
2. if…else statement.
4. else if ladder.
if (test Expression)
{ If expression is true
statement-block; (non-zero), executes
} statement.
next_statement;It gives you the choice
of executing
statement or skipping
it.
int main ()
{
int number;
printf(“Type in your number: “);
scanf(“%d”,&number);
if ( number < 0 )
number = -number;
printf(“The absolute value is”);
printf(“%d”,number);
return 0;
}
if (test expression )
{
statement _block1 if-else
} statement:
else enables you to
{ choose between
statement _block2 two statements
}
Next_statement
Example: if(job_code == 1)
rate = 7.00;
else
rate = 10.00;
prinf(“%d”,rate);
11/06/2023 CSE 1001 Department of CSE 13
The if-else statement
yes no
expression
Next _statement
#include<stdio.h>
int main()
{
int a, b;
printf(“Enter 2 numbers\n”);
scanf(“%d %d”,&a,&b);
if(a > b)
a;
printf(“Large is %d\t“,a);
else
else
cout<<"large is "<<fb;
printf(“Large is %d\t“,b);
return 0;
}
Syntactically OK (void
statement on if) but a
semantic error !
if ( x == 0 );
printf(“The number is zero.\n“);
Because the order of evaluation for the <= operator is left-to-right, the
test expression is interpreted as follows:
(5<= x) <= 10
The subexpression 5 <= x either has the value 1 (for true) or 0 (for
false). Either value is less than 10, so the whole expression is always
true, regardless of x !
if (number > 5)
if (number < 10)
printf(“1111\n“);
else printf(“2222\n“);
Rule: an else
goes with the
if (number > 5) {
most recent if,
if (number < 10)
unless braces
printf(“1111\n“);
indicate
}
otherwise
else printf(“2222\n“);
Next_statement
11/06/2023 CSE 1001 Department of CSE 26
else if ladder -Explanation
expression_1 is first evaluated. If it is TRUE, statement_1 is
executed and the whole statement terminated and the
next_statement is executed.
On the other hand, if expression_1 is FALSE, control passes to the
else if part and expression_2 is evaluated.
If it is TRUE, statement_2 is executed and the whole system is
terminated.
If it is False, other else if parts (if any) are tested in a similar way.
Finally, if expression_n is True, statement_n is executed; if not,
last_statement is executed.
Note that only one of the statements will be executed others will be
skipped.
The statement_n’s could also be a block of statement and must be
put in curly braces.
True False
Condition-1
True False
statement-1 Condition-2
True False
statement-2 Condition-3
True False
statement-3
Condition-n
statement-n
default statement
next statement
}
Example: else-if
// Program to implement the sign function
#include <stdio.h>
int main ( )
{
int number, sign;
printf("Please type in a number: “);
scanf(“%d”,&number);
if ( number < 0 )
sign = -1;
else if ( number == 0 )
sign = 0;
else // Must be positive
sign = 1;
printf(“Sign = %d“,sign);
return 0;
}
11/06/2023 CSE 1001 Department of CSE 31
Example – multiple choices
/* Program to evaluate simple expressions of the form number operator number */
#include <stdio.h>
int main ( )
{
float value1, value2,result;
char operator;
printf("Type in your expression.\n“);
scanf(“%f %c %f”, &value1,&operator,&value2);
if ( operator == '+' )
{result=value1+value2;
printf(“%f”,result);}
else if ( operator == '-' )
{result=value1-value2;
printf(“%f”,result);}
else if ( operator == '*' )
{result=value1*value2;
printf(“%f”,result);}
else if ( operator == '/' )
{result=value1/value2;
printf(“%f”,result);}
else
printf("Unknown operator.\n“);
11/06/2023
return 0; CSE 1001 Department of CSE 32
}
Problem…
else if (disc==0)
#include<stdio.h> {
int main() printf(“Real & equal roots“);
{ re=-b / (2*a);
float a,b,c,root1,root2,re,im, disc;
scanf(“%f %f %f”,&a,&b,&c); printf(“Root1 and root2 are
disc=b*b-4*a*c; %.21f”,re);
}
if (disc<0)
{ else /*disc > 0 */
printf("imaginary roots\n“); {
re= - b / (2*a); printf(“Real & distinct roots“);
im = pow(fabs(disc),0.5)/(2*a); printf(“Roots are“);
printf(“root1=%.21f+%.21fi and
root2 =%.21f-%.2fi”, re,im,re,im); root1=(-b + sqrt(disc))/(2*a);
} root2=(-b - sqrt(disc))/(2*a);
printf(“Root1 = %.21f and root2 =
%.21f”,root1,root2);
}
return 0;
}
11/06/2023 CSE 1001 Department of CSE 34
The switch Statement
case 50:
switch (mark)
grade=‘C’
{
break;
case 100: case 40:
case 90: grade=‘D’
case 80: grade=‘A’; break;
break;
default: grade=‘F’;
case 70: break;
case 60: }
scanf(“%c”,&grade);
grade=‘B’;
break;
switch(ch)
{
case ‘a’ : printf(“Vowel”);
break;
case ‘e’ : printf(“Vowel”);
break;
case ‘i’ : printf(“Vowel”);
break;
case ‘o’ : printf(“Vowel”);
break;
case ‘u’ : printf(“Vowel”);
break;
default: printf(“Not a Vowel”); }
switch(ch)
{
case ‘a’
case ‘e’
case ‘i’ :
case ‘o’ :
case ‘u’ : printf(“Vowel”);
break;
default: printf(“Not a Vowel”);
}
int iNum = 2;
switch(iNum)
{
case 1:
printf(“ONE”);
break;
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”);
break;
default:
printf(“INVALID”);
break;
}
iNum = 2;
switch(iNum)
{
default:
printf(“INVALID”);
case 1:
printf(“ONE”);
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”;)
}
switch (iDepartmentCode)
{
case 110 : printf(“HRD ”);
case 115 : printf(“IVS ”);
case 125 : printf(“E&R ”);
case 135 : printf(“CCD ”);
} IVS E&R CCD
if(disc<0) d=1;
if(disc==0) d=2;
if(disc>0) d=3;
switch(d)
{
case 1:
printf("imaginary roots\n“);
re= - b / (2*a);
im = pow(fabs(disc),0.5)/(2*a);
printf(“root1=%.21f+%.21fi and root2 =%.21f-%.2fi”, re,im,re,im);
break;
L12-L13
O b j e c ti v e s
• To learn and appreciate the following concepts
printf(hello !\n”);
printf(hello !\n”)
printf(hello !\n”)
…
Repeat 100 times
printf(hello !\n”)
printf(hello !\n”)
printf(hello !\n”)
1) Entry controlled loop: control is tested before the start of the loop. If
false, body will not be executed.
2) Exit controlled loop: test is performed at the end of the body. i.e. body
of loop executed at least once.
Test False
Body of
Condition
The loop
True
Body of
True
The loop Test
Condition
False
init_expression n=1
no
loop_condition n<=200
yes
triangularNumber =
Statement(s) triangularNumber + n
loop_expression n=n+1
while ( expression )
program statement Loop with the
test in the
beginning !
statement before loop Body might
never be
executed !
3 1 Loop_expression 4
No
yes
2 statement (s)
Next statement
while ( number != 0 )
{
right_digit = number % 10;
rev=rev*10 + right_digit;
number = number / 10;
}
printf(“The reversed number is %d“, rev);
return 0;
}
11/06/2023 CSE 1001 Department of CSE 65
The do – while statement
General form:
do
{
body of the loop
}
while(test condition);
Exit controlled loop. At the end of the loop, the test condition is evaluated.
After do statement, program executes the body of the Loop.
Then, the condition is tested, if it is true, body of the loop is executed once again
& this process continues as long as the condition is true.
Body of the loop is executed at least once.
do-while loop can be nested.
yes
loop_expression
2
No
Next statement 4
#include <stdio.h>
int main()
{
int number, rev=0, right_digit;
do
{
right_digit = number % 10;
rev=rev*10 + right_digit;
number = number / 10;
}
while ( number != 0 );
1 init_expression
no
5 2 loop_condition
yes
3 Program statement
4 Loop expression
Next Statement
no
1 2 5 4
for ( n = 1; nyes
<= 200; n = n + 1 );
{ 3 sum = sum + n; }
Next Statement
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}
#include <stdio.h>
int main()
{
int n;
int sum;
sum=0; //initialize sum
#include <stdio.h>
int main()
{
int n, triangularNumber=0;
scanf(“%d %d”,&n,&k);
Enter n & k values: 3 5
The table for 3 X 5 is
for (i=1; i<=k; i++) 1 * 1= 1 2 * 1= 2 3 * 1= 3
{ 1 * 2= 2 2 * 2= 4 3 * 2= 6
1 * 3= 3 2 * 3= 6 3 * 3= 9
for (j=1; j<=n; j++) 1 * 4= 4 2 * 4= 8 3 * 4= 12
{ 1 * 5= 5 2 * 5= 10 3 * 5= 15
prod = i * j;
printf(“%d * %d = %d\t”, j,i,prod);
}
printf(“\n”);
}
• Declaring variables
for(int i=0 ; i=10 ; i++ )
• You can actually rewrite any while as a for and vice versa !
while (……….) do
{……. {…….
………… …………
If(condition) If(condition)
break; break;
……… ………
Exit Exit
From ………. From ……….
loop loop
} // end of while } while(…);
………… //next ………….. // next
statement statement
for (……….)
for {…….
{……. for(……..)
………… { ………
If(condition)
If(condition)
break;
break;
… stmts of inner loop;
Exit
……… } // inner for loop ends
From
loop ………. Exit ….stmts of outer loop;
From
} inner } // outer for loop ends
……next Stmts; loop …… next Stmts;
int j, prime=1;
scanf(“%d”,&N);
for( int j=2; j<N; j++ )
{
if( (N % j) == 0)
{
prime=0;
break; /* break out of for loop */
}
}
if (prime == 1)
printf(“%d is a prime no”,N);
else
printf(“%d is a not a prime no”,N);
scanf(“%d %d”,&m,&n);
As the name implies, continue causes the loop to be continued with next iteration,
after skipping rest of the body of the loop.
while (……….) do
{ {
Statement-1; Statement-1;
Statement-2; Statement-2;
If(condition) If(condition)
continue; continue;
Statement-3; Statement-3;
Statement-4; Statement-4;
} } while(…);
Next_statement Next_statement
printf(“%d\t”,i);
}
enum
Enumerated data type - a type with restricted set of values.
case Thursday: }
printf(“ Thursday“);
break;
return 0;
}
#include<stdio.h>
Algorithm : Fibonocci Series
int main()
Step 1 : Input Limit {
Step 2: First0,Second1 int first=0, second=1;
Step 3: print First int limit, next;
Step 4: WHILE Second < Limit
begin scanf(“%d”,&limit);
Print Second printf(“%d”,first);
Next First +
Second while(second < limit)
FirstSecond {
Second printf(“%d”,second);
Next next = first + second;
first = second;
end
second = next;
Step 5:[End of Algorithm] }
Stop return 0;
}
11/06/2023 CSE 1001 Department of CSE 94
Example: Convert binary to decimal
dec = bd*2n + bd*2n-1 + … + bd*21 + bd*20
e.g.-given n=101 1*22 + 0*21 + 1*20 = 5
-----------------------------------------------------------
do {
k=n%10; // binary number in n
sum= sum + k * pow(2,p);//decimal number in sum
p++;
n= n/10;
} while (n!=0);
scanf(“%d”,&num);
while(num > 0)
{
rem=num%10; e.g.- num = 31467
num =num/10; OUTPUT
if(rem%2==0) 2 even & 3 odd
ecnt++; digits
else
ocnt++;
}
Palindrome (number)
n = num; e.g.- 121
while(num>0)
{
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
}
if (n == rev)
printf(“\n\t GIVEN NO IS A PALINDROME“);
else
printf(“\n\t GIVEN NO NOT A PALINDROME“);