lesson plan in conditional statement 02-16-12

16
Conditional Statements If Else If Else If Switch

Upload: danica-corpuz

Post on 26-Oct-2014

191 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lesson Plan in Conditional Statement 02-16-12

Conditional Statements

If ElseIf Else If

Switch

Page 2: Lesson Plan in Conditional Statement 02-16-12

are statements that check an expression/condition then may or may not execute a statement or group of statements depending on the result of the condition.

Conditional Statements

Page 3: Lesson Plan in Conditional Statement 02-16-12

if(condition)statement 1

elsestatement 2

Where:if and else are keywordscondition is relational expressionstatement 1 & 2 are results

If Else Statement

Page 4: Lesson Plan in Conditional Statement 02-16-12

Write a program that will output “CONGRATULATIONS YOU PASSED!”, if the student’s grade is greater than or equal to 60. Otherwise output, “SORRY YOU FAILED!”.

Example:

Page 5: Lesson Plan in Conditional Statement 02-16-12

#include<stdio.h>int grade;main(){

clrscr();printf(“Enter student grade:”);scanf(“%d”, &grade);if (grade >= 75)printf(“CONGRATULATIONS YOU

PASSED!”);elseprintf(“SORRY YOU FAILED!”)

getche();}

Page 6: Lesson Plan in Conditional Statement 02-16-12

if (Condition1)statement1;

else if(Condition2)statement2;

else if(Condition3)statement3;

elsestatement_else;

If Else If Statement

Page 7: Lesson Plan in Conditional Statement 02-16-12

In an if-else-if statement, the expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.

Remember!

Page 8: Lesson Plan in Conditional Statement 02-16-12

Write a program that will ask the user to input an integer then output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the input number is not within 1-7, output is “Day is not available!”

Example

Page 9: Lesson Plan in Conditional Statement 02-16-12

#include<stdio.h>main(){Int day;printf(“Enter an integer:”);scanf(“%d”, &day”);

If(day==1)printf(“Sunday”);

else if (day==2)printf(“Monday”);

;;else printf(“Day is not available!”);

getch();}

Page 10: Lesson Plan in Conditional Statement 02-16-12

switch(variable){

case constant1:statement sequence1;break;

case constant2:statement sequence2;break;

;;default:

statement sequence_default;}

Switch Statement

Page 11: Lesson Plan in Conditional Statement 02-16-12

Write a program that will ask the user to input an integer then output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the input number is not within 1-7, output is “Day is not available!”

Example

Page 12: Lesson Plan in Conditional Statement 02-16-12

#include<stdio.h>main(){int day;printf(‘Enter an integer:”);scanf(‘%d”, &day);

switch(day){case 1:

printf(“Sunday”);break;

case 2:printf(“Monday”);break;

default:printf(“Day is not available!”);

}getch();}

Page 13: Lesson Plan in Conditional Statement 02-16-12

If Else Statement Design a program which identifies the larger of two

numbers supplied by the user.

If Else If Statement Design a program to display the year level if students

based on their year entry number. Here are the given criteria:

Hands On

Entry Number High School Status1 Freshman2 Sophomore3 Junior4 Senior

Other entry number Out-of-school youth

Page 14: Lesson Plan in Conditional Statement 02-16-12

Switch Statement Write a program that will ask the user to

input 2 integers and a character (A, S, M, D). If the user inputs ‘A’ for the character, add the 2 numbers, if ‘S’ subtract the 2 numbers, if ‘M’ multiply and if ‘D’ divide the numbers. Output the computed value.

Hands On

Page 15: Lesson Plan in Conditional Statement 02-16-12

Design a program which determines if the supply age by the user is qualified to vote or not. The qualifying is 18 and above.

Evaluation

Page 16: Lesson Plan in Conditional Statement 02-16-12

Write a program that accepts the input magic number. If the input is right, the magic words will be displayed. The magic number is 143 and its corresponding magic words are “I love you”. If the input number is wrong, displays “Sorry, better luck next time”.

Assignment