Understanding the Use of '==' in While Loops: Explanation and Example

  • Thread starter chetzread
  • Start date
  • Tags
    Loop
In summary, the conversation is discussing the use of character constants in C and how they can be used in control structures. The question is about why the character constant 'y' is being checked twice in the code. The response explains that it is to account for both upper and lower case input from the user. The conversation also mentions other types of constants in C such as string, integer, and floating point constants.
  • #1
chetzread
801
1

Homework Statement


for teh circled part , why there's again =='y' ? i didnt see the variable 'y' anywhere other than the circled part ...What does it mean ?

Homework Equations

The Attempt at a Solution


Is the code wrong ?
 

Attachments

  • 1.PNG
    1.PNG
    11.9 KB · Views: 346
Physics news on Phys.org
  • #2
Characters in quotes are character constants. The statement in question is checking whether the user has input a 'Y' or a 'y' (so that both upper and lower case are checked).
 
  • #3
@chetzread, before you learned about loops and other control structures, there was probably some mention about the various types of constants that can be used in C. At least most of the books I've seen present such material in that order.

Character constants, such as 'y' or 'B' or '\t'
String constants, such as "cat"
integer constants, such as 23, 023, and 0x23 (all with different values)
floating point constants, such as 2.7 (a double) and 3.09F (a float)
This is not meant to be an exhaustive list.
 

Related to Understanding the Use of '==' in While Loops: Explanation and Example

What is the purpose of using '==' in while loops?

The '==' operator is used to compare two values and determine if they are equal. This is useful in while loops because it allows the loop to continue running as long as the specified condition is true.

How does the '==' operator work in while loops?

The '==' operator compares the values on either side of it and returns a boolean value (true or false) based on whether the values are equal or not. In while loops, the loop will continue to run as long as the statement using '==' evaluates to true.

Are there any other comparison operators that can be used in while loops?

Yes, there are several other comparison operators that can be used in while loops, such as '>', '<', '>=', '<=', '!=', and '!=='. These operators allow for more specific and complex conditions to be used in the while loop.

Can the '==' operator be used to compare different data types in while loops?

Yes, the '==' operator can be used to compare different data types in while loops. However, it is important to keep in mind that the values being compared must be of the same type, otherwise the comparison may not result in the desired outcome.

Can the '==' operator be used to assign values in while loops?

No, the '==' operator is strictly used for comparison and cannot be used to assign values in while loops. To assign values, the '=' operator should be used instead.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top