190 likes | 214 Views
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
E N D
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 statements; } else { // code to be executed if the // condition_expression is false statements; }
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; • }
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 }
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; }
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; }
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 */ } }
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; }
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; }
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
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
Practice Problem-3 • Try Problem-1 using switch case.
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.
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.
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