Fundamental of C programming

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

The if-else Statements
Decisions If statements in C.
More on Algorithms and Problem Solving
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Control Flow C and Data Structures Baojian Hua
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Conditional Statement
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4 Selection Structures: Making Decisions.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 5: Structured Programming
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Decision making If.. else statement.
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
Chapter 4: Control Structures I
Decisions Chapter 4.
Chapter 2.1 Control Structures (Selection)
Condition Statements.
Programming Fundamentals
JavaScript: Control Statements.
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
CS1100 Computational Engineering
Additional Control Structures
Structured Program
3 Control Statements:.
Chapter 7 Conditional Statements
Decision making If statement.
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Fundamental of C programming - Ompal Singh

Conditional Statement IF- Statement: It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation. Syntax: If (Expression) { Statement 1; Statement 2; } Where a statement may consist of a single statement, a compound statement or nothing as an empty statement. The Expression also referred so as test condition must be enclosed in parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non zero value), then the statement associated with it will be executed otherwise ignored and the control will pass to the next statement.

printf (“a is larger than b”); } Example: if (a>=b) { printf (“a is larger than b”); } true false a>=b print “A is greater”

IF-ELSE Statement: An if statement may also optionally contain a second statement, the ``else clause,'' which is to be executed if the condition is not met. Syntax: If (Expression) { Statement 1; } else statement; stalement;

printf (“a is larger than b”); } else Example: if (a>=b) { printf (“a is larger than b”); } else printf (“b is larger than a”); false true print “a is greater” print “b is greater” a >= b

if if/else Only performs an action if the condition is true Specifies an action to be performed both when the condition is true and when it is false

Else if else/if structures Test for multiple conditions by placing else/if selection structures inside else/if selection structures Once condition is met, rest of statements skipped

Syntax: if (test expression) to be executed if test expression is true; else if(test expression 1) statements to be executed if test expression is true; else if (test expression 2) Statements to be execute if test expression is true; else statements to be executed if all test expressions are false.

Example: int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are pretty young!\n" ); } else if ( age == 100 ) { printf( "You are old\n" ); } else { printf( "You are really old\n" ); } return 0; }

Example if( grade >= 90) printf("You got an A!\n"); else if ( grade >= 80 ) printf("You got a B!\n"); else if ( grade >= 70 ) printf("You got a C!\n"); else if ( grade >= 60 ) printf("You got a D!\n"); else printf("You failed!\n");

Nested if...else statement (if...elseif....else Statement) The if...else statement can be used in nested form when a serious decision are involved. Syntax: if (test expression) to be executed if test expression is true; Else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2) . . . statements to be executed if all test expressions are false.

Example: int main(){ int numb1, numb2; printf("Enter two integers to check"); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) printf("Result: %d=%d",numb1,numb2); else if(numb1>numb2) printf("Result: %d>%d",numb1,numb2); printf("Result: %d>%d",numb2,numb1); return 0; }

Switch Case This is another form of the multi way decision. It is well structured, but can only be used in certain cases where; Only one variable is tested, all branches must depend on the value of that variable. The variable may be an integral type. (int, long, short or char). Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

Syntax switch ( integer expression ) { case constant1 : statement(s) break ; case constant2 : . . . default: }

Int n; Printf(“Enter the choice”) Scanf(“%d”,&n); switch ( n ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; case 2: printf (“Tuesday\n”) ; case 3: printf (“Wednesday\n”) ; case 4: printf (“Thursday\n”) ; case 5: printf (“Friday\n”) ; case 6: printf (“Saturday\n”) ; default: printf (“Error -- invalid day.\n”) ; }

#include<stdio.h> #include<conio.h> void main() { int a,b,c,ch; clrscr(); printf("--------Main Menu--------\n\n"); printf("1.Add\n"); printf("2.Subtraction\n"); printf("3.Multiply\n\n\n"); printf("Enter the choice\n\n"); scanf("%d",&ch); printf("\n\nenter the number a and b\n"); scanf("%d%d",&a,&b); switch(ch) case 1: c=a+b; printf("%d",c); break; case 2: c=a-b; printf("%d",c); break; case 3: c=a*b; default: printf("wrong choice"); } getch();

