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

Lecture 4 - Conditional Statement

This document discusses conditional statements in programming. It covers if statements, if/else statements, if/else ladders, conditional operators, arithmetic operators, increment/decrement operators, and switch statements. Examples are provided for each. Readers are given exercises to write programs using these conditional statements, such as finding the maximum of two or three numbers, or checking if an input character is a vowel.

Uploaded by

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

Lecture 4 - Conditional Statement

This document discusses conditional statements in programming. It covers if statements, if/else statements, if/else ladders, conditional operators, arithmetic operators, increment/decrement operators, and switch statements. Examples are provided for each. Readers are given exercises to write programs using these conditional statements, such as finding the maximum of two or three numbers, or checking if an input character is a vowel.

Uploaded by

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

UNIT 1 -

PROGRAMMING
LECTURE 4 – CONDITIONAL STATEMENTS
TOPICS

 if statement  Compound statement


 if … else statement  Increase / Decrease statement
 if … else … if statement  Switch statement

Unit 1 - Programming / Lecture 3 - Introduction to C# and .NET 2


IF SINGLE-SELECTION STATEMENT
IF…ELSE DOUBLE-SELECTION STATEMENT
IF…ELSE DOUBLE-SELECTION STATEMENT
IF…ELSE DOUBLE-SELECTION STATEMENT

Exercise: Write a program to


find max between a, b
IF…ELSE DOUBLE-SELECTION STATEMENT

Exercise: Write a program to


find max among a, b and c
CONDITIONAL OPERATOR (?:)

 A ternary operator, can be used in place of if…else


o (conditional expression ? true expression : false expression)
o e.g., gradeStr = (grade >= 60 ? “Passed” : “Failed”);
o E.g., max = a > b ? a : b; max = max > c ? max : c;
 Exercise: Rewrite program find max with conditional operator
IF…ELSE IF LADDER

Instead of nesting
many levels of if…
else, we could use if…
else if ladder
IF…ELSE IF LADDER

 Instead of nesting
many levels of if…else,
we could use if…else if
ladder

 Exercise: Write
program according to
the flowchart in
previous slide
ARITHMETIC COMPOUND ASSIGNMENT
OPERATORS
INCREMENT AND DECREMENT OPERATORS
INCREMENT AND DECREMENT OPERATORS
GENERAL FORM OF SWITCH STATEMENT
GENERAL FORM OF SWITCH STATEMENT
switch(expression){
case item1:
//one or more statements;
break;
case item2:
//one or more statements;
break;
case itemn:
//one or more statements;
break;
default:
//one or more statements;
break;
}
GENERAL FORM OF SWITCH STATEMENT
ACTIVITY: ARITHMETIC OPERATORS

 Make a program to
o Ask user to input two numbers
o Ask user to input one of these arithmetic operators represented by character
(‘+’, ‘-’, ‘*’, ‘/’)
o Display “invalid operator” if user inputs the wrong character
o Else, display the corresponding result

17
HOMEWORK: VOWELS

 Make a program to
o Ask user to input a character
o Display an error message if user inputs a non alphabet character (check with
ASCII e.g., ‘a’ < c <‘z’ for lower case characters)
o If the character is alphabet, check if it is a vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) or a
consonant
o Display the result

18

You might also like