CSE 109 Computer Programming: Introduction To Control Statements
CSE 109 Computer Programming: Introduction To Control Statements
Computer Programming
Introduction to Control Statements
Prepared by
Johra Muhammad Moosa
Assistant Professor
CSE, BUET
Modified by
Madhusudan Basak
Assistant Professor
CSE, BUET
if statement
num>= num is
true
0 positive
false
if statement
• Selection statement/conditional statement
• Operation governed by outcome of a conditional test
• if(expression) statement;
– expression:
• any valid C expression
– If expression is true statement will be executed
– If expression is false statement will be bypassed
– true: any nonzero value
– false: zero
– if(num+1) printf(“nonzero");//num!=-1 statement will execute
– Normally expression consists of relational & logical operator
true, false
– true: any nonzero value
– false: zero
if statement
#include<stdio.h>
int main(void)
{
int num;
scanf("%d", &num);
if(num>=0) printf("num is positive");//if(num>-1)
return 0;
}
if statement
• Common programming error:
– Placing ; (semicolon) immediately after condition in if
• if(expression); statement;
– Confusing equality operator (==) with assignment operator
(=)
• if(a=b)
• if(a=5)
• if(9=5)
– left operand must be l-value
• if(9+5)
if statement
#include<stdio.h>
int main(void)
{
int num;
scanf("%d", &num);
if(num>=0) printf("num is positive");//if(num>-1)
if(num<0) printf("num is negative");
return 0;
}
if-else statement
• if(expression) statement1;
else statement2;
– If expression is true statement1 will be evaluated and statement2
will be skipped
– If expression is false statement1 will be bypassed and statement2
will be executed
– Under no circumstances both the statements will execute
– Two-way decision path
if-else statement
false
num is
negative
Notice indentation if-else statement
#include<stdio.h>
int main(void)
{
if(num>-1)
int num;
scanf("%d", &num); Condition gives same result
if(num>=0)
printf("num is positive");
else
printf("num is negative");
return 0;
}
if-else statement
• else part is optional
• The else is associated with closest else-less if
• if(n>0)
if(a>b) z=a; Even though the else is
else z=b; indented with the first if, by
• Braces must be used to force association with therulefirst
it will be associated with
• if(n>0) the nearest if
{
if(a>b) z=a;
}
else z=b;
if-else statement
• else part is optional
• The else is associated with closest else-less if
• if(n>0)
if(a>b) z=a;
else z=b; Even though the else is
indented with the first if, by
• Where else will be associated is shown by indentation
rule it will be associated with
• Braces must be used to force association with the first
• if(n>0) the nearest if
{
if(a>b) z=a;
}
else z=b;
Nested if (section 3.2)
#include<stdio.h> else
int main(void) {
{ if(id<61)
int id; printf("B1\n");
printf("Please enter last 3 digits else
of your id:\n"); printf("B2\n");
scanf("%d", &id); }
printf("You are in "); return 0;
if(id%2) }
{
if(id<60)
printf("A1\n");
else
printf("A2\n");
}
for while
while loop
• Common error
– Forgetting to increment
• Normally used when increment is not needed
– while(ch!=‘q’)
{
…
ch=getchar();
}
do while loop
• for(initialization; conditional-test; increment) statement;
• initialization;
do
{
statement;
increment;
} while(conditional-test);
do while loop
• Test is at the bottom
• Will execute at least once
• do
{
…
ch=getchar();
} while(ch!=‘q’);
• Common error
– Forgetting the semicolon (;) after while
while loop
for do while
for loop
• https://www.geeksforgeeks.org/interesting-facts-about-switch-statement-in
-c/
Thank You