Sie sind auf Seite 1von 24

Control Statements

The C language programs until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.

Lecture 16-17

C Programming Control Statements


Mohit Arora Lecturer , LIECA LPU

If Statement
The if . then . else statement can be used with or without the else. The two forms are: if (expression) statement1

if (expression) statement1 else statement2 In both cases, when the expression is true, then statement1 is executed. If the expression is false, then, in the first case, statement1 is skipped (not executed) In the second case, statement2 after the else is executed.

The simplest form of the control statement is the If statement. It is very frequently used in decision making and allowing the flow of program execution. The If structure has the following syntax

if (condition) statement;

The statement is any valid C language statement and the condition is any valid C language expression, frequently logical operators are used in the condition statement. The condition part should not end with a semicolon, since the condition and statement should be put together as a single statement. If the condition is true then perform the following statement or If the condition is fake the computer skips the statement and moves on to the next instruction in the program.

# include <stdio.h> //Include the stdio.h file void main () // start of the program

{
int numbers; // declare the variables printf ("Type a number:"); // message to the user

scanf ("%d", &number); // read the number from standard input


if (number < 0) // check whether the number is a negative number { printf ("The value is %d \n", number); // print the value } getch(); }

If Statement

test expression true

body of if

exit

IF-ELSE
The if else is actually just on extension of the general format of if statement. If the result of the condition is true, then program statement 1 is executed, otherwise program statement 2 will be executed. If any case either program statement 1 is executed or program statement 2 is executed but not both When writing programs this else statement is so frequently required that almost all programming languages provide a special construct to handle this situation.

IfElse Statement
if (a>b) { } else { } the if part is executed if the test statement is true, otherwise the else part is executed.

If Else Statement

test expression true

false

body of if

body of else

exit

IfElse Statement
#include <stdio.h> //include the stdio.h header file in your program void main () // start of the main { int num; // declare variable num as integer printf ("Enter the number"); // message to the user scanf ("%d", &num); // read the input number from keyboard if (num < 0) // check whether number is less than zero { printf ("The number is negative"); // if it is less than zero then it is negative } else // else statement { printf ("The number is positive"); // if it is more than zero then the given number is positive } getch(); }

Compound Relational test


C language provides the mechanisms necessary to perform compound relational tests A compound relational test is simple one or more simple relational tests joined together by either the logical AND or the logical OR operators. The compound operators can be used to form complex expressions in C. Syntax

if (condition1 && condition2 && condition3) The syntax in the statement a represents a complex if statement which combines different conditions using the and operator in this case if all the conditions are true only then the whole statement is considered to be true. Even if one condition is false the whole if statement is considered to be false.

b> if (condition1 || condition2 || condition3) The statement b uses the logical operator or (||) to group different expression to be checked. In this case if any one of the expression if found to be true the whole expression considered to be true, we can also uses the mixed expressions using logical operators and and or together.

Nested if Statement

The if statement may itself contain another if statement is known as nested if statement. Syntax: if (condition1) if (condition2) statement-1; else statement-2; else statement-3;

The if statement may be nested as deeply as you need to nest it. One block of code will only be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement.

#include<stdio.h> #include<conio.h> void main() { int a=12,b=13,c=15; if (a>b) { if (b>c) { printf(sorted = %d %d %d\n,a,b,c); } else if (a>c) { printf(sorted = %d %d %d\n,a,c,b); } else printf(sorted = %d %d %d\n,c,a,b); } else { if (a>c) printf(sorted = %d %d %d\n,b,a,c); else

Nested IfElse Statement

Switch Statement
the switch statement is used if there are more than two alternatives and the decision depends on the value of the same variable the switch statement can replace a ladder of multiple nested if..else statements
switch(<variable>) { case <constant1>: break; case <constant2>: break; default : }

The exact behavior of the switch statement is controlled by the break and default commands break continues execution after the switch statement default is executed if no other match is found

Switch Statement
variable equals const 1

true
first case body

false
variable equals const 2 false true

second case body

default body exit

Switch Statement
char c; printf(Enter your choice (a/b/c) : ); Scanf(%c,&c); switch (c) { case a: printf(You picked a!\n); break; case b: printf(You picked b!\n); break; case c: printf(You picked c!\n); break; default: printf(You picked neither a,b,c !\n); }

goto statement
The

goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The goto statement and its corresponding statement label must appear in the same function.

FORWARD JUMP goto label: ------------------------------label: statement;


Jumps

Forward jump

Backward jump

BACKWARD JUMP label: statement; -------------------------------------goto label;

#include <stdio.h> #include<conio.h> void main () { int n=10; loop: printf(value is :%d, n); n--; if (n>0) goto loop;

printf("Done!); getch();
}

The primary problem with goto is that it allows a programmer to cause the point of execution to jump around the code arbitrarily. There are some restrictions on goto use. For example, you cant jump forward over a variable thats initialized in the same block as the goto

drawback of goto statement


#include<stdio.h> #include<conio.h> void main() { int x ; printf(Enter the value); scanf(%d,&x);

getch();
}

Das könnte Ihnen auch gefallen