CSCE 206 Lab Structured Programming in C

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

The if-else Statements
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 CSE1301 Computer Programming Lecture 8 Selection.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 CSE1301 Computer Programming Lecture 9 Selection.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CPS120: Introduction to Computer Science Decision Making in Programs.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Branches and Program Design
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Tutorial #4 Summer 2005.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
C++ Lesson 1.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Decision Making in C.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Administrative things
Decisions Chapter 4.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CS1010 Programming Methodology
Condition Statements.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Introduction to Programming
Conditional Branching
Decision Making.
Lec 5.
Selection CSCE 121 J. Michael Moore.
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
CSE 1020:Control Structure
CSCE 206 Lab Structured Programming in C
Conditions and Boolean Expressions
Visual Basic – Decision Statements
CSI 121 Structure Programming Language Lecture 9 Selection
Conditionals.
If Statements.
SELECTIONS STATEMENTS
Control Structures Lecture 6.
More on conditional statements
Nate Brunelle Today: Conditional Decision Statements
Introduction to Computing Lecture 04: Booleans & Selection
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
Administrative things
Presentation transcript:

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

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;

Operators

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); printf("%d is a negative number", a); return 0;

switch case

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"); default: printf("No data"); return 0;

Now it’s your turn!

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