What is a loop? A loop is a programming structure that contains an executable block of code that repeats so long as a given condition is true/not true. Good programmers include a way to satisfy the condition somewhere inside the executable block. Types of Loop While Do While For

While Loop/Pre-Test Loops In pre-test loops, the executable block of code is execute after the condition test is true. the while loop is a pre-test loop. Syntax: initialization; While(condition) { statement; inc/dec; }

Without loop With loop #include<stdio.h> void main() { int i=1; while(i<=5) printf("hello\n"); i++; } #include<stdio.h> void main() { printf("hello\n"); } initialization Condition Increment

#include<stdio.h> Void main() { int i=10; while(i>=1) printf(“%d”,i); i--; or i=i-1; } Output:10 9 8 7 6 5 4 3 2 1 #include<stdio.h> Void main() { int i=1; while(i<=10) printf(“%d”,i); i++; or i=i+1; } Output:1 2 3 4 5 6 7 8 9 10 initialization Condition increment

Sum from 1 to 100 (while) int i=1,sum=0; while(i<=100) { sum=sum+i; } Printf(“%d”,sum);

Program to print a table #include<stdio.h> #include<conio.h> void main() { int i,n,t; clrscr(); i=1; printf("Enter the number"); scanf("%d",&n); while(i<=10) t=n*i; printf("\n%d",t); i++; } getch();

Do while/Post-Test Loops In post-test loops, the executable block of code is execute before the condition is test. The loop will always execute at least one during the run of a program. The do…while loop is a post-test loop. Syntax: initialization; do{ statement; inc/dec; } While(condition);

#include<stdio.h> Void main() { int i=1; do printf(“%d”,i); i++; or i=i+1; }while(i<=10); } Output:1 2 3 4 5 6 7 8 9 10 #include<stdio.h> Void main() { int i=10; do printf(“%d”,i); i--; or i=i-1; }while(i>=1); } Output:10 9 8 7 6 5 4 3 2 1 initialization increment Condition

Sum from 1 to 100 (do-while) int i=1,sum=0; do{ sum=sum+i; i++; Printf(“%d”,sum);

For Loop In C programming languages, the for construct is composed of three parts, each divided by a semicolon: Syntax for(initialization; test condition; inc/dec) { statement; } In the first section, the initialization, we initialize an index variable (which is usually being named i, j or k) to its initial value; In the second part, we test whether a variable (usually the same we have just initialized in the first part) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it; In the third and last part, we update the variable — usually by incrementing or decrementing it by 1.

#include<stdio.h> Void main() { int i; For(i=1;i<=10;i++) printf(“%d”,i); } Output:1 2 3 4 5 6 7 8 9 10 #include<stdio.h> Void main() { int i; For(i=10;i>=1;i--) printf(“%d”,i); } Output:10 9 8 7 6 5 4 3 2 1

Sum from 1 to 100 (for) int i,sum=0; for(i=1;i<=100;i++) { sum=sum+i; } Printf(“%d”,sum);

break Statement • The break statement can be used in while, do-while, and for loops to exit from the loop.

Example break in a for Loop #include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) break ; } printf (“%d “, i) ; printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; OUTPUT: 1 2 3 4 Broke out of loop at i = 5.

The continue Statement • The continue statement can be used in while, do-while, and for loops. • It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.

Example continue in a for Loop #include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) continue ; } printf (“%d ”, i) ; printf (“\nDone.\n”) ; return 0 ; OUTPUT: 1 2 3 4 6 7 8 9

Nested Loops • Loops may be nested (embedded) inside of each other. • Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. • It is common to see nested for loops.

Nested Loop * int i,j; ** for(i=0;i<5;i++) *** { **** ***** int i,j; for(i=0;i<5;i++) { for(j=0;j<i;j++) printf("*"); } printf("\n");