1 / 19

CSCE 206 Lab Structured Programming in C

CSCE 206 Lab Structured Programming in C. Fall 2018 Lecture 3 Luna BACKES. Today’s objective. If-else logic block If - else if - else logic block switch logic block. if-else. if ( condition_expression ) { // code to be executed if the // condition_expression is true

dwentz
Télécharger la présentation

CSCE 206 Lab Structured Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 206 Lab Structured Programming in C Fall 2018 Lecture 3 Luna BACKES

  2. Today’s objective • If-else logic block • If - else if - else logic block • switch logic block

  3. if-else if (condition_expression) { // code to be executed if the // condition_expression is true statements; } else { // code to be executed if the // condition_expression is false statements; }

  4. Sample Code-1 #include <stdio.h> int main(void) { int a; • printf("Enter a number:"); // Asks user for input • scanf("%d", &a); • if (a > 0) • { printf("%d is a positive number", a); • } • else • { printf("%d is not a positive number", a); • } • return 0; • }

  5. if-else if-else if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else if (condition3) { // code to be executed if condition3 is true } … else { // code to be executed if all conditions are false }

  6. Sample Code-2 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d", &a); if (a>0) { printf("%d is a positive number", a); } else if (a<0) { printf("%d is a negative number", a); } else { printf("%d is zero",a); } return 0; }

  7. Operators

  8. Sample Code-3 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d", &a); if (a > 0 || a == 0) { printf("%d is not a negative number", a); } else { printf("%d is a negative number", a); } return 0; }

  9. Nested if-else block if ( Boolean_expression1) { /* Executes when the Boolean expression 1 is true */ if ( boolean_expression2) { /* Executes when the Boolean expression 2 is true */ } }

  10. Sample Code-4 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d", &a); if (a >= 0) { if(a == 0) { printf("%d is zero", a); } else { printf("%d is a positive number", a); } } else { printf("%d is a negative number", a); } return 0; }

  11. switch case

  12. Sample Code-5 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d",&a); switch(a) { case 0: { printf("Zero"); break; } case 1: { printf("One"); break; } default: { printf("No data"); break; } } return 0; }

  13. Now it’s your turn!

  14. Practice Problem-1 • Write a program that takes an integer as input and determines whether it is odd or even. It gives a proper sentence as output. • Tips: Use if-else block

  15. Practice Problem-2 • Write a program that takes 3 integers as input and prints out the largest number of them. • Tips: Use if-else if-else block

  16. Practice Problem-3 • Try Problem-1 using switch case.

  17. Practice Problem-4 • Write a C program that gets a character from the input and outputs whether the character was a vowel or a consonant.

  18. Practice Problem-5 • Write a C program that gets 3 floats from the input that represent the sides of a triangle, and outputs whether the triangle is equilateral, isosceles or scalene.

  19. Practice Problem-6 • Write a C program that gets a number (single digit) from the input and outputs the number in words. • For example: • Input: 3 • Output: Three

More Related