Session 04 Introduction to Programming

20 136 0
Session 04 Introduction to Programming

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Differentiate between Command, Program and SoftwareExplain the beginning of CExplain when and why is C usedDiscuss the C program structureDiscuss algorithmsDraw flowchartsList the symbols used in flowcharts

LBC, Session 4 Condition FPT APTECH COMPUTER EDUCATION HANOI Objectives  Explain the Selection Construct - If Statement - If – else statement - Multi if statement - Nested if statement  Switch statement LBC/Session 4 2 Conditional Statement • Conditional statements enable us to change the flow of the program • A conditional statement evaluates to either a true or a false value Example : To find whether a number is even or odd: 1. Accept a number 2. Find the remainder by dividing the number by 2 3. If the remainder is zero, the number is “EVEN” 4. Or if the remainder is not zero the number is “ODD” LBC/Session 4 3 Selection Constructs C supports two types of selection statements The if statement The switch statement LBC/Session 4 4 The if statement-1 Syntax: if(expression) statements; If the if expression evaluates to true, the block following the if statement or statements are executed LBC/Session 4 5 The if statement-2 Program to display the values based on a condition void main() { int x, y; char a = ‘y’; x = y = 0; if (a == ‘y’) { x += 5; printf(“The numbers are %d and \t%d”, x, y); } } LBC/Session 4 6 The if – else statement-1 Syntax: if(expression) statements; else statements; LBC/Session 4 7 The if – else statement-2 • If the if expression evaluates to true, the block following the if statement or statements are executed • If the if expression does not evaluate to true then the statements following the else expression take over control • The else statement is optional. It is used only if a statement or a sequence of statements are to be executed in case the if expression evaluates to false LBC/Session 4 8 The if – else statement -3 Program to display whether a number is Even or Odd void main() { int num , res ; printf(“Enter a number :”); scanf(“%d”,&num); res = num % 2; if (res == 0) printf(“Then number is Even”); else printf(“The number is Odd”); } LBC/Session 4 9 The if–else–if statement-1 Syntax: if(expression) statements; else if(expression) statements; else if(expression) statements; … … else statements; LBC/Session 4 10 The if–else–if statement-2 • The if – else – if statement is also known as the ifelse-if ladder or the if-else-if staircase • The conditions are evaluated from the top downwards LBC/Session 4 11 The if–else–if statement-3 Program to display a message based on a value void main() { int x; x = 0; printf(“Enter Choice (1 - 3) : “); scanf(“%d”, &x); if (x == 1) printf (“\nChoice is 1”); else if ( x == 2) printf (“\nChoice is 2”); else if ( x == 3) printf (“\nChoice is 3”); else printf (“\nInvalid Choice “); } LBC/Session 4 12 Nested if-1 • The nested if is an if statement, which is placed within another if or else • In C, an else statement always refers to the nearest if statement that is within the same block as the else statement and is not already associated with an if • According to ANSI standards, a compiler should support at least 15 levels of nesting LBC/Session 4 13 Nested if-2 Syntax: if(expression){ if(expression) statements; if(expression) statements; else statements; } else{ statements; } LBC/Session 4 14 Nested if-3 void main () { int x, y; x = y = 0; printf (“Enter Choice (1 - 3) : “); scanf (“%d”, &x); if (x == 1){ printf(“\nEnter value for y (1 - 5) : “); scanf (“%d”, &y); if (y ‘z’) printf(“\nCharacter not a lower cased else switch (ch) { case ‘a’ : case ‘e’ : case ‘i’ : case ‘o’ : case ‘u’ : printf(“\nCharacter is a break; case ‘z’ : printf (“\nLast Alphabet break; default : printf(“\nCharacter is a break; } LBC/Session 4 } alphabet”); vowel”); (z) was entered”); consonant”); 19 Summary • Conditional statements enable us to change the flow of the program • C supports two types of selection statements: if and switch • Some of the conditional statements: – The if statement – The if…else statement – Nested if statements – The switch statement LBC/Session 4 20 [...]... conditions are evaluated from the top downwards LBC /Session 4 11 The if–else–if statement-3 Program to display a message based on a value void main() { int x; x = 0; printf(“Enter Choice (1 - 3) : “); scanf(“%d”, &x); if (x == 1) printf (“\nChoice is 1”); else if ( x == 2) printf (“\nChoice is 2”); else if ( x == 3) printf (“\nChoice is 3”); else printf (“\nInvalid Choice “); } LBC /Session 4 12 Nested if-1... statement always refers to the nearest if statement that is within the same block as the else statement and is not already associated with an if • According to ANSI standards, a compiler should support at least 15 levels of nesting LBC /Session 4 13 Nested if-2 Syntax: if(expression){ if(expression) statements; if(expression) statements; else statements; } else{ statements; } LBC /Session 4 14 Nested if-3... constant2: statements; break; case constant3: statements; break; … … … default LBC /Session 4 } 17 The switch statement-3 Program to check whether the entered lowercase character is vowel or ‘z’ or a consonant void main () { char ch; printf (“\nEnter a lower cased alphabet (a - z) : “); scanf(“%c”, &ch); contd…… LBC /Session 4 18 The switch statement - 4 if (ch < ‘a’ || ch > ‘z’) printf(“\nCharacter... printf(“\nCharacter is a break; } LBC /Session 4 } alphabet”); vowel”); (z) was entered”); consonant”); 19 Summary • Conditional statements enable us to change the flow of the program • C supports two types of selection statements: if and switch • Some of the conditional statements: – The if statement – The if…else statement – Nested if statements – The switch statement LBC /Session 4 20 ... y exceeds 5 “); } else printf (“\nChoice entered was not 1”); } LBC /Session 4 15 The switch statement-1 • The switch statement is a multi-way decision maker that tests the value of an expression against a list of integers or character constants • When a match is found, the statements associated with that constant are executed LBC /Session 4 16 The switch statement-2 Syntax: switch(expression){ case ... remainder is not zero the number is “ODD” LBC /Session Selection Constructs C supports two types of selection statements The if statement The switch statement LBC /Session 4 The if statement-1 Syntax: if(expression)... printf(“The numbers are %d and %d”, x, y); } } LBC /Session The if – else statement-1 Syntax: if(expression) statements; else statements; LBC /Session The if – else statement-2 • If the if expression... is Odd”); } LBC /Session The if–else–if statement-1 Syntax: if(expression) statements; else if(expression) statements; else if(expression) statements; … … else statements; LBC /Session 10 The if–else–if

Ngày đăng: 08/10/2015, 22:23

Từ khóa liên quan

Mục lục

  • LBC, Session 4

  • PowerPoint Presentation

  • Conditional Statement

  • Slide 4

  • Slide 5

  • Slide 6

  • The if – else statement-1

  • The if – else statement-2

  • The if – else statement -3

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Summary

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan