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

Muhammad Ali Nayeem Student of Knowledge

This document discusses control statements in C programming, specifically the if and else statements. It provides examples of using if and else statements to control program flow based on expressions evaluating to true or false. Key points covered include: - The basic syntax of if statements and else statements - Using relational and logical operators in expressions - The else statement executing code when the if expression is false - Using braces {} to group multiple statements - Nesting if-else statements within each other - Logical operators like && and || to connect expressions

Uploaded by

IamIN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Muhammad Ali Nayeem Student of Knowledge

This document discusses control statements in C programming, specifically the if and else statements. It provides examples of using if and else statements to control program flow based on expressions evaluating to true or false. Key points covered include: - The basic syntax of if statements and else statements - Using relational and logical operators in expressions - The else statement executing code when the if expression is false - Using braces {} to group multiple statements - Nesting if-else statements within each other - Logical operators like && and || to connect expressions

Uploaded by

IamIN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Muhammad Ali Nayeem

Student of Knowledge
http://teacher.buet.ac.bd/ali_nayeem/
 We have already learned 2 types of statements
 Declaration statement
 Assignment statements

 Insha'Allah we will learn a new type of statement today


 Control Statement
 Control Statements determine your program’s flow of execution
 Order of execution of statements in a program

 Default order ?
 Often we need to alter/change order
 When?
if
 General form if(expression)
statement;
 A new keyword
 expression:
 any valid C expression
 know as target

 If expression is non-zero statement will be executed


 If expression is zero statement will be bypassed/skipped
 Normally expression consists of relational & logical
operator
 Allow us to compare two values
#include<stdio.h>
main()
{
int num;
scanf("%d", &num);
if(num>=0)
printf("num is positive");/*if(num>-1)*/
if(num<0)
printf("num is negative");
}
 Placing ; (semicolon) immediately after condition in if
 if(expression); statement;
 Confusing equality operator (==) with assignment operator (=)
 if(a=b)
 The assignment operators return the value of the
variable specified by the left operand after the
assignment
 The resultant type is the type of the left operand
 if(a=5)
 if does nothing when the expression evaluates to false
 Can we execute one statement if the expression evaluates to true and
another statement if the expression evaluates to false?
 Of course! This is what is the purpose of the else statement
if(expression)
statement1;
else
statement2;
 If expression is true statement1 will be evaluated and statement1 will be skipped
 If expression is false statement1 will be bypassed and statement2 will be executed

 Under no circumstances both the statements will execute


 Mutually exclusive

 else part is optional


#include<stdio.h>
main()
{
int num;
scanf("%d", &num);
if(num>=0)
printf("num is positive");//if(num>-1)
else
printf("num is negative");
}
 Statements enclosed within { }
 Group two or more statements into one unit
 Can be used anywhere a single statement can
 Common programming error:
 Forgetting braces of compound statements/blocks
 if(expression)
{
statement1;
statement2;

statementN;
}
else
{
statement1;
statement2;

statementN;
}
 If expression is true all the statements with if will be executed
 If expression is false all the statements with else will be executed
 Connect together true/false results
 ‘&&’ logical AND binary
 ‘||’ logical OR binary
 ‘!’ logical NOT unary
 It is perfectly all right if we write an entire if-else construct within
 either the body of the if statement
 or the body of an else statement.

 This is called ‘nesting’of ifs


#include<stdio.h>
main()
{
int id;
printf("Please enter last 3 digits of your id:\n");
scanf("%d", &id);
if(id>130 && id<196)
{
if(id<163)
printf("You are in C1\n");
else
printf("You are in C2\n");
}
else
{
printf("You are in A or B\n");
}
}
 The marks obtained by a student in 5 different subjects are input
through the keyboard. The student gets a division as per the
following rules:
 Percentage above or equal to 60 - First division
 Percentage between 50 and 59 - Second division
 Percentage between 40 and 49 - Third division
 Percentage less than 40 - Fail

 Write a program to calculate the division obtained by the student.


 Johra Muhammad Moosa
 Lecturer
 Department of Computer Science & Engineering
 Bangladesh University of Engineering & Technology

You might also like