Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views21 pages

Lecture 5 Conditional Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Lecture 5 Conditional Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Conditional Statements

Lecture 5
COMPUTER FUNDAMENTALS AND PROGRAMMING

Engr. Jennelyn P. Cabale


If-else Conditional Statement
• Syntax:
if (condition)
statement;
else
statement;

• If the expression is true, it executes the statement(s) that fall under the if
statement otherwise the statement(s) under the else is executed.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 4


Example 1 : Write a program that accepts the input magic number. If the input is
right, it would display the word “RIGHT” otherwise display the word “WRONG”.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 5


Example 2 : Write a program that test the divisibility of 2 integers.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 6


Example 3 :Divisibility using if-else

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 7


Example 4 :
Finding The Maximum Of Two Integers

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 8


Example 5 : Finding The Maximum Of Three Integers

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 9


Example 6 : Sorting

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 10


Example 7 : Finding The Maximum Of Three Integers Again

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 11


Nested If Conditional Statement
• Example 8 :
• Write a program that accepts the input magic number. If the input is
right, it would display the word “RIGHT” otherwise display the word
“WRONG”. If the input number is wrong tell the user that his guess is
either too high or too low.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 12


Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 13
Example 9 :
Finding The Maximum Of Three Integers

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 14


Ladderized if-else If Conditional Statement
Syntax:
if ( condition 1 )
statement;
else if ( condition 2 )
statement;
else if ( condition 3 )
statement;
else if n
statement;

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 15


Example 10: Write a program that accepts the input magic number. If the input is right, it would display the word
“RIGHT”. If the input is wrong, display the word “WRONG…Too high” otherwise display the word “WRONG…Too Low”.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 16


Example 11: This Program Converts A Test Score Into Its Equivalent Letter Grade

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 17


The ? Alternative Conditional Statement

• Syntax:
< condition > ? < is_true > : < is_false >

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 18


Example 12: Write a program that accepts two inputs and determine which is the
highest among the two.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 19


Switch/Case Conditional Statement
Syntax:
switch (variable/expression)
{ case constant 1: statement sequence; break;
case constant 2: statement sequence; break;
case constant 3: statement sequence; break;
case constant n: statement sequence; break;
default: statement sequence;
}

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 20


Example 13: Write a program that asks the user to input a number then displays its equivalent day
of the week.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 21


Example 14: Write a program that converts a test score into its equivalent
letter grade.

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 22


THE END

Engr. Jenn P. Cabale Lecture 5 - CONDITIONAL STATEMENTS 27

You might also like