You are on page 1of 6

WEEK 8 Fundamental programing BSCS : z-section

Topic:

Operator and conditional statement

if statement and else statement

Introduction: if statement and else statement

we've learnt how to collect basic data from the user, but wouldn't it be useful

if we could do different things depending on what the user typed in? Well this

happens to be a very core concept of computer programming, and we can do

exactly as previously described with these things called 'if' statements. These

are basic statements which allow us to do certain things only in certain

conditions.

The first thing we're going to learn about is the 'if' itself. Just write

the  if  keyword and then in some brackets, the condition. To specify the

condition you simply write one value (either a variable or constant), then the

comparison operator you want to compare them with (for example - equal to,

which is  == ) and then the second value (either a variable or constant). We

then put some curly brackets, and anything inside the curly brackets is what

will be executed if the condition is true. For example, the following would

compare 1 to 1 (which is a bit silly, but gives an example which is obviously

always true)
C++ Conditions and If Statements
C++ supports the usual logical conditions from mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

C++ has the following conditional statements:

 Use if to specify a block of code to be executed, if a specified condition is


true
 Use else to specify a block of code to be executed, if the same condition
is false
 Use else if to specify a new condition to test, if the first condition is
false
 Use switch to specify many alternative blocks of code to be executed

Difference between condition true and false

The  if  statement evaluates the  condition  inside the parentheses  ( ) .


 If the  condition  evaluates to  true , the code inside the body of  if  is
executed.
 If the  condition  evaluates to  false , the code inside the body of  if  is
skipped.
Note: The code inside  { }  is the body of the  if  statement.
Program #1

Write a simple program that demonstrate the use of if statement (conditional statement if) with sign
greater then.

program #2

write a simple program that demonstrate the use of if statement (conditional statement if) with sign
less than.
Program #3

Write a simple program that demonstrate the use of if else statement (conditional statement ) with sign
less than and greater than .

Program #4

Write a simple program that demonstrate the use of if else statement (conditional statement) with sign
less than and greater than.
Program # 5

Online compiler

https://www.programiz.com/cpp-programming/online-compiler/
https://www.w3schools.com/cpp/trycpp.asp?filename=demo_if2

https://www.programiz.com/cpp-programming/if-else

Home task for week 8

Run at least four program related to if and if else statement and add
to your lab manual document.

You might also like